Scope of variables in a function

Paul Johnson paul at
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> wrote:
> >  my $result = "FOO" if $in =~ /foo/;
> Conditional variable declaration is confusing.
> Try:
>     my $result;
>     $result = "FOO" if $in =~ /foo/;
> See

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

More information about the mailing list