Your question stands on its own merits, of course, but I'm still puzzled as to why one would want to do it. The syntax you describe above could be accomplished without overloading simply by using the wrapped functions reference address as a key into a hash that stores the original code reference. This would also give you the advantage of making the definition of i_know_what_im_doing class specific. Perhaps you could suggest another use case where overloading really is essential? The modified routine for those interested (I expect the OP could easily write it on his/her own).
use Scalar::Util; { package Before; my %hUnwrapped; sub new { my ($sClass,$f) = @_; my $fBefore= sub { print "Special stuff for class $sClass\n"; goto &$f }; $hUnwrapped{Scalar::Util::refaddr($fBefore)}=$f; return bless($fBefore,$sClass); } sub i_know_what_im_doing { my $k = Scalar::Util::refaddr(shift); goto $hUnwrapped{$k}; } } my $f1=Before->new(sub {print "Hello @_!\n";}); print "Wrapped sub\n-------------\n"; $f1->('tweedledum','tweedledee'); print "\nUnwrapped sub\n-------------\n"; $f1->i_know_what_im_doing('walrus','carpenter');
which prints
Wrapped sub ------------- Special stuff for class Before Hello tweedledum tweedledee! Unwrapped sub ------------- Hello walrus carpenter!
Best, beth
Update: distinguished my question from OP's and clarified my own question which is still about why
In reply to Re^3: Overloading without infinite descent
by ELISHEVA
in thread Overloading without infinite descent (fixed by time: see Perl 5.10.1)
by JadeNB
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |