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__

In reply to Re^2: subroutine concatenation by Fletch
in thread subroutine concatenation by imcsk8

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.