Begin
from Larry Wall, in Learning Perl, 2nd Edition... What you'll need most is courage. It is not an easy path that you've set your foot upon. You're learning a new language: a language full of strange runes and ancient chants, some easy and some difficult, many of which sound familiar, and some of which don't. You may be tempted to become discouraged and quit. But think you upon this: consider how long it took you to learn your own native tongue. Was it worth it? I think so. And have you finished learning it? I think not. Then do not expect to learn all the mysteries of Perl in a moment, as though you were consuming a mere peanut, or an olive. Rather, think of it as though you were consuming, say, a banana. Consider how this works. You do not wait to enjoy the banana until after you have eaten the whole thing. No, of course not. You enjoy each bite as you take it. And each bite motivates you to take the next bite, and the next.
Find An Already Installed perl
If you are working on a Unix-like operating system, chances are very high that you already have perl installed. If you're not sure where it is or what version you have, you can simply type the following two commands respectively at your terminal command prompt.
$ which perl $ perl -v
Install perl From Source
If you have access to a capable C compiler, and sufficient permissions, you can install the latest version of perl (5.8.8 in this example) by following these steps. If wget is not on your system try the curl -O command, lwp-download or your favorite tool for downloading files.
$ wget http://www.perl.com/CPAN/src/stable.tar.gz $ tar -xvzf stable.tar.gz $ cd perl-5.8.8 $ perldoc INSTALL
Then follow the directions in the INSTALL file, it contains a wealth of information. For example a default install would look like this...
$ rm -f config.sh Policy.sh $ ./Configure -de $ make $ make test $ sudo make install
Install mod_perl Under Apache 2
If you already have an apache 2.2 (or later) web server and perl 5.8.8 (or later) installed, you may be able to add mod_perl support without recompiling apache2. The first step is to download and extract the mod_perl source code.
$ wget http://perl.apache.org/dist/mod_perl-2.0-current.tar.gz $ tar -xvzf mod_perl-2.0-current.tar.gz $ cd mod_perl-2.0.2
You'll want to note the location of your apache 2.2 installation root now. Mine happens to be under /opt/apache2.2/ but that's just me. You should replace the path I'm using with the path to your own Apache 2.2 installation root in all these examples.
As part of the configuration you must specify the full path to the apache installation root. Run the configuration script, and enter the correct information for your own system...
$ perl Makefile.PL MP_AP_PREFIX=/opt/apache2.2/
As noted in the output printed by the configuration script, the mod_perl apache module will eventually be installed in the modules directory, so add the following line to your apache conf/http.conf file...
LoadModule perl_module modules/mod_perl.so
Now compile and install mod_perl...
$ make && make test $ sudo make install
You can check that the mod_perl.so file was actually installed in apache's modules directory. If it was and you had no error messages: congratulations! You've now got mod_perl installed. You can restart apache now or continue on with the next step.
Write a mod_perl Handler under Apache 2
You'll need to create a directory to hold the mod_perl handlers you'll soon be writing. Pick any convenient location...
$ mkdir -p /opt/apache2.2/lib/perl
Every handler we write will need certain common mod_perl code; we'll put that in a separate library file to avoid having to write it in every handler module. Save the following as "startup.pl" in some convenient place like the apache conf directory.
use lib qw(/opt/apache2.2/lib/perl); # wherever you will put your handlers use ModPerl::Util (); use Apache2::RequestRec (); use Apache2::RequestIO (); use Apache2::RequestUtil (); use Apache2::ServerRec (); use Apache2::ServerUtil (); use Apache2::Connection (); use Apache2::Log (); use APR::Table (); use ModPerl::Registry (); use Apache2::Const -compile => ':common'; use APR::Const -compile => ':common'; 1;
Tell apache to run that startup script whenever the server starts by adding the following line to httpd.conf.
PerlRequire /opt/apache2.2/conf/startup.pl
Now let's create a test mod_perl handler to verify that everything works. Save this in /opt/apache2.2/lib/perl/MyApache2/HelloWorld.pm.
package MyApache2::HelloWorld;
use strict;
use warnings;
sub handler {
my $r = shift;
$r->content_type('text/plain');
print "Hello world!\n";
return Apache2::Const::OK;
}
1;
Finally tell apache to map that HelloWorld module to a URL location by appending the following to httpd.conf.
<Location /mod/hello> SetHandler perl-script PerlResponseHandler MyApache2::HelloWorld </Location>
Everything should be good to go now. Restart your apache and access the location for your Hello World handler...
$ sudo /opt/apache2.2/bin/apachectl restart $ lwp-request -e http://localhost/mod/hello
Learn More About mod_perl 2.0
The mod_perl 2.0 User's Guide, available via the Apache Software Foundation, is a surprisingly easy-to-read reference and definitely worth a bookmark.
Also highly recommended is the Practical mod_perl book, written by Eric Cholet and Stas Bekman (who develops mod_perl 2.0). The entire book is available online under a Creative Commons license.
Use Command Line Output as perl Input
The -n switch allows your script to accept numerous items from the shell's pipeline. Each will be placed in the $_ variable, one after another, and you can then do whatever you like with each one. One handy use of this is to combine Perl's text transforming power with the find command's search power.
$ find . -type f -name '*.pl' | perl -n -e'chomp; print "script: $_\n"'
Your perl script can saved to a file, rather than inline, and it will work the same.
chomp; print "script: $_\n";
If you had saved that in a file named "printscript", you would then run it like so...
$ find . -type f -name '*.pl' | perl -n printscript
Interestingly, you can turn this technique inside-out by quoting the find command within a perl script, using back ticks to execute it.
my @scripts = `find . -type f -name '*.pl'`;
foreach (@scripts) { chomp; print "script: $_\n" }
Let perl worry about the lines
When using the -n your script can accept lines of output from another command, but it's a pain to worry about chomping off the line endings and then adding them back on again when printing the results. Use the -l switch to let perl worry about all that for you. The following two snippets are equivalent.
$ find . -type f -name '*.pl' | perl -n -e'chomp; print "script: $_\n"'
$ find . -type f -name '*.pl' | perl -nl -e'print "script: $_"'
Swap the -n with the -p option and perl will take care of printing for you as well, that is it will automatically print whatever is in the $_ variable after each execution.
$ find . -type f -name '*.pl' | perl -pl -e'$_ = "script: $_"'
Use C Preprocessing Commands
If you run your script with the -P switch you can include C preprocessing commands in your script. Conveniently, because C preprocessing commands have the same syntax as Perl comments, you can safely disable those commands by simply removing the -P switch. So in the example below, if you run it as perl -P myscript.pl you will see the debugging message, but run it as just perl myscript.pl you won't.
#define DEBUG
my $x = "zoom";
my $log_level;
#ifdef DEBUG
$log_level = 3;
#else
$log_level = 0;
#endif
($log_level >= 3) and print "DEBUG: \$x is \"$x\"\n";
print "done\n";
Note that this behavior can also be recreated using Paul Marquess' Filter::cpp module.