up
Perl Lecture Notes - Feb 8
- Housekeeping
- Please check the online grade book for my comments on
your assignments.
- CGI.pm / MLDBM - AddressBook1.cgi - mostly review
- One more example of a "html within perl" cgi script.
This is a pretty typical single-file cgi script.
html code : CGI.pm and print().
data persistence: MLDBM (like DB_File but allows
extended data structures).
- One of last week's scripts had problems
(as yet not debugged). This example
works as advertised.
- File permissions on database here are 0664 = -rw-rw-r-- which
allows group read/write. I create the database by
running the script from the command line (user msie)
then change the group to www (chgrp www AddressData.db),
which both user=msie and user=httpd belong to.
- Note that the .db file is a binary - cannot be
transported across platforms.
- AddressBook1.cgi example
code - run it
- Templates
- Often you'd rather remove the html from within the perl code.
There are many, many modules to do this. Here are two.
- HTML::Template
- Described in detail in "CGI Programming with Perl"
- Very simple and clean
- Both a perl code file and an html "template" file
- Allows variable replacement, loops, and if
within xml-ish tags.
All template variables and
loop counters are hashes, name => value .
Loops are set with arrays of hash refs, i.e.
people=> [ {name=>'Joe', age=>4}, {name=>'Jim',age=>5}, ]
- used in template: < TMPL_VAR age > or
< TMPL_VAR NAME=>'age' >
- set in perl: $template->param( age => $value );
- AddressBook2.cgi example
run it> /
code> /
template
- HTML::Mason
- perl embedded within html
- must set up the mechanism to have
HTML::Mason process components -
this is typically done within httpd.conf / mod_perl
In this directory, I'm using a combination
of a .htaccess file and a mason_handler.cgi script
to do that. Not the fastest, but self contained
and easier to show without root privileges.
(On bob, all files ending in .mhtml are automatically
run through a mod_perl, persistent Mason.)
- each "component" can "call" others with
passed arguments, at the same time acting
like either embedded html or a perl subroutine.
Arguments passed can be treated as either
perl arguments or url?key=value parameters.
- Several special tags :
- % at start of line for one line of perl
- <% $variable %> to insert output of perl expression
- <%perl> ... </%perl> for longer blocks
- also <%init> and <%cleanup>
- <%args> ... </%args> block define arguments
- <& page.html, key=>$value &> to call other components
- AddressBook3.cgi example
(I've kept this similar to the other AddressBook applications, using only a few of Mason's bells and whistles.)
run it> /
code> /
htaccess> /
mason handler
- Others
- EmbPerl
- PHP
- Template Toolkit
- etc
- SQL Databases from Perl - the DBI and DBD::x modules
- what it is: Database Interface , independent of specific server/client database
- drivers: DBD::mysql, DBD::CSV, DBD::Oracle, ...
- Typical sequence :
- (1) open a connection and get a database handle
$dbase = "DBI:mysql:cgi_data";
$dbh = DBI->connect( $dbase, $user, $passwd, \%attributes );
- (2) prepare a SQL command and get a statement handle
$sth = $dbh->prepare( "SELECT ? FROM critters" );
- (3) execute the statement
$sth->execute( "animals" );
- (4) fetch results, typically as an array or array reference
@array = $sth->fetchrow_array();
- Or, use the $dbh->do() method , does (2), (3) in one command.
$dbh->do("DROP TABLE critters"); - execute a SQL query that doesn't return a result
Or even @row = $dbh->selectrow_array($sql_statement);, which does (2), (3), and (4) at once.
- "binding" variables:
The notion is that you can prepare a statement without specifying all the details,
and $sth->execute it several times, with different variations. Some databases can do this more efficiently,
since some of the preperation can be done ahead of time, once. Note also that variables so bound are
automatically quoted properly; otherwise, you may need to call $dbh->quote().
- Specifics vary with specific DBD driver and database you're using, but generally you can do whatever
SQL stuff is supported by that database - including transactions, commit, rollback, locking, etc.
- DBD::CSV - Using Comma Separated Value files as tables
- advantages: text files are easy to edit/maintain; no server to set up
- disadvantages: slow for large databases; limited functionality
- Putting 'em both together : AddressBook4
run it |
perl source |
html source |
database file
- DBD::mysql, DBD::Oracle, DBD::kitchen_sink,... - later.
(see full list of several dozen backends at Alligator's site, listed below)
- dbish - shell to issue DBI commands; similar interface to mysql interactive shell
- Documentation
- Assignment:
- Read chapter 6 in CGI Programming, "HTML Templates" and
chapter 10, "Data Persistence". These two server as an
introduction to both topics; however, for the details on
any specific perl module you'll really need the documentation
and examples for that module.
- Start looking at the various documentation for the
various template-like modules and DBI stuff. See the
reading links above.
- Write another perl cgi application that gets user
input and saves something to the disk, but this time
use (a) a different HTML interface, (i.e. HTML::Template,
Mason, etc.)
and (b) a different database interface (i.e. MLDBM or perl DBI)
Have the appropriate amount of fun.