John Deighan wrote:
At 01:06 PM 2/17/2006, Joe Discenza wrote:

Fascinating! You have one misconception right off: The "my" line is never executed (not even to make the new variables) if $L is false.


OK, then, consider this program. Notice that there's a global $i which has the value 5. Now, when func() is called, if the "my $i" did not create a new lexically scoped variable, then the print line would print "i = 5". But it doesn't - obviously because the lexical variable was created, but is undefined because $b is false.

our $i = 5;
our $b = 0;

func();

sub func {

my $i = 3 if $b;
print("i = $i\n");
}

my is a compile time directive that creates the lexically scoped $i, giving is scope from its definition until the end of the enclosing block.

Assignment is a run time operation. In your example the modified assignment doesn't get executed, so the value of $i is *not defined*, in current perl's it happens to take the 'undef' value.

In the original OPs question he had the modified assignment inside a loop. persyn says this at the end of the 'Statement modifiers' section:

<quote>
NOTE: The behaviour of a my statement modified with a statement modifier conditional or loop construct (e.g. 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.
</quote>

So, ensure you separate the my $i and the modified assignment to ensure you know what's happening:
  my $i;
  $i = 3 if $b;

Regards,
Rob.
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to