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

    Sub, schmub. Overload is your (evil) friend . . .

    Update: Of course for efficiency you might want to check if UNIVERSAL::isa( $other, "CatCode" ) and pull out its coderef from $other->{_code}.

    Update: Another tweak. To paraphrase Homer Simpson, "In America first joo geet dee syntactic shoooogar, dehn joo geet dee powah, dehn joo geet de wimin."

    Update: And even more silliness, buscc that reverses the call order (new code called first then the old code).

    #!/usr/bin/perl BEGIN { ## This would really be in CatCode.pm . . . package CatCode; use UNIVERSAL qw( isa ); use overload "." => "_cat_sub", "&{}" => "_call_sub"; require Exporter; @CatCode::ISA = qw( Exporter ); @CatCode::EXPORT_OK = qw( ccsub ); sub ccsub (&) { return CatCode->new( shift() ); } sub new { my $class = shift; my $code = shift; return bless { _code => $code }, $class; } sub _cat_sub { my( $self, $other ) = @_; my $oldcode = $self->{_code}; if( isa( $other, "CatCode::Reverse" ) ) { $other = $other->{_code}; $self->{_code} = sub { $other->( @_ ); $oldcode->( @_ ); }; } elsif( isa( $other, "CatCode" ) ) { $other = $other->{_code}; $self->{_code} = sub { $oldcode->( @_ ); $other->( @_ ) }; } else { $self->{_code} = sub { $oldcode->( @_ ); $other->( @_ ) }; } return $self; } sub _call_sub { my $self = shift; sub { $self->{_code}->( @_ ) } } package CatCode::Reverse; @CatCode::Reverse::ISA = qw( CatCode ); @CatCode::Reverse::EXPORT_OK = qw( buscc ); sub buscc (&) { CatCode::Reverse->new( shift() ); } 1; } package main; BEGIN { CatCode->import( qw( ccsub ) )} BEGIN { CatCode::Reverse->import( qw( buscc ) )} my $cc = ccsub { print "one $_[0]\n" }; $cc .= sub { print "two $_[0]\n" }; $cc .= buscc { print "three $_[0]\n" }; $cc .= sub { print "four $_[0]\n" }; $cc->( "fish" ); exit 0 __END__

      If you overrode _cat_sub in CatCode::Reverse, you could replace the conditional with polymorphism and wouldn't have to use the polymorphism-hostile direct UNIVERSAL::isa call.

        True, but then it wouldn't be able to break encapsulation and pull out $other->{_code} to speed things up by avoiding another overloaded &{} dispatch. Then again that's not exactly safe either, but what does one expect for a ten minute one-off example. :)

        A better solution might be to ask if UNIVERSAL::can( $other, "to_code" ) and then use that to obtain a coderef from $other (which would be starting to look a bit like duck typing . . .).

Re^2: subroutine concatenation
by flounder99 (Friar) on Feb 09, 2005 at 23:33 UTC
    Using an array is the right solution but this works.
    my $code = sub {print "Sub 1\n"}; $code = joincoderefs( $code, sub {print "Sub 2\n"}); &$code; sub joincoderefs { my @refs = @_; return sub { $_->() for @refs }; }
    outputs:
    Sub 1 Sub 2

    --

    flounder

      Using an array is the right solution

      I agree that an array of coderefs seems to be the best solution, in this case. Especially since the OP has already responded, saying that the array works. But I still think it's useful to show other techniques -- such as you have done -- which may be more appropriate in other situations. Talking about which one is the "right solution" is a different discussion altogether.

Re^2: subroutine concatenation
by phaylon (Curate) on Feb 09, 2005 at 22:17 UTC
    to execute from a single starting point.

    Just put the loop in a sub, there ya go :D