Useful Perl packages for web site development
There are many excellent Perl packages on CPAN -- the Comprehensive Perl Archive Network. Some of them can be life saving. :)
Following we describe some modules that are very useful for
web site development. Some of them may be part of Perl distribution
already. To find if any of them is installed you can
try perl -MCool::Module in case you need to check
the package Cool::Module.
CPAN
Most of the time you can just use this module to manage all your Perl package installation. To invoke its shell interface, run
perl -MCPAN -eshell
Once you are in the CPAN shell, you can just type
install WWW::Mechanize
if you want to install the WWW::Mechanize package.
If the package you want to install also requires other packages, CPAN can follow the dependencies and install them automatically or ask you for confirmation depending on your configuration settings.
Because CPAN will not install a package unless all test cases included in that package pass, you may have to install the package manually if you know those test cases will fail, e.g., when you don't have a SQL server running while installing some database related packages.
It is also relatively simple to install Perl modules without using CPAN these days. Just download the tar ball from CPAN site, unpack it and then run
perl Makefile.PL
gmake
# testing is optional
gmake test
gmake install
Data::Dumper
This comes with perl core distribution. It can be handy to print a variable which may have complex structures. You can just use it like this:
use Data::Dumper;
print Dumper($any_var);
When you use the Perl debugger, you can also load the module at any time you want.
If you want to dump a hash or an array variable, you need to put a slash (\)
before it because Data::Dumper accepts references only.
WWW::Mechanize
This can be used to automate certain Web based tasks like testing web sites. As an example, you can use WWW::Mechanize to automate resume update on hotjobs.yahoo.com.
One thing to note is that this package depends on quite a lot of other packages and your hosting environment does not necessarily support it. However it is still quite useful as a general tool to aid web site development.
Crypt::SSLeay
This package is usually used together with
WWW::Mechanize
to sign in those web sites that use HTTPS/SSL so your user name and password
are not sent in the clear. Usually you don't need to call this module
directly.
HTML::Mason
This package is usually used together with mod_perl for developing dynamic web sites. According to Mason's official web site, Amazon.com also uses it to build their web site.
There are two ways to use this package. One is to use it to build dynamic web sites like what you can do with PHP. Mason provides higher level of abstraction over mod_perl because you can mix HTML text and presentation logic in one file. As to business logic, you are advised to use regular Perl modules to do the heavy lifting.
The other way is to use Mason as a template system to manage your documents. This is very helpful when many of your documents share some common elements like headers, footers and navigation elements. You just need to make changes in one place. Then you can regenerate all your documents from the templates through Mason and they all receive the same changes. In fact, almost all the pages on this web site are managed through Mason.
It may take some time to overcome the initial learning curve of Mason, however it is well worth the time and efforts. We will provide separate articles on web site development with Mason.
Class::DBI
Perl DBI is the standard database interface for Perl and is already quite easy to use. However, the Class::DBI package goes further and provides higher level of abstraction. In many cases you don't even need to write SQL statements. We will describe its usage in separate articles.
When you install Class::DBI with CPAN, it will also install Perl DBI automatically for you.
You also need to install individual drivers for the database management system (DBMS) you use. For example, for MySQL you need to install the DBD::mysql driver package.
Test::More
Test::More is part of the Test-Simple package. It can be handy when you develop your scripts and need to verify certain functions work as expected.
Most of the time you just need to include the following line in your script.
use Test::More qw(no_plan);
Then you can use the ok function to test those
functions. For example,
ok(any_function() == 1/i);
It is a good practice to write test cases as you develop your code so you will be more confident in making changes later because your test cases can back you up.
Mail::Send
Mail::Send is part of the MailTools package. With this package it is simple to write Perl script to send email from your web site.