in reply to Trying to re-use code at different levels of an inherited object.
Instead, how about a more traditional polymorphic approach:
I _think_ this will accomodate your needs ...package Foo; use strict; sub new { bless {}, shift(); } sub do_something { my $self = shift; my $name = shift || 'foo'; return make_do_something($name); } sub make_do_something { my $name = shift; return sub { my $self = shift; print "Foo $name!!!\n"; } } 1; ############################################## package Bar; use strict; use base 'Foo'; sub new { bless {}, shift(); } sub do_something { my $self = shift; return $self->SUPER::do_something('bar'); } 1;
This is my test script. The big difference is that the do_something() method returns a code ref, so you need to add some syntax:
The reason why is, well - i don't know exactly what you are trying to accomplish by returning a sub in your make_do_something() method. If the only reason you do that is to create a method, then you can use this code and just drop make_do_something() completely. Everything you need is in Foo::do_something(). Hope this helps.#!/usr/bin/perl -w use strict; use Foo; use Bar; $_->do_something()->() for (new Foo, new Bar);
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (jeffa) Re: Trying to re-use code at different levels of an inherited object.
by ehdonhon (Curate) on Jan 29, 2002 at 09:19 UTC |