Article

Embedding Perl Into Web Pages

Page: 1 2 3 4 5 6 7 8 9 10 11 Next

A Longer Example

As a more involved example of using HTML::Mason, we illustrate the use of a telephone book lookup form. In this example, the data is assumed to be in a file /usr/local/data/phone.txt, with one entry per line containing the name and phone number of a person, separated by the # symbol. This is an example of what it should look like:

Andrew Logan#377-0971

Matt Jones#598-6788

We split this application across four files – a master file (called phone.html), a form through which a user enters a query (called form), a lookup script that gathers the data from the database (called lookup.html), and finally a script to print out the results (called print_it). We will also have a header file, header, and a footer file, footer, called though the autohandler template file. Also in this example, we illustrate the use of cookies in saving session data across invocations of the script. All files are to be placed in the directory specified by the /mason location in httpd.conf.

Using the same Apache configuration as before, we first give the autohandler template file:

% my $title = 'My HTML::Mason demo';    
<& header, title => $title &>      
<H3>Phone book example</H3>      
<% $m->call_next %>      
<& footer &>

The header file, called with a parameter title through the syntax <& header, title => $title &>, is:

<HTML>      
<HEAD><TITLE><% $title %></ TITLE ></HEAD>      
<body bgcolor="#ffffff" link="#ff5599" vlink="#993399">      
<BR>      
<%ARGS>      
$title => undef      
</%ARGS>

And the footer file is:

<P>      
Comments to      
<A HREF="mailto:me@my.address.com">me</A>      
are welcome.      
</BODY>      
</HTML>

Note the use of the <%ARGS>...</%ARGS> construction, which is used to define arguments to be passed into the file. The master file by which the script is called, phone.html, contains:

% my $name = param('name');      
% if (! $name) {    
   <& form &>      
% }      
% else {    
   % $r->header_out('Set-Cookie', "name = $name");      
<& lookup.html, name => $name &>      
% }

We begin here by checking, via the param method of the CGI.pm module (pulled in through handler.pl), if a value has been entered for the name parameter. If it hasn't, we print out the form contained in the file form. If a value has been entered, we set a cookie via the mod_perl-specific call $r->header_out('Set-Cookie', "name = $name") to save this value across sessions, and then call lookup, which will query the database and print out the results. Note that we pass to this file the variable $name.

The file form used to print out the form by which the user enters the query contains:

% my $val = cookie(-name => 'name');    
<FORM>      
<TABLE><TR>      
<TD>Please enter the name:</TD>      
<TD>      
<INPUT TYPE="text" NAME="name" SIZE=30 VALUE="<% $val %>">      
</TD>      
</TR><TR>      
<TD COLSPAN=2>      
<INPUT TYPE="submit" VALUE="Search!"> </TD></TR></TABLE>      
</FORM>

This uses the cookie method of the CGI.pm module to retrieve the value of the cookie associated with the name parameter, if it is present. This value is used as the default for the textfield box in which the user enters the query.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links