[Melbourne-pm] Postfix conditionals and creating lexicals

Robert Norris rob at eatenbyagrue.org
Thu Jul 12 16:26:44 PDT 2012


Seems a bit hit-and-miss (actually mostly miss):

    robn at pyro:~$ perl -Mwarnings -Mstrict -E 'my $x if 0'
    Deprecated use of my() in false conditional at -e line 1.
    robn at pyro:~$ perl -Mwarnings -Mstrict -E 'my $x if 1'
    robn at pyro:~$ perl -Mwarnings -Mstrict -E 'my $x if undef'
    robn at pyro:~$ foo=0 perl -Mwarnings -Mstrict -E 'my $x if $ENV{foo}'
    robn at pyro:~$ foo=1 perl -Mwarnings -Mstrict -E 'my $x if $ENV{foo}'
    robn at pyro:~$ perl -v

    This is perl 5, version 16, subversion 0 (v5.16.0) built for x86_64-linux

Its great that its sort-of documented in perlsyn, but other parts of the
document are vaguely misleading, eg perlsub:

    The parameter list to my() may be assigned to if desired, which
    allows you to initialize your variables.  (If no initializer is
    given for a particula variable, it is created with the undefined
    value.)

Or "state" in perlfunc:

    "state" declares a lexically scoped variable, just like "my".
    However, those variables will never be reinitialized, contrary to
    lexical variables that are reinitialized each time their enclosing
    block is entered.

So while we can argue about whether or not its a bug (which seems more
about how you define "bug") it does seem that the docs and warnings
aren't consistent, and it certainly doesn't give the behaviour you'd
expect. If its been used in the past as a way to do statics then its
unlikely that it will ever change (backward compatibility) but better
docs and warnings could help a lot.

But docs and warnings aside, there's the general expectation. Everybody
"knows" that my $x gets you $x == undef. Its one of the first things you
learn in Perl kindergarten. True, Perl has plenty of quirks but for the
most part they're internally consistent and easy to avoid until you
actual need them. This touches something pretty fundamental and when it
breaks it does so silently and can have serious consequences (like
mod_perl state leaks).

If I get around to it I'll open a bug report, even if its just asking
for better warnings. I doubt I'll have time to work up a patch myself.


More information about the Melbourne-pm mailing list