Home » Databases » Sybase » ASE » Perl Problem with filtered Mason code & db connection

Perl Problem with filtered Mason code & db connection

Hi,

In chapter 5 (Advanced Features pgs 82,83) of Embedding Perl in HTML with Mason from O’Reilly, the"a simple SQL select expressed in something like a taglib style" example appears to be straight forward.  It is but it doesn’t seem to work too well.

The premise is that the ".components/sql/select" will filter the chunk of html code:

< &| .components/sql/select, query => 'SELECT name, type FROM sysobjects' &>
  <tr>
     <td>%name</td>
     <td>%type</td>
  </tr>

The select returns data correctly but the ".components/sql/select" doesn’t appear to be printing the code hmmmm….  see the very bottom for the answer… I didn’t catch it for awhile but later saw the cause and could have kicked myself.

produced HTML code:

<html>
<head>
  <title> Databases</title>
</head>
<body>
   <table border="1">
 	<tr>
 		<th>Server</th><th>Service Type</th>
	</tr>
    </table>

    <center><img src="/images/seal.png" height="170" width="170"/></center>
</body>
</html>

test.html:

<table border="1">
    <tr>
        <th>Server</th>
        <th>Service Type</th>
    </tr>
    < &| .components/sql/select, query => ‘SELECT name, type FROM sysobjects’ &>
        <tr>
            <td>%name</td>
            <td>%type</td>
        </tr>
    
</table>

.components/sql/select:

    < %args>
        $query
    
    < %init>
        my $sth = $dbh->prepare($query);

        while (my $row = $sth->fetchrow_hashref) {
            my $content = $m->content;

            $content =~ s/%(w+)/$row->{$1}/g;
            $m->print($content);
        }

        $sth->finish;

Solution:

     < %init>
        my $sth = $dbh->prepare($query);

        while (my $row = $sth->fetchrow_hashref) {

should be:

    < %init>
        my $sth = $dbh->prepare($query);

        $sth->execute();  # dang typos in the book Cry

        while (my $row = $sth->fetchrow_hashref) {
Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Facebook login by WP-FB-AutoConnect