Here is my scenario. I have a package that imports methods into another package. Those methods being imported need to be able to call the SUPER class of the package they're being imported into. For example...

I have a simple derived class:
package DerivedClass; use strict; use base qw(BaseClass); use ImportClass qw(blah); sub new { return bless {}; } 1;
Which inherites from a base class:
package BaseClass; sub blah { return "blah"; } 1;
And uses another module to generate a 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;
And then I want to call that generated method:
#!/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:

Can't locate object method "blah" via package "ImportClass" at ImportC +lass.pm line 10.
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 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.

In reply to SUPER or NEXT from imported methods by jsadusk

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.