mod_perl

http://www.coe.tamu.edu/cs/apache/mod_perl/all.html
http://perl.apache.org/docs/1.0/guide/debug.html
http://www.perlmonks.org/?node_id=412121
Apache::Session
EmbperlObject
Benchmarking

What are best practices specific to mod_perl?

Research mod_perl and multi-thread programming.

Research mod_perl and POE. Is it possible?

Include conf/mod_perl.conf

<Perl>
    name of the perl script to be executed at server startup
</Perl>

PerlRequire perlscript.pl
PerlModule perlscript.pl

Alias /foo /home/httpd/foo
ScriptAlias /cgi-bin /home/httpd/cgi-bin

Alias /cgi-bin /home/httpd/cgi-bin
SetHandler cgi-handler

<Location /perl>
    SetHandler perl-script
    PerlHandler Apache::Registry
    Options ExecCGI
    Allow from all
    PerlSendHead On
</Location>

PerlModule Apache::DBI CGI DBD::mysql
PerlRequire /home/httpd/perl/lib/startup.pl
<Directory directory>
    Options ExecCGI
    AddHandler cgi-script cgi pl
</Directory>

Alias /foo /home/httpd/foo
ScriptAlias /cgi-bin /home/httpd/cgi-bin

Alias /cgi-bin /home/httpd/cgi-bin
SetHandler cgi-handler
Alias /perl /home/httpd/perl
PerlModule Apache::Registry
PerlRequire /home/httpd/startup.pl
<Location /perl>
    SetHandler perl-script
    PerlHandler Apache::Registry
    Options ExecCGI On
    Allow from all
</Location>

The above configuration cause each URI starting with /perl to be handled by Apache::Registry module

Do we have to convert all CGI programs into modules in order to take advantage of pre-loading?

Traditional CGI programs should be converted to modules to take advantage of pre-loading (probably allow all child processes to share memory and save compilation time). However, ordinary CGI script can be pre-loaded as well using the Apache::RegistryLoader module inside the startup.pl file:

use Apache::RegistryLoader();
Apache::RegistryLoader->new->handler(
    "/perl/test.pl",
    "/home/httpd/perl/test.pl"
);

For sites that had converted all CGI script to mod_perl (using Apache::args() rather than $ENV{'QUERY_STRING'}) should set PerlSetupEnv Off.

Scripts that are still using CGI.pm need to have PerlSetupEnv On.

If your site does not serve CGI script (all have been converted to modules), the PerlTransHandler should be:

PerlTransHandler Apache::OK    # probably should be inside the <Perl> section.

This is to reduce the number of stat() call.

Why should we use compiled regular expression?

Under traditional CGI, compiled regular expression optimized with /o modifier is a big win inside loops. Under mod_perl, the regular expression portion may change with each invocation. To take advantage of compiled regular expression, we need to wrap the entire code section which involves the regular expression in an eval block. Traditional CGI:

$regex = $q->param('someparam');     // the value of someparam may change between invocations.
while (somecondition) {            // $regex change between invocations but not within the loop
    if ($_ =~ /$regex/o) { ... } 
}

Under mod_perl, we need to wrap the above code inside an eval block:

$regex = $q->param('someparam');
eval {
    while (somecondition) {
        if ($_ =~ /$regex/o) { ... }
    }
}

If the code section contains only one operator (be it m// or s///), the eval can be eliminated using property of the null pattern:

$pat = $q->param('someparam');
"\377" =~ /$pat|^\377$/;     # guaranteed if meta-chararacters present, must be successful.
                    # \377 is a non-searchable character
while (somecondition) {
    if (//) { ... }
}

The null pattern default to the last pattern seen.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License