开发者

Is the 'my $x if 0' trick usable for static variable creation for Perls before 5.10?

开发者 https://www.devze.com 2022-12-19 00:38 出处:网络
In Perl before 5.10 there is no \"state\" declaration. I\'ve come across an example of creating static variables in these Per开发者_开发百科ls: my $x if 0. The if 0 conditional makes the variable act

In Perl before 5.10 there is no "state" declaration.

I've come across an example of creating static variables in these Per开发者_开发百科ls: my $x if 0. The if 0 conditional makes the variable act like a static variable:

use strict; use warnings;
add() for 1..7;

sub add {
    my @arr = () if 0;

    push @arr, '+';
    print @arr, "\n";
}

prints:

+
++
+++
++++
+++++
++++++
+++++++

Is this behaviour consistent in all versions of Perl before 5.10?


The behavior of my $x if 0 is a bug. It has survived for a long time because it's useful and thus used; fixing it would break existing code. It's consistent and could therefore be considered usable but that doesn't mean you should do so. This "feature" has been deprecated and issues a warning as of 5.10:

Deprecated use of my() in false conditional

Even if you can't use state (i.e your code needs to be able to run under versions of Perl prior to 5.10) the my $x if 0 trick is just laziness. Use a closure otherwise:

{
    my $x;
    sub counter {
        $x = '1' unless defined $x;
        print $x++, "\n";
    }
}


I've always used scoping braces to create static variables.

add() for 1..2;       # Append to existing.
add('foo', 'bar');    # Re-initialize if args are passed.
add() for 1..2;       # Append to existing.
{
    my @arr;
    sub add {
        @arr = @_ if @_;
        push @arr, '+';
        print @arr, "\n";
    }
}


No, it's very crummy. Use a closure instead.

use strict;

sub add {
    my @arr;

    return sub {
        push @arr, '+';
        print @arr, "\n";
    }
}

my $iter = add();
$iter->() for 1..7;
0

精彩评论

暂无评论...
验证码 换一张
取 消