[Melbourne-pm] Postfix conditionals and creating lexicals

Damian Conway damian at conway.org
Fri Jul 13 07:57:54 PDT 2012

Sam suggested:

> looks like targetting the specific idiom:
>   my $foo ... if 0
> should warn about any
>   my $foo ... if ...

I doubt it. Because:

    my $foo if 1;

is perfectly legal and does exactly what you'd expect.

It's only when the 'if' fails that Perl omits the run-time
aspect of a 'my', leaving only the compile-time
declaration...which leads to the static-like behaviour.

In fact (and I can't believe I'm showing you this!), because of that
conditional behaviour you can actually create a "selectably static"
variable within a subroutine, like so:

    use warnings;

    my $static_bar = 1;

    sub foo {
        my $bar = 666 if !$static_bar;
        say "bar = $bar";

    foo; foo; foo;          # 1 2 3

    $static_bar = 0;
    foo; foo; foo;          # 667 667 667

    $static_bar = 1;
    foo; foo; foo;          # 1 2 3

As you can see, it's quite a difficult philosophical and moral problem
to decide whether this is (a) genius, (b) demented, (c) evil, (d) all of
the above. And until you can agree on that, how can you decide whether
it warrants a warning?



More information about the Melbourne-pm mailing list