jeremy.marzhillstudios.com/content/Mod_Perl-20---A-Real-World-Guid-2006-4-22.md

11 lines
8.9 KiB
Markdown
Raw Permalink Normal View History

2020-04-12 10:56:18 -04:00
+++
title = "Mod_Perl 2.0 - A Real World Guide - part I"
2020-04-12 15:59:04 -04:00
date = 2006-04-22T20:32:48Z
2020-04-12 10:56:18 -04:00
in_search_index = true
[taxonomies]
tags = [
"Site-News",
]
+++
Right now there is a shortage of really easy to understand documentation on using mod_perl to write web applications. There are a lot of examples on using it to rewrite URI's, redirect output, run CGI apps unaltered and even turning Apache into an email over http protocol server. Those are all really wonderful uses, but I want to build a website. So how do you go about doing that? Especially if you aren't using CGI.pm on the backend. This article and others after will focus on that topic. The tutorial is for mod_perl 2 on Apache2. It works equally well on windows or Linux as far as I can tell. I shall be assuming you already have or can find out how to get mod_perl 2 and apache2 on your server. Basically you will be following me as I peek into the internals of making mod_perl useful. Lets start with a simple script to take a look at the internals of what mod_perl lets you do. Here is our script: <blockquote><pre><code> package mod_perl_report; use strict; use warnings; use Apache2::RequestRec (); use Apache2::RequestIO (); use Apache2::Const -compile => qw(OK); sub handler { my $r = shift; my $report; $r->content_type('text/plain'); $report .= "server: ".$r->server()."\\n\\n"; $report .= "hostname: ".$r->hostname()."\\n"; $report .= "user: ".$r->user()."\\n"; $report .= "unparsed uri: ".$r->unparsed_uri()."\\n"; $report .= "uri: ".$r->uri()."\\n"; $report .= "filename: ".$r->filename()."\\n"; $report .= "pathinfo: ".$r->path_info()."\\n"; $report .= "request time: ".$r->request_time()."\\n"; $report .= "request method: ".$r->method()."\\n"; $report .= "request string: ".$r->args()."\\n\\n"; $report .= "cookies: ".$r->headers_in->{Cookie}."\n"; $report .= "status: ".$r->status()."\\n"; $report .= "status line: ".$r->status_line()."\\n"; $report .= "notes: ".$r->notes()."\\n"; $report .= "\n\npost data: ->|".read_post($r)."|< -"; # $report .= "\n\nmodifying variables now: \\n\\n"; print "mod_perl 2.0 Debugging output:\\n\\n"; print $report; return Apache2::Const::OK; } sub read_post { my $r = shift; my $buffer; my $data; while ($r->read($buffer, 1000)) { $data .= $buffer; } return $data; } 1; </code></pre></blockquote> You will need to save it into a file called mod_perl_report.pm and then tell mod_perl where it is. To do that you will need some configuration directives in your apache2 httpd.conf file. Now in my case I created a folder in my apache server's root directory (whatever you set ServerRoot to in the conf file) called mod_perl. Then I created a file called mod_perl_prep.pl in the apache configuration directory with my common use statements. In this file I also had the line <pre><code>use lib qw(mod_perl);</code></pre> so that mod_perl will know where my handler is. I call the mod_perl_prep.pl script from the apache configuration file like so: <blockquote><pre><code>PerlRequire conf/mod_perl_prep.pl</code></pre></blockquote> and finally since this is a development machine I save myself some time by adding this line directly underneath: <blockquote><pre><code> # comment out the following line for production use. PerlInitHandler Apache2::Reload </code></pre></blockquote> That line tells mod_perl to reload any modules I use when they change so I don't have to keep restarting apache to see my changes. Believe me you will want this on your development machine cause the restarting gets really old really fast. Now we are ready to tell Apache when to call our debugging report handler. At the bottom of your configuration file add the following section: <blockquote><pre><code> <location /report > SetHandler perl-script PerlResponseHandler mod_perl_report </location> </code></pre></blockquote> Now Apache knows that when it sees the /report URI after the hostname it needs to call my mod_perl_report handler. So lets take a look at that handler right now and see what it does. This module illustrates all the most useful pieces of the Apache2::RequestRec object that I have so far been able to figure out. It starts out with our modules package declarations and any use statements we will need to do the work we are planning to do.