home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeMySQL and mSQLSearch this book

12.4. Embedded Perl

Several Perl modules and related programs let you embed Perl code into an HTML document. A CGI program then executes this code before sending the final HTML file to the browser.

The most obvious advantage that these solutions have over W3-mSQL and PHP is that the scripting language used in the HTML file is regular Perl. Although they may be easy to learn and similar to C and Perl in style, Lite and the PHP scripting language are unique, proprietary languages that exist only for their one use. Perl, on the other hand, is virtually ubiquitous. It is a standardized programming language with years of bug elimination and extensive security features. There is a persuasive argument to using this sort of solution.

12.4.2. EmbPerl

EmbPerl is a more recent creation than ePerl, which is more specifically focused on HTML and the web. There are additional "metacommands" -- HTML style tags processed by EmbPerl -- that allow flow control and other programming features within the HTML itself.

As an example of Perl code embedded within an HTML file, consider the shark database output form used earlier. We will use EmbPerl for our example, but since we are using a standard language (Perl) the code in the page would be nearly identical between the different Perl embedders.

<HTML>
<HEAD><TITLE>Shark Search Result</title></head>
<BODY>
<H1>Here are the sharks that match your search...</h1>
<p>
[-
   use Msql;
   use CGI qw(:standard);

   $dbh = Msql->connect;
   $dbh->selectdb("sharks");

   %age = ( '0' => 'Young',
            '1' => 'Adult',
            '2' => 'Old'
          );
   # We now start to build the query. When finished, a typical query
   # will look something like this:
   # SELECT * FROM SHARK WHERE SPECIES='Isurus Paucus' AND AGE=2

   $query = "select * from sharks";
   if ($species or $age or $location) {
      $query .= " where ";
      $query .= join(" and ", param);
   }

   $result = $dbh->query($query);
   if (result == -1) {
      echo("Error : " . Msql->errmsg . "\n");
      exit(1);
   }
   $numresults = $result->numrows;
-]

<UL>

[$if (! $numresults ) $]
 <H2>No results matched</h2>
[$else$]
   [$while (%shark = $Msql->fetchhash($result)) $]
      <LI>
      <IMG SRC="graphics/shark[+$shark{'id'}+].gif" ALIGN=LEFT>
      <B>Species:</b> [+$shark{'species'}+]<br>
      <B>Age:</b> [+$age{$shark{'age'}}+]<br>
      <B>Location</b> [+$shark{'location'}+]<br>
   [$endwhile$]
[$endif]
</ul>
<A HREF="search.html">Search again</a>
</body></html>


Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.