#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 #### # 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) #### use feature qw(state); # also use 5.10.0; sub foo { state @bar; ... }