Hi, this is what I got to work:
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.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) = @_;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |