jsadusk has asked for the wisdom of the Perl Monks concerning the following question:
Which inherites from a base class:package DerivedClass; use strict; use base qw(BaseClass); use ImportClass qw(blah); sub new { return bless {}; } 1;
And uses another module to generate a method:package BaseClass; sub blah { return "blah"; } 1;
And then I want to call that generated method:package ImportClass; use strict; use NEXT; sub import { my $package = shift; my $methodname = shift; my $dest = caller; my $method = sub { return shift->SUPER::blah(); }; my $symbol = $dest . '::' . $methodname; { no strict 'refs'; *$symbol = $method; } } 1;
#!/usr/bin/perl use DerivedClass; my $object = DerivedClass->new(); print $object->blah() . "\n";
Obviously this is a simplified version of something actually useful I'm trying to accomplish.
If I run this, I'll get the error:
As SUPER is trying to look at the package in which it's defined. I thought the NEXT module would fix this, but if I change that SUPER to NEXT I get:Can't locate object method "blah" via package "ImportClass" at ImportC +lass.pm line 10.
Can't call NEXT::blah from ImportClass::__ANON__ at ImportClass.pm lin +e 10
I thought the point of NEXT was to do runtime resolution of SUPER dispatch, but it doesn't seem to like this because it is looking at the method where it was declared, not at where it was imported to. Is there any way to massage NEXT into doing what I want? Is there another way of achieving this?
Thanks for any help anyone can give.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: SUPER or NEXT from imported methods
by tilly (Archbishop) on Feb 20, 2009 at 22:59 UTC | |
by jsadusk (Acolyte) on Feb 20, 2009 at 23:24 UTC | |
by tilly (Archbishop) on Feb 21, 2009 at 02:04 UTC | |
by jsadusk (Acolyte) on Feb 21, 2009 at 03:35 UTC | |
|
Re: SUPER or NEXT from imported methods
by chromatic (Archbishop) on Feb 21, 2009 at 05:34 UTC | |
by tye (Sage) on Feb 21, 2009 at 06:38 UTC | |
by tilly (Archbishop) on Feb 21, 2009 at 08:45 UTC | |
|
Re: SUPER or NEXT from imported methods
by Bloodnok (Vicar) on Feb 21, 2009 at 12:32 UTC |