You are viewing a plain text version of this content. The canonical link for it is here.
Posted to docs-cvs@perl.apache.org by mo...@apache.org on 2002/04/24 06:12:18 UTC
cvs commit: modperl-docs/src/start/tips config.html handler.html logging.html
moseley 02/04/23 21:12:18
Added: src/images titlegrad.gif
src/start/tips config.html handler.html logging.html
Log:
Adding new files to CVS, so I can then tag.
Revision Changes Path
1.1 modperl-docs/src/images/titlegrad.gif
<<Binary file>>
1.1 modperl-docs/src/start/tips/config.html
Index: config.html
===================================================================
<html>
<head>
<title>Configure Apache with Perl Example</title>
<meta name="Description" content="Configuation Example">
</head>
<body bgcolor="white">
<h1>Configureing Apache using <Perl> sections</h1>
<p>
With mod_perl, Perl code can be embedded directly in the Apache configuration file.
Perl in httpd.conf is commonly used to dynamically configure Apache, but anything from
URL translation to content generation can be accomplished directly in the configuation file.
</p>
<p>
This example reads configuration settings from a text file and configures Apache's
virtual hosts.
<p>
The httpd.conf setup:
</p>
<pre>
NameVirutalHost 192.168.1.22
# Read in virtual host config from a tab-delimited file.
<Perl>
open(HOSTS,'/etc/apache/vhosts.txt')
or die "Failed to open vhosts.txt: $!";
while (<HOSTS>) {
my %config;
my @params = qw/ServerName DocumentRoot ErrorLog TransferLog ServerAdmin/;
@config{ @params } = split /\t/;
push @{$VirtualHost{'192.168.1.22'}}, \%config;
}
close HOSTS;
</Perl>
</pre>
<p>
See <a href="../../docs/1.0/guide/config.html#toc_Apache_Configuration_in_Perl">The Guide</a>
for other examples of configuring Apache with mod_perl.
</p>
<p>
<a class="more" href="../index.html#config">« back</a>
</p>
</body>
</html>
1.1 modperl-docs/src/start/tips/handler.html
Index: handler.html
===================================================================
<html>
<head>
<title>Content Handler Example</title>
<meta name="Description" content="Content Handler Example">
</head>
<body bgcolor="white">
<h1>Creating a content handler with mod_perl</h1>
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.
<pre>
package My::Greeting;
use strict;
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 Apache::Constants::OK;
}
1; # modules must return true
</pre>
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:
<pre>
<location /hello>
SetHandler perl-script
PerlHandler My::Greeting
</files>
</pre>
<p>
For a more in-depth explanation of creating mod_perl handlers see
<a href="../../docs/index.html">Documentation</a>.
The <a href="../../docs/1.0/guide/index.html">mod_perl Guide</a> is also recommended
reading.
<p>
<a class="more" href="../index.html#handler">« back</a>
</p>
</body>
</html>
1.1 modperl-docs/src/start/tips/logging.html
Index: logging.html
===================================================================
<html>
<head>
<title>Log Handler Example</title>
<meta name="Description" content="Log Handler Example">
</head>
<body bgcolor="white">
<h1>Creating a PerlLogHandler</h1>
Every request phase can be controlled using mod_perl. Here's an example
of a PerlLogHandler. The PerlLogHandler is one of the last phases of the request
cycle.
</p>
<p>
This example sends mail when a request is made to the /private section of your
web space. A more common use of a PerlLogHandler might be to track hits on a
specific set of URLs, or to write logging data to a relational database.
</p>
<pre>
package My::Notify;
use strict;
use Apache::Constants(':common');
use Mail::Send;
sub handler {
my $r = shift;
my $email = $r->server->server_admin || return DECLINED;
my $mail = Mail::Send->new(
To => $email,
Subject => "mod_perl Notification",
);
my $file = $r->filename;
my $fh = $mail->open;
$fh->print("File '$file' was accessed");
$fh->close;
return DECLINED; # let apache write to the lot
}
1; # modules must return true
</pre>
<p>
The httpd.conf setup:
<pre>
<location /private>
SetHandler perl-script
PerlLogHandler My::Notify
</location>
</pre>
<p>
<a class="more" href="../index.html#logging">« back</a>
</p>
</body>
</html>
---------------------------------------------------------------------
To unsubscribe, e-mail: docs-cvs-unsubscribe@perl.apache.org
For additional commands, e-mail: docs-cvs-help@perl.apache.org