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 );
In reply to Re^3: Variable scope
by Anonymous Monk
in thread Variable scope
by Alphaphi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |