Compare using perl -w -Mstrict
:
# case Alpha
print $c;
...
# case Bravo
if (0) {
my $c = 1;
}
print $c;
...
# case Charlie
my $c = 1 if 0;
print $c;
Alpha
and Bravo
both complain about the global symbol not having an explicit package name, which is to be expected. But Charlie
does not give the same warning, only that the value is uninitialized, which smells a lot like:
# case Delta
my $c;
print $c;
What exactly is going on under the hood? (Even though something like this should never be written for production code)
(kəndɪʃənəl ) adjective. If a situation or agreement is conditional on something, it will only happen or continue if this thing happens. Their support is conditional on his proposals meeting their approval.
mob's answer is a great explanation of what currently happens (and why), but don't forget that perldoc perlsyn
tells us:
NOTE: The behaviour of a
my
,state
, orour
modified with a statement modifier conditional or loop construct (for example,my $x if ...
) is undefined. The value of themy
variable may beundef
, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
Don't count on that result or the explanation for it still being true in future versions of Perl. (Although it probably will be.)
You can think of a my
declaration as having an action at compile-time and at run-time. At compile-time, a my
declaration tells the compiler to make a note that a symbol exists and will be available until the end of the current lexical scope. An assignment or other use of the symbol in that declaration will take place at run-time.
So your example
my $c = 1 if 0;
is like
my $c; # compile-time declaration, initialized to undef
$c = 1 if 0; # runtime -- as written has no effect
Note that this compile-time/run-time distinction allows you to write code like this.
my $DEBUG; # lexical scope variable declared at compile-time
BEGIN {
$DEBUG = $ENV{MY_DEBUG}; # statement executed at compile-time
};
Now can you guess what the output of this program is?
my $c = 3;
BEGIN {
print "\$c is $c\n";
$c = 4;
}
print "\$c is $c\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With