common lispだと
(let ((count 0))
(list
#'(lambda () (incf count))
#'(lambda () (decf count))
#'(lambda () (count)))
同じことをperlでもlispっぽくかける。
#!/usr/bin/env perl
use Perl6::Say;
sub make_counter {
my $c = 0;
return (
sub {$c++},
sub {$c--},
sub {$c}
);
}
my ($inc,$dec,$counter) = make_counter();
say $counter->(); # 0
$inc->();
$inc->();
$inc->();
say $counter->(); # 3
$dec->();
say $counter->(); # 2