Hi, this is what I got to work:

package Foo; # must use '\' to create the reference, and since it's a # reference not an invocation, we can't specify arguments # here. (see the .pl file at the end) *do_something = \&make_do_something; sub make_do_something { # make_do_something is the method called, hence the # object reference will be on *this* @_, not the # one below. my ($self, $name) = @_;
Update: I didn't quite get this, you do in fact have to grab the object reference here in the anonymous sub definition, which requires that the calling code in the script be $b->$sub(). When I realized this I went back and found the same problem that you've been having, and it sounds like chromatic has pointed out the grim reality of the situation. To avoid the compile-time check you could use 'SUPER' as a string in an eval block.
return sub { if ( $name ne 'foo' ) { print "Not foo\n"; $self->SUPER::do_something(); } print "Foo $name!!!\n"; return 1; } } 1; #File: Bar.pm package Bar; use base 'Foo'; *do_something = \&Foo::make_do_something; sub new { bless {}, shift(); } 1; #!/usr/local/bin/perl use lib '.'; use Foo; use Bar; $b = new Bar(); # just calling do_something won't cause the subroutine # returned to execute; we've got to do that ourselves. my $sub = $b->do_something('foo'); # supply args here, not above. stor +e the subroutine ref $sub->(); # dereference it to run it


In reply to Re: Trying to re-use code at different levels of an inherited object. by djantzen
in thread Trying to re-use code at different levels of an inherited object. by ehdonhon

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.