Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is happening when my() is conditional?

Tags:

perl

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)

like image 788
Mark Canlas Avatar asked Jun 29 '12 20:06

Mark Canlas


People also ask

What does it mean when something is conditional?

(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.


2 Answers

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, or our modified with a statement modifier conditional or loop construct (for example, my $x if ... ) is undefined. The value of the my variable may be undef, 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.)

like image 35
Dave Sherohman Avatar answered Sep 20 '22 13:09

Dave Sherohman


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";
like image 93
mob Avatar answered Sep 24 '22 13:09

mob