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

Hi! I've just found what INIT{} and CHECK{} blocks ignored in eval(). I've not found information about this behaviour in docs, so this may be a bug. What do you think about this?
$ perl -le ' print 1; INIT { print 2 } END { print 3 } BEGIN { print 4 } CHECK { print 5 } ' 4 5 2 1 3 $ perl -le 'eval " print 1; INIT { print 2 } END { print 3 } BEGIN { print 4 } CHECK { print 5 } "' 4 1 3

Replies are listed 'Best First'.
use warnings;
by PodMaster (Abbot) on Mar 28, 2004 at 13:56 UTC
    turn warnings on, and you'll see things like
    C:\dev\LOOSE>cat s eval " print 1; INIT { print 2 } END { print 3 } BEGIN { print 4 } CHECK { print 5 } "; print "out of eval $@"; C:\dev\LOOSE>perl -Wl s Too late to run INIT block at (eval 1) line 3. 4 Too late to run CHECK block at (eval 1) line 6. 1 out of eval 3

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: eval() and INIT{}
by calin (Deacon) on Mar 28, 2004 at 14:03 UTC

    Try running with warnings enabled:

    $ perl -wle 'eval " print 1; INIT { print 2 } END { print 3 } BEGIN { print 4 } CHECK { print 5 } "' Too late to run INIT block at (eval 1) line 3. 4 Too late to run CHECK block at (eval 1) line 6. 1 3

    What happens is that "string eval" code is compiled after the run-time has been initialised, therefore it's too late for INIT/CHECK mechanism.

Re: eval() and INIT{}
by diotalevi (Canon) on Mar 29, 2004 at 03:47 UTC
    You won't see this if you do your evals during BEGIN, before the once-only INIT has happened.