in reply to Composing or chaining subroutines
sub chain { my (@subs) = @_; sub { my (@p) = @_; foreach (@subs) { @p = $_->(@p); } } } sub a { $_[0] += 1; print "a - $_[0]\n"; @_ } sub b { $_[0] += 2; print "b - $_[0]\n"; @_ } sub c { $_[0] += 3; print "c - $_[0]\n"; @_ } $x = chain(\&a, \&b, \&c); $x->(7); # Output: # a - 8 # b - 10 # c - 13
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Composing or chaining subroutines
by dragonchild (Archbishop) on Jan 18, 2006 at 01:24 UTC |