in reply to Logic for importing and strict vars?
why the fourth of these examples is an error
I'd say it's in accordance with strict (emphasis mine):
strict vars
This generates a compile-time error if you access a variable that was neither explicitly declared (using any of my, our, state, or use vars ) nor fully qualified.
(Edit: to re-phrase, maybe it's more clear -- was neither explicitly declared nor is fully qualified.)
Thus, no surprise that next 2 lines fail:
perl -wMstrict -e "$::x=0; $x++" # not OK perl -wMstrict -e "package foo; $foo::x=0; $x++" # not OK
Next 2 lines are your examples -- it's strange 2nd (your third) doesn't fail:
perl -wMstrict -e "BEGIN{*::x=\$a} $x++" # not OK (1) perl -wMstrict -e "BEGIN{package foo; *::x=\$a} $x++" # OK (2)
It has something to do with BEGIN:
perl -wMstrict -e "{package foo; *::x=\$a} $x++" # not OK
the deparsed output is an insult:
C:\>perl -MO=Deparse -wMstrict -e "{package foo; *::x=\$a} $x++" Variable "$x" is not imported at -e line 1. Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at -e line 1. -e had compilation errors. BEGIN { $^W = 1; } use strict; { package foo; *main::x = \$a; } $main::x++;
But also with "main" package. Next 2 are symmetrical with examples (1) and (2), just with "bar" instead of "main", but both OK now:
perl -wMstrict -e "BEGIN{package bar; *bar::x=\$a} package bar; $x++" + # not OK perl -wMstrict -e "BEGIN{package foo; *bar::x=\$a} package bar; $x++" + # OK
Edit 2: Actually, replacing "main" with "bar" doesn't change anything (fixed one of the lines above).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Logic for importing and strict vars?
by Veltro (Hermit) on Feb 27, 2019 at 13:14 UTC | |
by haukex (Archbishop) on Feb 28, 2019 at 08:49 UTC | |
|
Re^2: Logic for importing and strict vars?
by haukex (Archbishop) on Feb 28, 2019 at 08:45 UTC |