Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

mod_perl and imports

by Anonymous Monk
on Sep 26, 2013 at 18:27 UTC ( #1055877=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

First timer running mod_perl. If I put code in a BEGIN block will it get executed each time a request is made (I'm understanding it to be no)? I have some "require"d pms that I want to include but there is no need to execute the code each time a request is made (same declarations, etc..,). How can I include a file of code in a BEGIN and have it execute only on program initialization? Thanks

Comment on mod_perl and imports
Re: mod_perl and imports
by Eily (Hermit) on Sep 26, 2013 at 22:22 UTC

    Why don't you just try it?

    #file One.pm package One; BEGIN { print "Begin One\n" } sub import { print "Import One\n" } 1
    #file Two.pm package Two; use One; 1
    use One; use Two;
    The import method won't be called if you require instead of use your modules.

Re: mod_perl and imports
by Marshall (Prior) on Sep 27, 2013 at 04:43 UTC
    A BEGIN{} block will run right after the module is compiled.
    It will not run again.

      It will actually be run straightaway, as soon as the block is reached by the compiler. So whatever lines are below the BEGIN { } won't have even been seen at the time of its execution.

        from http://perldoc.perl.org/perlmod.html

        A BEGIN code block is executed as soon as possible, that is, the moment it is completely defined, even before the rest of the containing file (or string) is parsed. You may have multiple BEGIN blocks within a file (or eval'ed string); they will execute in order of definition. Because a BEGIN code block executes immediately, it can pull in definitions of subroutines and such from other files in time to be visible to the rest of the compile and run time. Once a BEGIN has run, it is immediately undefined and any code it used is returned to Perl's memory pool.
        So, yes, a BEGIN block will run when it is encountered. But it will only run once - that part of my statement is correct. But it does appear that you can intersperse BEGIN blocks within the code and they will run essentially as the code "compiles". that is cool Perl thing.

        When I have used BEGIN{}, I have never interspersed these within the code, but rather have put these at the "end" of the code. In other words, this is initialization code that should be executed before the main code - not interspersed.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2014-07-25 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls