Article

Embedding Perl Into Web Pages

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

When the user enters a value and submits the data, the file lookup.html will be called. This file is given by:

<%perl>      
my %match;      
open (PHONE, "/usr/local/data/phone.txt") or      
   die "Cannot open phone.txt: $!";      
while (<PHONE>) {      
   my @a = split /#/, $_;      
   next unless $a[0] =~ /$name/;      
   $match{$a[0]} = $a[1];      
}      
close (PHONE);      
</%perl>      
<& print_it, match => \%match, name => $name &>      
<%ARGS>      
$name => undef      
</%ARGS>

In this file, we first open up the file and cycle through the entries, saving those entries that match the search criteria in the hash %match. Note that, as with any CGI script, some form of taint checking should be done on any user-supplied input – see perldoc perlsec for a discussion. After the results are obtained, the file is closed and print_it is called, which will print out the results (note that a reference to the %match hash and the original query term $name are passed to this file). The print_it file is as follows:

<%perl>      
my @names = sort keys %$match;      
my $num = @names;      
if ($num > 0) {      
   my $string = sprintf(      
      "<B>%d</B> match%s for &quot;<B>%s</B>&quot; %s found:",      
      $num, ($num > 1 ? 'es' : ''), $name,      
         ($num == 1 ? 'was' : 'were') );      
</%perl>      
   <% $string %>      
   <TABLE WIDTH="40%">      
   <TR><TD COLSPAN=2><HR></TD></TR>      
   <TR><TH ALIGN="LEFT">Name</TH>      
   <TH ALIGN="LEFT">Number<TH></TR> %  foreach (@names) {      
      <TR>      
      <TD ALIGN="LEFT"><% $_ %></TD>      
      <TD ALIGN="LEFT"><% $match->{$_} %></TD>      
      </TR>      
% }      
<TR><TD COLSPAN=2><HR></TD></TR>      
  </TABLE>      
% }      
% else {      
   Sorry  nothing matched &quot;<B><% $name %></B>&quot;.      
% }      
% my $url = url;      
   Try <A HREF="<% $url %>">another search</A>.      
<%ARGS>      
$match => undef      
$name => undef      
</%ARGS>

In this file we first construct an array @names from the passed %match hash reference, which contains the names of the successful matches. We also set a variable $num equal to the number of successful matches obtained. If there were any, we print out the results in a table. If there were no matches we report that as such. Finally, at the bottom of this page we provide a link back to the original script through the url function of the CGI.pm module.

Some screenshots of this application in action appear below. When the address http://localhost/mason/phone.html is first requested, the basic form is presented through which the user enters a query:

518basicform

Upon entering a query (say M) and submitting the form, the results are then presented:

518resultstable

If the user selects the another search link, the original query form is presented, but with the textfield filled in via the cookie, with the default value from the original query.

518formcookie

These examples just show the basic structure of Mason. For more complex examples, we can refer to the Mason web site at http://www.masonhq.com/ and also to the examples contained within the source distribution.

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

Sponsored Links