in reply to subroutine concatenation
Concatenation is for strings. You need to make a copy of the first coderef and make a new coderef that calls the copy. Note that the copy stays around, even though it's gone out of scope, because the new one creates a closure around it. Here's an example:
my $code = sub { print "Sub 1"; }; $code = do { my $oldcode = $code; sub { $oldcode->(); print "Sub 2"; }; }; $code->();
Update: this do block could be further generalized into a subroutine, but that's an exercise left to the reader. :-)
Update: phaylon's solution is much simpler, and the OP seems happy with it, but the closure technique I've shown is a way to get both [or as many as necessary] to execute from a single starting point, [update] and without a loop at all :-P
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: subroutine concatenation
by Fletch (Bishop) on Feb 09, 2005 at 21:53 UTC | |
by chromatic (Archbishop) on Feb 10, 2005 at 01:15 UTC | |
by Fletch (Bishop) on Feb 10, 2005 at 01:32 UTC | |
|
Re^2: subroutine concatenation
by flounder99 (Friar) on Feb 09, 2005 at 23:33 UTC | |
by revdiablo (Prior) on Feb 09, 2005 at 23:41 UTC | |
|
Re^2: subroutine concatenation
by phaylon (Curate) on Feb 09, 2005 at 22:17 UTC |