Previous   -   Index   -   Next  >


Mailing List using CGI.pm


So far we have seen how...
Perl can produce the an HTML mailing list form using a multi-line print statement So what does the...
Mailing List Form look like when written using CGI.pm?

Things to note about   mailform2.pl:

Each of the three mailing list forms we have looked at pass their form information to the same Perl cgi script so it is about time that we had a look at...


maillist1.pl

#!/usr/local/bin/perl -w

# maillist1.pl - k.mcmanus@gre.ac.uk 20000915,20011017,20021109

# Mailing list example CGI program:
#     accepts data from a form
#     logs it to file for later processing by a separate batch program
#     returns confirmation of the data to the user 

use lib ('/home/mk05/perl/lib');
use MyTime qw(dtstamp);
use CGI qw(:standard);
use Fcntl qw(:flock);

# Save the data into mlist.txt

open(MLISTHANDLE, ">> ../comp1037/perl/mlist.txt");
flock(MLISTHANDLE, LOCK_EX);
print MLISTHANDLE dtstamp('compact'), ":", param('Email'), ":",
   param('Title'), " ", param('Initials'), " ", param('Surname'), ":";
foreach $val(param('Platforms')) { print MLISTHANDLE $val, " " }
print MLISTHANDLE "\n";
flock(MLISTHANDLE, LOCK_UN);
close(MLISTHANDLE);

# Return confirmation

print
   header(-type    =>'text/html',
          -expires => 'now',
          -charset => 'UTF-8'),
   start_html(-title => 'Results from mailing list program maillist1.pl',
              -style => {-src => '../comp1037/perl/mailform.css'}),
   h2('The following details about you are now held on our mailing list:'),
   "\n Title: ", param('Title'), br,
   "\n Initials: ", param('Initials'), br,
   "\n Surname: ", param('Surname'), br,
   "\n Email: ", param('Email'), br, br,
   "\n Platforms: ", br;
foreach $val (param('Platforms')) { print "       ", $val, br }
print
   hr, "\n", a({-href => "$ENV{HTTP_REFERER}"}, "Return to the previous page"),
   br, "\n", a({-href => "../comp1037/perl/mlist.txt"}, "Look at the mailing list"),
   hr, end_html;

Things to note about maillist1.pl:

Debugging code such as this is not helped by an error message in a browser reporting an internal server error but giving no details. It can be helpful to run the code in a shell, preferably a Unix shell. At the command prompt simply type the name of the script and provide the form data as a list of space delimited name=value arguments. You even get to see the HTTP header. CGI.pm recognises the Perl -debug pragma. See the documentation for other useful tricks.


Previous   -   Index   -   Next  >

best viewed using Mozilla browsers
© k.mcmanus 2004
Valid XHTML 1.0! . Valid CSS . WCAG priority 3 approved