Scope of variables in a function
andy at andybev.com
Sat Jun 1 18:58:13 BST 2013
On Sat, 2013-06-01 at 18:23 +0100, Dirk Koopman wrote:
> On 01/06/13 18:03, Hakim Cassimally wrote:
> > Andy,
> > I believe your problem is:
> > my $x = 'FOO' if $condition;
> > This only declares the new variable if $condition, so it ends up having
> > surprising, static-like behaviour, which you probably shouldn't rely on.
> > Rewriting to:
> > my $result;
> > $result = 'FOO' if ...
> > gives your expected result.
Thanks for all the replies.
I assume the above that everyone suggested is the cleanest way of
writing it? I try to avoid separately declaring variables where
possible, as IMHO it clutters the code.
> It is very annoying.
It certainly cost me a couple of hours of debugging time, especially as
it was an intermittent problem!
More information about the london.pm