Scope of variables in a function
Yitzchak Scott-Thoennes
sthoenna at gmail.com
Sun Jun 2 06:25:45 BST 2013
On Sat, Jun 1, 2013 at 12:13 PM, Andrew Beverley <andy at andybev.com> wrote:
> On Sat, 2013-06-01 at 19:15 +0100, Anthony Lucas wrote:
>> It's not that it's not recommended, it's just that it's usually not what
>> you meant. There's nothing objectively wrong with not reinitialising a
>> variable on every call into your function.
>
> From the previously quoted perlsyn text "The behaviour ... is
> undefined... Future versions of perl might do something different from
> the version of perl you try it out on."
>
> That says to me that it should never be used?
Yes, if recollection serves me correctly, that statement was put in the docs
to discourage people from using "my $state_var if 0;" before we had state
instead of doing:
{
my $x;
sub foo {
}
}
There were two reasons to discourage it; first, that it might someday get
fixed, since people kept getting hurt by it, and second, that it didn't actually
"work" in all cases (e.g. recursion).
More information about the london.pm
mailing list