Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I know if I'm in a BEGIN block?

by dgaramond2 (Monk)
on Oct 29, 2009 at 03:10 UTC ( #803860=perlquestion: print w/replies, xml ) Need Help??

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

I made a poor judgement in one of my modules and mixed in some code (say A+B) in a sub (say foo()). foo() is called from other modules (outside of any sub, so they run in BEGIN phase) as well as in the main script (so they run in run-time phase).

Turns out, the A portions of the code actually should not really be run until run-time phase. But foo() is called in many places so, pending refactoring, I want to do a quick-fix first and tackle A & B.

Now I need to be able to check whether I am running in BEGIN phase or not, to prevent A from being run in BEGIN phase (when called by other modules), but I want to let A run when in run-time phase. Is there a way to do that? I only see something like $^C in perlvar.

Replies are listed 'Best First'.
Re: How do I know if I'm in a BEGIN block?
by kyle (Abbot) on Oct 29, 2009 at 03:35 UTC

    See perlmod.

    BEGIN { $I_AM_COMPILING = 1 } INIT { $I_AM_COMPILING = 0 } sub foo { if ( ! $I_AM_COMPILING ) { } }

    I don't know if that will work, but that's what I'd try first.

      Thanks! I feel so stupid for not having come up with that myself :-(
Re: How do I know if I'm in a BEGIN block?
by dave_the_m (Monsignor) on Oct 29, 2009 at 10:54 UTC
    You could use caller() to see if one of the functions in the call chain is called 'BEGIN'

    Dave.

Re: How do I know if I'm in a BEGIN block?
by LanX (Cardinal) on Oct 30, 2009 at 02:31 UTC
    It should be possible straight away by attaching Attribute::Handlers to foo() which sets a flag or even modifies your code...

    "it is possible to set up attribute handlers that are called at other points in the program's compilation or execution, by explicitly stating the phase (or phases) in which you wish the attribute handler to be called."

    Cheers Rolf

Re: How do I know if I'm in a BEGIN block?
by Kirsle (Pilgrim) on Oct 29, 2009 at 21:23 UTC
    [kirsle@damocles ~]$ cat begin.pl #!/usr/bin/perl -w use begin; BEGIN { begin::myfunc(); } print "lalala\n"; begin::myfunc(); [kirsle@damocles ~]$ cat begin.pm package begin; sub myfunc { my @caller = caller(1); print "caller info: @caller\n"; } 1; [kirsle@damocles ~]$ perl begin.pl Use of uninitialized value $caller[6] in join or string at begin.pm li +ne 5. Use of uninitialized value $caller[7] in join or string at begin.pm li +ne 5. Use of uninitialized value $caller[10] in join or string at begin.pm l +ine 5. caller info: main begin.pl 7 main::BEGIN 1 0 256 UUUUUUUUUUUU lalala caller info:

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-05-17 11:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (154 votes). Check out past polls.

    Notices?