in reply to state is the root of evil?
Output (ACCEPTED):use strict; use warnings; use feature 'state'; my $test_string = $ARGV[0] // q{ababababac}; sub transition { my $symbol = shift; state $current = 'start'; # initialize on first call to __SUB__ my $trans = { start => { a => 1, b => 'start', c => 'sink' }, 1 => { a => 1, b => 'start', c => 'final' }, final => { a => 'sink', b => 'sink', c => 'sink' }, }; my $next = ( $trans->{$current}->{$symbol} ) ? $trans->{$current}->{$symbol} : q{sink}; print qq{$current -> $next on "$symbol"\n}; $current = $next; # update stateful var, will be this valu +e on next call to __SUB__ return $current; } my $position; for my $s ( split //, $test_string ) { $position = transition($s); } printf( qq{string "%s" is %s\n}, $test_string, ( $position eq q{final} + ) ? q{ACCEPTED} : q{REJECTED} );
Output (REJECTED):$ perl state.pl abababac start -> 1 on "a" 1 -> start on "b" start -> 1 on "a" 1 -> start on "b" start -> 1 on "a" 1 -> start on "b" start -> 1 on "a" 1 -> final on "c" string "abababac" is ACCEPTED
The primary use case I am showing here is that, what would normally need to be global in scope can now be cleanly scoped lexically within the subroutine, transition. Can state be used for caching? Yes, but no more effectively than using a global variable.perl state.pl abababa start -> 1 on "a" 1 -> start on "b" start -> 1 on "a" 1 -> start on "b" start -> 1 on "a" 1 -> start on "b" start -> 1 on "a" string "abababa" is REJECTED
|
|---|