perl メモ(state)
#!/usr/bin/perl use strict; use warnings; use 5.010; { # 1 増やす sub r1 { my $n = shift @_; $n += 1; } say r1(0); # 1 say r1(0); # 1 # 1 増やす(再初期化されない) sub r2 { state $n = shift @_; $n += 1; } say r2(0); # 1 say r2(0); # 2 } exit;
#!/usr/bin/perl use strict; use warnings; use 5.010; { # 1 増やす sub r1 { my $n = shift @_; $n += 1; } say r1(0); # 1 say r1(0); # 1 # 1 増やす(再初期化されない) sub r2 { state $n = shift @_; $n += 1; } say r2(0); # 1 say r2(0); # 2 } exit;