Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Why no bareword warnings while inside of BEGIN (more)

by tye (Sage)
on Mar 22, 2004 at 23:20 UTC ( #338824=note: print w/replies, xml ) Need Help??

in reply to Why no bareword warnings while inside of BEGIN

It appears that the test for barewords happens after compiling is finished. The test for undeclared variables also appears to wait until compilation is finished but happens sooner:

> perl -w use strict; BEGIN { while( calldepth < 5 ) { sleep 1; } print $n; } Global symbol "$n" requires explicit package name at - line 6. BEGIN not safe after errors--compilation aborted at - line 7.

Note that I don't even have to type end-of-file there but that the error didn't appear until I finished the BEGIN block. If the infinite loop ever finished, then you'd get the 'bareword' error:

> perl -w use strict; BEGIN { my $n= 5; while( calldepth < $n ) { warn $n--; } } Argument "calldepth" isn't numeric in numeric lt (<) at - line 4. 5 at - line 5. 4 at - line 5. 3 at - line 5. 2 at - line 5. 1 at - line 5. my $x = 5; BEGIN { warn "interesting" } Bareword "calldepth" not allowed while "strict subs" in use at - line +4. BEGIN not safe after errors--compilation aborted at - line 12.

Note that I still didn't have to type end-of-file. And it didn't take until the file was finished being compiled. So it seems like this might be a problem that can be fixed. The BEGIN block can't be run until it is done being compiled. The check for barewords is happening after the block is done being compiled but after the block is run.

So the next step is to figure out why it waits (until a subsequent BEGIN block is declared, in the above example) before the check for barewords is done?


- tye        

Replies are listed 'Best First'.
Re: Re: Why no bareword warnings while inside of BEGIN (more)
by TimToady (Parson) on Mar 23, 2004 at 01:14 UTC
    The code to execute BEGIN immediately doesn't properly check the compilation error flag before executing the block. Here's a patch against 5.8.3:
    --- op.c.orig 2004-03-22 16:59:09.000000000 -0800 +++ op.c 2004-03-22 16:59:25.000000000 -0800 @@ -4359,7 +4359,7 @@ if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I') goto done; - if (strEQ(s, "BEGIN")) { + if (strEQ(s, "BEGIN") && !PL_error_count) { I32 oldscope = PL_scopestack_ix; ENTER; SAVECOPFILE(&PL_compiling);
    It already appears to be fixed in 5.9.1.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://338824]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2017-05-29 20:36 GMT
Find Nodes?
    Voting Booth?