Hi,

I just got bitten by a very simple issue, where Perl behaves totally
different from what I had expected;

According to the documentation, lexical variables are visible only after
the line they have been declared in; they may be initialized;
otherwise their value is undefined, so the following short code snippet:

foreach (qw(a b c)) {
    my $t;
    warn("\$t == ", $t||'undef', "\n");
    $t=$_;
}

will 3 times print "$t == undef" (as to be expected).
Now a minor variation:

my $x=undef;
foreach (qw(a b c)) {
    my $t =$x if $x;
    warn("\$t == ", $t||'undef', "\n");
    $t=$_;
}

$t would be initialized with the value of $x if that was true;
otherwise (at least that's what I would expect) $t should be undefined,
so the result would be as before. The real outcome, however, is:

$t == undef
$t == a
$t == b

$t now retains its value from the last loop iteration.
Is this a bug or a feature (tm)?
If it is a feature, then why isn't the value also retained in the 1st example?

Regards,
                          Peter Daum


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to