Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

mod_perl question

by PyrexKidd (Monk)
on Apr 11, 2011 at 21:31 UTC ( [id://898787]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,

I am attempting to convert a program from a CGI application to a mod_perl application. To my understanding mod_perl will run CGI scripts however, running CGI only experience minor increase in performance.

My question is this: how do I write a multi-screen mod_perl program?

I haven't found a direct solution for the multi-screen mod_perl application; searching for a solution returns CGI solutions. Perhaps I am misunderstanding the use of handlers, essentially I don't want to have to create an entry in the httpd.conf file for every page on my website.

this is how I do it in my CGI script:

my $q = new CGI; my %States = ( 'Default' => \&index, 'Index' => \&index, 'Save' => \&save, 'Etc' => \&etc, ); # Query for the value of ".State" as passed by the POST # Method my $current_screen = $q->param(".State") || "Default"; die "No Screen for $current_screen" unless $States{$current_screen}; # iterate through values in %States for active page while (my ($screen_name, $function) = each %States){ $function->($screen_name eq $current_screen); } #generate pages based on sub called sub index { my $active = shift; return unless $active; do_stuff(); }

Then in my HTML template I use a submit button to change the state of the page, i.e.:

<input type="submit" name=".State" value="Index" /> <input type="submit" name=".State" value="Save" /> <input type="submit" name=".State" value="Etc" />

perhaps I am misunderstanding the use of handlers, or the configuration of mod_perl. What I would like to avoid is an entry in httpd.conf for every page (and this is where I think I might be misunderstanding something.).

I am under the impression that to create multiple pages I'll do the following:

PerlRequire /var/www/Index.pm <Location / > SetHandler perl-script PerlHandler Index PerlSendHeader On Allow from all </Location> PerlRequire /var/www/Save.pm <Location /Save> SetHandler perl-script PerlHandler save PerlSendHeader On Allow from all </Location> PerlRequire /var/www/Etc.pm <Location /Etc> SetHandler perl-script PerlHandler Etc PerlSendHeader On Allow from all </Location>

If the above would be used for a website with multiple pages, but then how would I create a multi-screen application?

Am I way off? I think I might be close but I'm not sure.

Thanks for the help

Replies are listed 'Best First'.
Re: mod_perl question
by davido (Cardinal) on Apr 11, 2011 at 22:36 UTC

    Getting up to speed on mod_perl with respect to writing Apache handlers is a non-trivial pursuit. While the rewards are often significant, you do need a pretty thorough understanding. I recommend the O'Reilly book, "Practical mod_perl." It's a fantastic resource, and will teach more than you ever thought you wanted to know about the subject.

    If you don't want to get into it that deeply, use Apache::Registry. You still may get a strong performance boost over mod_cgi (plain old CGI), without having to know quite as much. It's one of those areas where you can't skip to the last chapter and hope to reap the benefits while at the same time producing bug-free code. But Apache::Registry gives you many of the benefits of mod_perl handlers, without quite as much of a learning curve. You still need to write pristine code, but you don't need to know as many details. The down side is slightly less performance, but that may be ok in your application.


    Dave

Re: mod_perl question
by grantm (Parson) on Apr 13, 2011 at 09:18 UTC

    I would strongly suggest that you look at using Plack or more specifically, coding to the PSGI API rather than writing to the mod_perl API. The advantage is that if you write to the PSGI API, you can deploy to CGI, FastCGI, mod_perl, standalone server etc - without changing your code. It sounds awesome, because it is :-)

Re: mod_perl question
by sundialsvc4 (Abbot) on Apr 12, 2011 at 00:21 UTC

    Vigorous nods!

    It might be interesting to note that I worked with a very large bookstore chain that to this day (as far as I know...) still uses “plain ol’ CGI” to do everything.   And it works just fine.

    I suggest also that you should thoroughly consider your options.   I am not a huge fan of mod_perl, preferring the “FastCGI” approach (and other distributed-processing techniques) for a variety of reasons.   (However, it is not my purpose to start any sort of religious war on that point.)   At this juncture, consider carefully why your app is not giving you the performance that you require, in its present configuration.   Seriously consider the now-viable option of, “throw silicon at it.”   Conversion of a web app to a fundamentally different form of running, as you now contemplate, is no trivial undertaking and has no guarantee of stable success.

Re: mod_perl question
by Anonymous Monk on Apr 12, 2011 at 10:59 UTC
    My question is this: how do I write a multi-screen mod_perl program?

    The same way you should have been writing it from the start ;D proper scoping and careful usage of globals ;)

    See Practical mod_perl: 2.6.1. Porting Existing CGI Scripts to mod_perl

    See also CGI to mod_perl Porting. mod_perl Coding guidelines

    perhaps I am misunderstanding the use of handlers, or the configuration of mod_perl. What I would like to avoid is an entry in httpd.conf for every page (and this is where I think I might be misunderstanding something.).

    Yes. Before mod_perl, I might start a program with a nice template (commentary on this template) like so:

    #!/usr/bin/perl -- use strict; use warnings; use CGI; Main(@ARGV); exit(0); sub Main { ... # CGI program here }
    After mod_perl all I need for a handler is to rename MyProgram.pl to MyProgram.pm, and rename Main to handler
    package MyProgram; sub handler { CGI::initialize_globals(); # unneeded if using CGI->new ... # CGI program here }
    Now, I might also use CGI::Application or Catalyst, so sub handler would consist of
    MyApp->new->run;
    or
    Catalyst::ScriptRunner->run('MyApp', 'Apache');
Re: mod_perl question
by scorpio17 (Canon) on Apr 12, 2011 at 15:44 UTC

    You do NOT need to create an entry in http.conf for each page. I recommend you use CGI::application, then follow the tips here to get it working under mod_perl, once you have everything running under standard CGI.

Log In?
Username:
Password:

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

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

    No recent polls found