ForNext
Only Do What Only You Can Do
151. function with private state
Hyperpolyglot Interpreted Languages: PHP, Perl, Python, Ruby より (2012.05.08)
VBScript
JScript
Perl
use feature state; sub counter { state $i = 0; ++$i; } print counter() . "\n";
PHP
function counter() { static $i = 0; return ++$i; } echo counter();
Python
# state not private: def counter(): counter.i += 1 return counter.i counter.i = 0 print(counter())
Ruby
none