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)
You can think of a
mydeclaration as having an action at compile-time and at run-time. At compile-time, amydeclaration 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
is like
Note that this compile-time/run-time distinction allows you to write code like this.
Now can you guess what the output of this program is?