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

One line CGI::Application instance script

by Arunbear (Parson)
on Apr 21, 2004 at 09:15 UTC ( #346942=snippet: print w/ replies, xml ) Need Help??

Description: CGI::Application instance scripts are refreshingly brief e.g.
use My::App; my $app = My::App->new; $app->run;
But this can be abbreviated even more ...
put this in your application module - it will be executed when the module is used
sub import { __PACKAGE__->new->run }
Now your instance script looks like this
use My::App; # yes that's it!
Comment on One line CGI::Application instance script
Select or Download Code
Re: One line CGI::Application instance script
by diotalevi (Canon) on Apr 21, 2004 at 11:26 UTC
    That defeats the purpose of having instance scripts. At least do something with @_ during that import call. You've got to handle parameters!
      What parameters? CGI::Application needs absolutely nothing from the instance script. It's merely a boostrap. Everything comes from the CGI instance, which C::A handles as part of the run() method.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I pass my configuration into the CGI::Application object. Of course, that change to operating everything during BEGIN is somewhat unusual as well. The perl interpreter running the parent's code doesn't reach INIT, CHECK or the normal runtime until the ->run call has completed. I wonder if that would cause problems for anyone or if fatal errors would interact poorly since to perl, this is all during the code-parsing stage.

        PerlMonks::Summoner -> new ( TMPL_PATH => TEMPLATEROOT . 'PerlMonks_Summoner/', PARAMS => { encryption_key => ..., url => ..., dbh => undef, change => undef, dbi_dsn => 'dbi:Pg:dbname=perlmonks', dbi_username => 'perlmonks', dbi_password => '...', }, )->run;
      At least do something with @_ during that import call.
      Like this?
      sub import { (shift)->new->run; }

      Anyway, I like the idea of removing the need for the use of an unclear idiom, and replace it with something much simpler. Liek this.

      Unfortunately, now your script will run at compile time...

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://346942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2014-07-29 04:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls