in reply to Variable scope

This is related to the hack used to implement state variables in subroutines, before state variables have been implemented in perl 5.10.0, which emits a warning on recent perls. It allocates the lexical variable on the subroutine's pad (or better: the pad of the current scope), but prevents it from being cleared:

#file state.pl sub foo { my $bar if 0; $bar++; print "foo called $bar times\n"; } foo for 'a'..'f'; __END__ Deprecated use of my() in false conditional at state.pl line 2. foo called 1 times foo called 2 times foo called 3 times foo called 4 times foo called 5 times foo called 6 times

This works also for lexical arrays.

However, if the conditional is declared beforehand as a variable, there is no such warning, since that warning happens at compile time, but the conditional is resolved at runtime:

# file state.pl my $cond; sub foo { my @bar if $cond; push @bar, @_; print "array \@bar = (@bar)\n"; } foo $_ for 'a'..'f'; $cond = 1; foo $_ for 1..6; __END__ array @bar = (a) array @bar = (a b) array @bar = (a b c) array @bar = (a b c d) array @bar = (a b c d e) array @bar = (a b c d e f) array @bar = (a b c d e f 1) array @bar = (2) array @bar = (3) array @bar = (4) array @bar = (5) array @bar = (6)

Huh? We have array @bar = (a b c d e f 1) here? Well, my variables are cleared at the end of their scope, so setting $cond = 1 doesn't clear the (state) array immediately.

If you don't have a sophisticated use (read: obfuscated use) for altering the behavior of my variables, you should use:

use feature qw(state); # also use 5.10.0; sub foo { state @bar; ... }
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: Variable scope
by choroba (Cardinal) on Apr 05, 2018 at 12:12 UTC
    From my experience, using state in a named subroutine is a code smell. Sooner or later, you'll need to call the same subroutine for a different purpose, and you'll need a way to reinitialize the state variable with a different value. I usually use two closures over the same my variable, one of them being the setter.
    my $state; sub init_state { $state = shift } sub foo { if ($state) { ...

    Where state makes sense, in my opinion, is in anonymous subs, because an anonymous sub has a defined life span:

    for my $i (1 .. 3) { my $s = sub { state $x = $_; say "$i $x" }; $s->($_) for qw( a b ); }

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      From my experience, using state in a named subroutine is a code smell.

      That depends on the life span of the code. The older it gets, the more it reeks of foul.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Static variables have their uses. For example, one might generate a crc32 table on-demand. This has the advantages of: (1) not using globals, (2) not relying on BEGIN, (3) on-demand construction, (4) conciseness. It replaces the less expressive construct

      { my $table; sub foo { $table //= init_tables(0x123); ... } }
      There's nothing wrong with procedural programming.

      Oh, and another use for state variables — caching/memoization. For example:

      sub sort_network { state @mem; my $cmp = $mem[@_] //= do { ... }; my @res = @_[ @$cmp ]; }
      I'm sure there are plenty more uses.

        That's exactly what I warned against. Imagine you now need to sort two networks: how do you clear the cache before the second run?

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Hm. One more example. Here state is used to enumerate unique string instances (over the whole lifetime of a program):

      #! /usr/bin/perl use warnings; use 5.010; sub enumerate { my ($property, $key) = @_; state (%hash, %enum); $hash{$property}{$key} //= ++$enum{$property}; } print "\n", "Edibles:\n"; print "$_ => @{[enumerate edible => $_]}\n" for qw( apple spam pear orange spam spam spam kebab spam caterpill +ar ); print "\n", "Colors:\n"; print "$_ => @{[enumerate color => $_]}\n" for qw( red yellow red red red green );

        Thank you for giving me another counter-example. Add "orange" to the colours.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,