in reply to Re: subroutine concatenation
in thread subroutine concatenation

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__

Replies are listed 'Best First'.
Re^3: subroutine concatenation
by chromatic (Archbishop) on Feb 10, 2005 at 01:15 UTC

    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 . . .).