Interesting Perl behavior

2008-05-30 1 min read

    I ran into this problem a while back and wanted to share it. It was a bit unintuitive but documentd so I guess I shouldn't be surprised by the results. Hopefully this will help someone else avoid this pitfall.

    It looks as if declaring a variable with the "my" statement but then guarded with an "if" statement causes the scope of the variable to be global - note that the "use strict 'vars';" pragma does not give an error in this case.

    #!/usr/bin/perl -w
    use strict  'vars';
    sub foo{
        my $val = 0 if (0);
        $val = 1 unless defined($val);
        print "Val: $val\n";
        $val = 2;

    The output of this call gives:
    Val: 1
    Val: 2

    Although the expected result would seem to be:
    Val: 1
    Val: 1

    Using Google, I found the following nugget from perlsyn:

    NOTE: The behaviour of a my statement modified with a statement modifier conditional or loop construct (e.g. 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.