in reply to Re^2: Variable scope
in thread Variable scope

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 );

Replies are listed 'Best First'.
Re^4: Variable scope
by choroba (Cardinal) on May 15, 2018 at 17:51 UTC
    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,