in reply to Overloading without infinite descent (fixed by time: see Perl 5.10.1)
Update: I missed the line about "I could arrange that the constructor wraps the subroutine at construction time, rather than de-referencing time". My apologies to the OP. This won't answer the OP's question, but maybe it will be helpful to someone interested in the more general solution "how do I..."
Is your goal to wrap the call to $f in a sub that inserts some before (or maybe before and after behavior)? Or it is to do that and insure that the blessed code ref and $f have the same memory address? And if so, why is this important?
If you don't need the same memory address, you could simply define a constructor that wraps the subroutine in another subroutine, like this:
{ package Before; sub new { my ($sClass,$f) = @_; my $fBefore= sub { print "Special stuff for class $sClass\n"; goto &$f }; return bless($fBefore,$sClass); } } { package AlsoBefore; our @ISA=qw(Before); } my $f1=Before->new(sub {print "Hello world!\n"}); my $f2=Before->new(sub {print "Bonjour, le monde!\n"}); my $f3=Before->new(sub {print "Guten tag, die welt!\n"}); my $f4=AlsoBefore->new(sub {print "Boker tov, olam!\n"}); $f1->(); $f2->(); $f3->(); $f4->();
which prints
Special stuff for class Before Hello world! Special stuff for class Before Bonjour, le monde! Special stuff for class Before Guten tag, die welt! Special stuff for class AlsoBefore Boker tov, olam!
Best, beth
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Overloading without infinite descent
by JadeNB (Chaplain) on Aug 03, 2009 at 07:30 UTC | |
by ELISHEVA (Prior) on Aug 03, 2009 at 08:07 UTC |