NAME

Content Handler Example

Creating a content handler with mod_perl 1.0

Handlers are simply perl subroutines called by the server at various stages of the HTTP request cycle. A content handler is a subroutine that is called by the response phase. Handlers, are typically created as a perl modules, separate files store in a library, and accessable via perl's @INC array.

For example, here's an example that returns a greeting and the current local time.

file:My/Greeting.pm
-------------------
package My::Greeting;
use strict;

use Apache::Constants qw(OK);

sub handler {
    my $r = shift;
    my $now = scalar localtime;
    my $server_name = $r->server->server_hostname;

    $r->send_http_header('text/plain');

    print <<EOT;
Thanks for visiting $server_name.
The local time is $now.
EOT

    return OK;
}
1; # modules must return true

Save the above as a file file in your perl library (e.g. My/Greeting.pm). Now, to return the above greeting when the URL /hello is visited on your server:

<Location /hello>
    SetHandler perl-script
    PerlHandler My::Greeting
</Location>

For a more in-depth explanation of creating mod_perl handlers, and mod_perl in general, see the mod_perl Guide.

« back