- The system has mod_perl2 installed and already configured in the httpd.conf file.
- You do not have write access to httpd.conf, and might not even be able to convine the admins to make changes for you.
- You do not have write access to the main perl or cgi-perl directory configured as the httpd server's directory for mod_perl scripts.
- The system is configured to give you the ability to use an .htaccess file in your web document directory (i.e., ~/public_html) to change your own configuration.
This has been tested with mod_perl 1.99_09. YMMV.
First, check the httpd.conf setup. The file is often in a directory like /usr/local/apache/conf. It should contain something like this:
Then you need three more files:LoadModule perl_module modules/mod_perl.so # ... <IfModule mod_perl.c> <Location /perl> #AllowOverride None SetHandler perl-script PerlHandler ModPerl::Registry Options ExecCGI allow from all PerlSendHeader On </Location> # the next part is not required for this recipe to work <Location /cgi-perl> #AllowOverride None SetHandler perl-script PerlHandler ModPerl::PerlRun Options ExecCGI allow from all PerlOptions +ParseHeaders </Location> </IfModule>
1. ~/public_html/.htaccess, containing these lines:
2. ~/public_html/TestModPerl.pm, containing this:<FilesMatch "modperl-test.pl"> SetHandler perl-script PerlResponseHandler ModPerl::Registry Options +ExecCGI PerlSendHeader on </FilesMatch>
(The following example is adapted from the example in Stas Bekman's tutorial "Getting Started With mod_perl".)
3. ~/public_html/modperl-test.pl, containing this:package TestModPerl; my $total = 0; my $counter = 0; sub run { $counter = 0; for (1 .. 5) { increment_counter(); } } sub increment_counter { $counter++; print "Counter is equal to $counter!<br>\n"; } sub total { $total++; print "This has run $total times.<br>\n"; } 1; __END__ # apparently in mod_perl2 having __END__ is OK.
Now access the URL for your script to see the result:#!/usr/bin/perl BEGIN { push @INC, "/home/YOURACCOUNTNAMEHERE/public_html"; } use strict; use TestModPerl; print "Content-type: text/html\n\n"; TestModPerl::run(); TestModPerl::total();
http://www.yourdomain.org/modperl-test.pl
If things are working as intended, you will see the count go from one to five with each invocation (as you hit refresh) and you will see the total climb incrementally across invocations. So after three invocations it will look like this:
Counter is equal to 1!
Counter is equal to 2!
Counter is equal to 3!
Counter is equal to 4!
Counter is equal to 5!
This has run 3 times.
|
---|