Show Contents Previous Page Next Page Chapter 2 - A First Module Now that you have First you'll need to create a location for your Apache Perl modules to live.
If you haven't done so already, create a directory in some convenient place.
We suggest creating a You'll now have to tell Apache where to look for these modules. PerlSetEnv PERL5LIB /my/lib/perl:/other/lib/perl Unfortunately this adds a little overhead to each request. Instead, we recommend
creating a Perl startup file that runs the use lib statement. You
can configure #!/usr/local/bin/perl # modify the include path before we do anything else BEGIN { use Apache (); use lib Apache->server_root_relative('lib/perl'); } # commonly used modules use Apache::Registry (); use Apache::Constants(); use CGI qw(-compile :all); use CGI::Carp (); # put any other common modules here # use Apache::DBI (); # use LWP (); # use DB_File (); 1; This example startup file first modifies the include path to point to the
location of the Apache Perl module directory. It uses the Apache::server_root_relative()
method to turn the relative path into an absolute path that use lib
will honor. It then loads up some commonly used libraries, including If most of your modules are going to use these libraries, loading them once at start-up time makes sense and assures the absolute fastest performance of your modules. Loading less-frequently used libraries should be deferred to the time you actually need them. Save the startup file to some logical place. We recommend We'll need to tell Apache to run the startup file at launch time. Open PerlRequire conf/startup.pl PerlFreshRestart On The first directive tells Apache to load and run the startup script when it is first launched. Like other file paths in Apache's configuration files, partial paths are treated as relative to the server root. The second directive tells the server to repeat this process every time it is restarted. This allows changes to the startup script (and other Apache Perl modules) to take effect without bringing the server completely down. You should now start or restart the server. On Unix platforms, the easiest
way to do this is to use the Watch the server Now that everything's configured properly, you can write a module using the
Apache Perl API. Example 2-1 gives a basic one named
Example 2-1. A First Apache Perl Module package Apache::Hello; # File: Apache/Hello.pm use strict; use Apache::Constants qw(:common); sub handler { my $r = shift; $r->content_type('text/html'); $r->send_http_header; my $host = $r->get_remote_host; $r->print(<<END); <HTML> <HEAD> <TITLE>Hello There</TITLE> </HEAD> <BODY> <H1>Hello $host</H1> Who would take this book seriously if the first example didn't We'll go into the details in later chapters, but essentially, this module
contains the definition for a single subroutine named handler(). When
the time comes, Apache will invoke handler() to handle the request,
passing it an Apache request object stored in the variable Using methods provided by the request object, our module first sets the MIME
content type of the outgoing data to text/html and then sends the HTTP
headers by calling send_http_header(). It retrieves the DNS name of
the remote host by making another call to the request object and incorporates
this value into a short HTML page that it sends to the browser by calling the
request object's print() method. At the end of the subroutine, the
module returns a value of To install this module, save it as <Location /hello/world> SetHandler perl-script PerlHandler Apache::Hello </Location> The first directive, SetHandler perl-script, tells Apache to invoke
You will have to restart the server again in order to have the new <Location>
section take effect. Later we will discuss how to install new modules without
restarting the server. Fire up your favorite browser and fetch the URI Figure 2-1. Apache::Hello results
If you get a server error of some sort, don't despair. Look in the server
error log for helpful messages from the Perl interpreter. They may be bare messages,
or if you are loading Most commonly you'll see messages about syntax errors. Fix the errors, restart
the server, and try again. If you get messages about not being able to find
the These are the basic steps for creating and installing a module using the Apache Perl API. Later chapters will give you a more in-depth understanding of what's going on here and how you can take advantage of it to do wonderful stuff. Footnotes 6 In the context of incoming Apache requests, we use "URI" (Uniform Resource Identifier) rather than "URL" (Uniform Resource Locator) throughout this book. URI is the more general term, so it can refer to partial documents as well as to fully qualified URLs. The main reason, however, is that URI is used in the Apache online documentation and in the names of API function calls, and who are we to buck tradition? Show Contents Previous Page Next PageCopyright © 1999 by O'Reilly & Associates, Inc. |
HIVE: All information for read only. Please respect copyright! |