Scope of variables in a function

Paul Johnson paul at pjcj.net
Sat Jun 1 18:13:26 BST 2013


On Sat, Jun 01, 2013 at 07:03:45PM +0200, Marcel Grünauer wrote:
> On H.25/06/01, at 18:43, Andrew Beverley <andy at andybev.com> wrote:
> 
> >  my $result = "FOO" if $in =~ /foo/;
> 
> Conditional variable declaration is confusing.
> 
> Try:
> 
>     my $result;
>     $result = "FOO" if $in =~ /foo/;
> 
> See https://metacpan.org/module/Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations

And the official line from perlsyn:

    NOTE: The behaviour of a "my", "state", or "our" modified with a statement
    modifier conditional or loop construct (for example, "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.

-- 
Paul Johnson - paul at pjcj.net
http://www.pjcj.net



More information about the london.pm mailing list