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


In reply to Re: Overloading without infinite descent by ELISHEVA
in thread Overloading without infinite descent (fixed by time: see Perl 5.10.1) by JadeNB

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.