mod_perl logo perl icon
no previous pagepage up: What is mod_perl?no next page

Content Handler Example






Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

By Lincoln Stein, Doug MacEachern
Embedding Perl in HTML with Mason

Embedding Perl in HTML with Mason

By Dave Rolsky, Ken Williams
mod_perl2 User's Guide

mod_perl2 User's Guide

By Stas Bekman, Jim Brandt
Practical mod_perl

Practical mod_perl

By Stas Bekman, Eric Cholet
The mod_perl Developer's Cookbook

The mod_perl Developer's Cookbook

By Geoffrey Young, Paul Lindner, Randy Kobes
mod_perl Pocket Reference

mod_perl Pocket Reference

By Andrew Ford


Table of Contents

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 accessible 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




TOP
no previous pagepage up: What is mod_perl?no next page