in reply to Confusion about BEGIN block
gets us:#!perl -l use strict; use warnings; print "v: $v"; BEGIN{ my $v }
Global symbol "$v" requires explicit package name at begin.pl line 4. BEGIN not safe after errors--compilation aborted at begin.pl line 5.ok, it's barfing on the print line. looks like the BEGIN declaration isn't getting handled first, or if it is, it's not the right scope. let's try swapping the order of the lines to see if it makes a difference
gets us#!perl -l use strict; use warnings; BEGIN{ my $v } print "v: $v";
Global symbol "$v" requires explicit package name at begin.pl line 5. Execution of begin.pl aborted due to compilation errors.different error. looks like our declaration isn't in scope. let's using "our" as you originally did, and try fully qualifying $v as $::v
gets us#!perl -l use strict; use warnings; BEGIN{ our $v } print "v: $::v";
Use of uninitialized value in concatenation (.) or string at begin.pl line 5. v:ok, so that works, it just doesn't like the fact that $::v isn't defined. now let's try defining $v outside the begin block
gets us#!perl -l use strict; use warnings; my $v = 5; BEGIN{ $v++ } print "v: $v";
v: 5ok, so there's still no problems with scope, but does the BEGIN code even get run? let's try something else
gets us#!perl -l use strict; use warnings; my $v; BEGIN{ $v++ } print "v: $v";
v: 1so the BEGIN code is getting run... it looks like BEGIN requires a definition and will use it even if it's in non-BEGIN code, but it gets first dibs on assignment. the reason we got v: 5 is because the assignment = 5 happened AFTER the begin block, even though the declaration and assignment happened in the same statement
fun stuff!
perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Confusion about BEGIN block
by Sandy (Curate) on Oct 15, 2004 at 16:10 UTC | |
by revdiablo (Prior) on Oct 15, 2004 at 16:41 UTC |