Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

In CGI::Application cgiapp_prerun vs. cgiapp_init?

by bradcathey (Prior)
on Dec 12, 2005 at 15:50 UTC ( [id://516037]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monasterians,

Still new to CGI::Application but forcing myself to learn it and use it. Currently I have one question. What is the difference between cgiapp_prerun{} and cgiapp_init{}?

Their functions seem so similar. All I can glean from the documentation is that prerun may be the place to alter run modes and that init is the place to grab C::A params. But then there is mention of setting variables. Anyway, here's what I have:

use base 'CGI::Application'; use CGI::Application::Plugin::Config::Simple; sub cgiapp_init { my $self = shift; $self->config_file('/usr/home/foobar/.dsadmin.conf'); } sub cgiapp_prerun { my $self = shift; $query = $self->query(); $subscriber_id = $query->param('id'); $password => $self->config_param('s_pass'), $username => $self->config_param('s_user'), $dsn => $self->config_param('s_dsn'), }

Also, what might the difference be between sub teardown{} and cgiapp_postrun{}? Thanks all!


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: In CGI::Application cgiapp_prerun vs. cgiapp_init?
by Belgarion (Chaplain) on Dec 12, 2005 at 16:38 UTC

    The main difference is that cgiapp_init() is called once by new, while cgiapp_prerun() is called everytime a request comes in. In a standard CGI environment, both will always happen, but in a modperl environment, cgiapp_init() will only happen when the server first starts.

    The order of operation in a CGI::Application is basically:

    1. cgiapp_init()
    2. setup()
    3. cgiapp_prerun()
    4. your selected run mode
    5. cgiapp_postrun()
    6. teardown()

    cgiapp_init() and setup() are called in the class's new() method, while the remaining hooks are run in the class's run() method.

      The order of operations you provide does a better job at explaining this than your statement, "cgiapp_init() is called once by new, while cgiapp_prerun() is called everytime a request comes in".

      Basically, cgiapp_init() is called as the object is first created in order to setup the environment for the application. cgiapp_prerun() is called prior to executing the selected run mode for a given request. I typically use cgiapp_prerun() to check if the run mode is allowed at this point by this user -- i.e. I use cgiapp_prerun() as the place to implement ACLs.

      I know this is an old thread, but I ran into an issue under mod_perl where cgiapp_init was being called on every request, not just at server (or process) start. I did some searches and found this: http://old.nabble.com/cgi%3A%3Aapplication%3A%3Adispatch-and-modperl-td29916199.html Summary: they're both called on every request. Not sure if it's a change since this thread was posted but the information here doesn't seem to be correct.
Re: In CGI::Application cgiapp_prerun vs. cgiapp_init?
by dragonchild (Archbishop) on Dec 12, 2005 at 17:35 UTC
    As Belgarion said, the difference doesn't matter when dealing with a standard CGI request cycle. It only matters when dealing with a persistent environment.

    The theoretical difference is that cgiapp_init() and setup() should do things like set up your configuration vs. cgiapp_prerun() which should handle things like logging the request and setting up your per-request items. And, the same difference applies to cgiapp_postrun() (per-request) and teardown() (per-application).


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      Thanks dragonchild, that helps. I assume by "persistent" you mean something like mod_perl? I'm running the regular old CGI cycle, so may I can just dispense with the cgiapp_prerun.

      Well, on to C::A and cookies...


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        I can just dispense with the cgiapp_prerun.

        Don't. You will still benefit mentally from the separation of app-setup vs. response-setup. Plus, if you keep them separate, your migration to mod_perl will be easier when (not if!) it happens.


        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: In CGI::Application cgiapp_prerun vs. cgiapp_init?
by RhetTbull (Curate) on Dec 12, 2005 at 21:58 UTC
    The best place for a module specific question like this would be the module's mailing list or the module's wiki. There's a good article on the project wiki that addresses the order of operations.

Log In?
Username:
Password:

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

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

    No recent polls found