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

后端 未结 3 1684
我在风中等你
我在风中等你 2020-12-11 15:58

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

I\'ve come across an example of creating static variables in these Perls: my $x if 0. The i

3条回答
  •  死守一世寂寞
    2020-12-11 16:41

    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";
        }
    }
    

提交回复
热议问题