in reply to BEGIN and END blocks, use strict and scoping
... which implies that the global variable $shbstatus was not restricted by the use strict call inside the first BEGIN block.
Why not?
strict is block scoped, so its effect stops at the end of the BEGIN block.
The following works:
use strict; use warnings; my $shbstatus; BEGIN { $shbstatus = (-f "C:/shb") ? 1 : 0; } END { $shbstatus ? print "shb identified\n" : print "shb was not identified\n"; } print "XYZ\n"; print "$shbstatus\n";
Update: Things happen in the following order:
[*] It'll affect the current block (which is the current file) and nested blocks (which includes the BEGIN and END blocks, and sub definitions, loops, etc). It will *not* affect modules used/required by these blocks or subs called by these blocks.
[**] When a sub closes over a variable, it means the variable will be available to the sub (including special subs BEGIN and END) when the sub is executed, even if the variable is no longer in scope. Search for documentation on closures for more detail.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: BEGIN and END blocks, use strict and scoping
by jkeenan1 (Deacon) on Sep 23, 2005 at 21:21 UTC |