Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

mod_perl and cgi detente

by derby (Abbot)
on Mar 26, 2007 at 19:17 UTC ( [id://606640]=perlquestion: print w/replies, xml ) Need Help??

derby has asked for the wisdom of the Perl Monks concerning the following question:

In a nutshell, I have a collection of web applications that are either vanilla cgi or mod_perl. Ideally, I would like to move towards a pure mod_perl implementation but that's simply not practical.

I've handled this in the past by setting up the core modules and then deriving (or wrapping) into ::Apache and ::Cgi modules. I'd like to get rid of that namespace distinction and use just the core modules (with the crusty bits hidden behind $ENV{MOD_PERL} checks).

Doing a search for some examples of this approach is a bit dodgy -- if anyone can provide links to cgi and mod_perl living together, I would appreciate it.

-derby

Replies are listed 'Best First'.
Re: mod_perl and cgi detente
by perrin (Chancellor) on Mar 26, 2007 at 20:36 UTC
    Is there any reason you aren't looking to wrap your old CGI apps in ModPerl::PerlRun or ModPerl::Registry? That's usually what people do when they move them over.

    Aside from that, you'll have to tell us what isn't porting cleanly from your CGIs in order to get advice. I saw that you mentioned CGI.pm vs. Apache::Request for parsing params. They are intended to offer essentially the same API. If you want them to be exactly the same, just CGI.pm in all cases. It works fine under mod_perl and the performance difference is not significant for most sites.

Re: mod_perl and cgi detente
by Joost (Canon) on Mar 26, 2007 at 19:34 UTC
    Are there specific issues you're dealing with? If you build your app on CGI.pm (or CGI::Application etc), use DBI with Apache::DBI or some other persistance-checking technique and load your modules as early as possible you've probably got most of the performance enhancements for free without doing anything apache specific - ie. your code should work both as CGI and as Apache::Registry modules.

      I'm looking into building a Sessions module that can sit over a namespace (mod_perl) or be called from a cgi script (before or after the CGI creation) -- So I'm looking to do a Foo::Session instead of Foo::Session::Apache and Foo::Session::Cgi. Now if the session was simple, it would be straightforward (get cookie, set cookie); however, sometimes I need to store GET params in the session and I may need to redirect if the session is invalid. So sometimes I have an Apache request object to deal with sometimes, a CGI object.

      -derby
        Are you sure you will need to go through all that trouble? CGI is aware of mod_perl, and will use the appropriate Apache modules under the hood. You can even access the request object through $cgi->r.

        I would just write the code as if it's going to run as plain CGI, and let CGI.pm and mod_perl handle their environment for me.

Re: mod_perl and cgi detente
by Khen1950fx (Canon) on Mar 26, 2007 at 19:55 UTC
Re: mod_perl and cgi detente
by randyk (Parson) on Mar 26, 2007 at 20:41 UTC

    I'm working on a module - CGI::Apache2::Wrapper - which may help in this regard. The aim here is to provide a (mostly) CGI.pm-compatible interface to certain mod_perl2 and libapreq2 methods. Currently the main one present is param(), but work is being done on others like cookie() and upload(). One of the main purposes is to assist in the porting of apps from cgi to mod_perl, so that one can say in a handler

    my $cgi; if ($I_want_to_use_mod_perl_modules) { $cgi = CGI::Apache2::Wrapper->new($r); } else { $cgi = CGI->new($r); }
    and then call methods like param(), etc. on the $cgi object, with the work of loading and calling the appropriate mod_perl2/libapreq2 modules and methods done behind the scenes.

    Let me know if there's something along these lines that would be useful.

Re: mod_perl and cgi detente
by philcrow (Priest) on Mar 26, 2007 at 20:26 UTC
    You might consider a framework like Catalyst or Gantry which factor out the server engine. But they might require more re-writing than you want to do.

    Phil

Re: mod_perl and cgi detente
by cbrandtbuffalo (Deacon) on Mar 26, 2007 at 20:13 UTC
    Are you working with Apache/mod_perl 1 or 2?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://606640]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 22:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found