Chas. Owens wrote:
On Jan 13, 2008 6:22 AM, Peter Daum <[EMAIL PROTECTED]> wrote:
snip
my $t =$x if $x;
snip
$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?
snip
People argue about whether this is a bug or not*. In my opinion, it
is a bug because the variable's scope should be within the if
statement.
Statements don't define scope, braces and files define scope.
You wouldn't expect to be able the use the variable if the
code looked like this
if ($x) {
my $t = $x;
}
The scope is limited because of the braces.
print "$t\n";
so why should you be able to use it because it has been changed to this
my $t = $x if $x;
print "$t\n";
You can use it because it is in the same scope.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/