in reply to Re: Variable scope
in thread Variable scope

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,

Replies are listed 'Best First'.
Re^3: Variable scope
by shmem (Chancellor) on Apr 05, 2018 at 12:28 UTC
    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'
Re^3: Variable scope
by Anonymous Monk on Apr 06, 2018 at 09:19 UTC

    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.

Re^3: Variable scope
by Anonymous Monk on Apr 06, 2018 at 09:36 UTC

    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,

        Pardon me? That example would presumably return a sorting network or certain size and type; I'm not sure what you mean by "sort two networks". Memoization helps there because of recursive structure. I'll give another example that requires no explanation.

        sub fibonacci { my $n = shift; return 0+($n==1) if $n < 2; state @mem; $mem[$n] //= fibonacci($n-1) + fibonacci($n-2); }
        And before you ask: no, I do not foresee a need to produce two (different) Fibonacci sequences.

Re^3: Variable scope
by Anonymous Monk on May 15, 2018 at 17:11 UTC

    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,