Previous Perl And CGI |
CGI-Perl Tutorial Using Modules |
Next Life After Tutorial |
In the previous page, you saw how to create a Guestbook using perl. Now we will see how to do it using modules - in our case the CGI module. A module is just a set of related functions in a library file, i.e., a Perl package with the same name as the file. It is specifically designed to be reusable by other modules or programs. You can think of a module as the fundamental unit of reusable Perl code.
Some very useful modules are given below. Using this while programming will assure that you have a high quality end product.
strict | Restrict unsafe constructs. |
warnings | Control optional warnings. |
Using the modules are easy - just write 'use <ModuleName>;' at the top of the file. A tiny example...
#!/usr/local/bin/perl use strict; use warnings; my $subject = "World"; print "Hello, $subject";To see the list of all available modules, see the manual. But before using any of these modules, make sure you have them. If not get them from http://www.cpan.org/. The module we are most concerned about right now is the CGI module. CGI module phases the form results for you - so a lot of coding can be avoided. An example...
#!/usr/local/bin/perl use strict; use warnings; use CGI; my $cgi = new CGI; # Get input my $name = $cgi -> param('name'); my $email = $cgi -> param('email'); my $loc = $cgi -> param('loc'); my $comments= $cgi -> param('comments');
This will replace these lines in the GuestBook script given in the last page.
#!/usr/local/bin/perl my $query_string = ""; #Get the input if ($ENV{REQUEST_METHOD} eq 'POST') { read(STDIN, $query_string, $ENV{CONTENT_LENGTH}); } else { $query_string = $ENV{QUERY_STRING}; } # Split the name-value pairs @pairs = split(/&/, $query_string); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); # Making the input English. And removing unwanted things $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; }
CGI.pm makes it easy to create html files and parse form contents. One can use it to create HTML tags. I generally prefer to do this myself, but here is an example - the full GuestBook Program - using CGI.pm
#!/usr/local/bin/perl use CGI; my $cgi = new CGI; # Get input my $name = $cgi -> param('name'); my $email = $cgi -> param('email'); my $loc = $cgi -> param('loc'); my $comments= $cgi -> param('comments'); #Give output - create HTML using CGI module. print $cgi -> header, $cgi -> start_html('GuestBook Result'), $cgi -> h1("Guest book Results"); print "<p>Dear $name,<br> Thank You for filling out our Guest Book. I appreciate this effort in your part.</p>"; print $cgi -> table( $cgi -> Tr( $cgi -> td(['Name',"$name"]) ), $cgi -> Tr( $cgi -> td(['E-Mail', "<a href=\"mailto:$email\">$email</a>"]) ), $cgi -> Tr( $cgi -> td(['Location',"$loc"]) ), $cgi -> Tr( $cgi -> td(['Comments',"$comments"]) ) ); print $cgi -> end_html; # Open Guest Book File open (FILE, ">>guests.txt") || die "Can't open guests.txt: $!\n"; #Write the information to the file print FILE "$name came from $loc. "; print FILE "E-mail address is $email. "; print FILE "Comments : $comments\n"; close(FILE);
A better(and more complete) guestbook script using CGI module that I wrote can be found here. See manual to know more about what you can do with CGI module.
Previous Perl And CGI |
Contents | Next Life After Tutorial |