Hi, Perl Monks. I have this in file 'test.pl':

#!/usr/local/bin/perl use strict; use warnings; use Player; my $player = Player->new('vx' => -1); for (keys %$player) { print "$_: ", $player->{$_}, $/; }

which creates an object from Player.pm:

package Player; use Mover; @Player::ISA = qw(Mover); use strict; sub defaults { my $defaults = { 'symbol' => '_', 'score' => 0, } } 1;

which inherits from Mover.pm:

package Mover; use Object; @Mover::ISA = qw(Object); use strict; sub defaults { my $defaults = { 'symbol' => '?', # override this in subclasses 'x' => 0, # x-coordinate 'y' => 0, # y-coordinate 'vx' => 0, # velocity in x-direction 'vy' => 0, # velocity in y-direction 'shown' => 1, # still alive? } } 1;

which in turn inherits from Object.pm:

package Object; use strict; sub new { my ($caller, %args) = @_; my ($self, $class); $class = ref $caller; $class ||= $caller; $self = bless {}, $class; $self->init(\%args); return $self; } sub init { my ($self, $args) = @_; my ($defaults); # See if superclass has defaults if ($self->can('SUPER::defaults')) { $defaults = $self->SUPER::defaults(); } # Let current class override superclass if ($self->can('defaults')) { my $current_defaults = $self->defaults(); for my $attr (keys %$current_defaults) { $defaults->{$attr} = $current_defaults->{$attr}; } } # Set default values if (ref($defaults) eq 'HASH') { for my $attr (keys %$defaults) { $self->{$attr} = $defaults->{$attr}; } } # If %args are given to new(), use those instead for my $attr (keys %$args) { $self->{$attr} = $args->{$attr}; } } 1;

That's the subset of code that I've been using to try to isolate the problem. It's meant to be able to 1) inherit basic default "instance data" values from Mover.pm, 2) override some of those values in Player.pm, and finally 3) override both of those from the arguments passed to Player->new(). That's all done in the init() method of Object.pm.

The problem is, when I run 'test.pl', I get this:

$ ./test.pl score: 0 vx: -1 symbol: _

That is, I only get 2) Player.pm defaults() and 3) new() args, while Mover.pm defaults() is never called. Thus, $self->can('SUPER::defaults') doesn't return "true". I've tried a very simple test case, and I found that the syntax of $self->can('SUPER::defaults') is correct. I can't figure out why it's not working in the code above, and I'm just going around in circles at this point. Please help?


In reply to can('SUPER::defaults') by kwoff

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.