Oh wise monks,

So, I've inherited some code, and I've been assigned the task of enhancing it in various ways, but I am constrained along various dimensions. In particular, I cannot change the class/package structure, nor how packages and objects are related to one another. And I'm stuck trying to understand what's happening here...

The code here is a minimal example of what I have. The method I'm trying to understand is Q::bar.

package P; sub new { my ($proto) = @_; my $self = {}; bless $self => $proto; } sub foo { print "P: foo\n"; } sub bar { print "P: bar\n"; } package Pprime; @Pprime::ISA = qw(P); sub new { my ($proto) = @_; my $self = {}; bless $self => ref($proto) || $proto; } package Q; sub new { my ($proto) = @_; my $self = {}; bless $self => $proto; } sub bar { my $self = shift; print "Q: bar\n"; $self->SUPER::bar(); } sub test { my $self = shift; $self->foo(); $self->bar(); } package Qprime; @Qprime::ISA = qw(Q Pprime); sub new { my ($proto) = @_; my $self = {}; bless $self => ref($proto) || $proto; } package main; my $obj = Qprime->new(); $obj->test();

Running this script yields the following output:

P: foo Q: bar Can't locate object method "bar" via package "Q" (perhaps you forgot t +o load "Q"?) at ./demo.pl line 40.

Now as it turns out, I can get around the problem because I'm able to rename Q::bar, at which point the calls to P::foo and P::bar act the same. But I just don't understand why the SUPER doesn't work.

I appreciate any wisdom you can shed on the issue.
--roundboy


In reply to Weird inheritance and SUPER by roundboy

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.