Perl
  Home arrow Perl arrow Page 5 - Introduction to mod_perl (part 2): mod_perl Quickstart
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Introduction to mod_perl (part 2): mod_perl Quickstart
By: Stas Bekman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2002-05-07


    Table of Contents:
  • Introduction to mod_perl (part 2): mod_perl Quickstart
  • Installing mod_perl Detailed
  • Configuring and Starting mod_perl Server
  • The "mod_perl rules" Apache::Registry Script
  • The "mod_perl rules" Apache Perl Module
  • Is This All I Need to Know About mod_perl?
  • Acknowledgements
  • References

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introduction to mod_perl (part 2): mod_perl Quickstart - The "mod_perl rules" Apache Perl Module
    ( Page 5 of 8 )

    mod_perl is about running both scripts and handlers. Although I have started to present mod_perl using scripts because it's easier if you have written CGI scripts before, the more advanced use of mod_perl is about writing handlers. But have no fear. As you will see in a moment, writing handlers is almost as easy as writing scripts.

    To create a mod_perl handler module, all I have to do is to wrap the code I have used for the script into a handler subroutine, add a statement to return the status to the server when the subroutine has successfully completed, and append a package declaration at the top of the code.

    Just as with scripts you can use either the CGI API you are probably used to (save the following as Rules1.pm):

    package ModPerl::Rules1; use Apache::Constants qw(:common); sub handler{ print "Content-type: text/plain\r\n\r\n"; print "mod_perl rules!\n"; return OK; } 1; # satisfy require()
    or the Apache Perl API that allows you to interact more intimately with the Apache core by providing an API unavailable under regular Perl. Of course in the simple example that I show, using any of the approaches is fine, but when you need to use the API, this version of the code should be used (save as Rules2.pm):

    package ModPerl::Rules2; use Apache::Constants qw(:common); sub handler{ my $r = shift; $r->send_http_header('text/plain'); print "mod_perl rules!\n"; return OK; } 1; # satisfy require()
    Create a directory called ModPerl under one of the directories in @INC (e.g. /usr/lib/perl5/site_perl/5.005), and put Rules1.pm Rules2.pm into it, the files should include the code from the above examples.

    To find out what the @INC directories are, execute:

    % perl -le 'print join "\n", @INC'
    On my machine it reports:

    /usr/lib/perl5/5.6.1/i386-linux /usr/lib/perl5/5.6.1 /usr/lib/perl5/site_perl/5.6.1/i386-linux /usr/lib/perl5/site_perl/5.6.1 /usr/lib/perl5/site_perl .
    Now add the following snippet to httpd.conf to configure mod_perl to execute the ModPerl::Rules::handler subroutine whenever a request to mod_perl_rules1 is made:

    PerlModule ModPerl::Rules1 <Location /mod_perl_rules1> SetHandler perl-script PerlHandler ModPerl::Rules1 </Location>
    Now you can issue a request to:

    http://localhost/mod_perl_rules1

    and just as with our mod_perl_rules.pl scripts you will see:

    mod_perl rules!
    as the response.

    To test the second module ModPerl::Rules2 add the sameconfiguration, while replacing all 1's with 2's:
    PerlModule ModPerl::Rules2<Location /mod_perl_rules2>  
    SetHandler perl-script  
    PerlHandler 
    ModPerl::Rules2</Location>[code]


    And to test use the URI:

    http://localhost/mod_perl_rules2

     
     
    >>> More Perl Articles          >>> More By Stas Bekman
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek