The problem is, when I run 'test.pl', I get this:
$ ./test.pl score: 0 vx: -1 symbol: _
And this is a problem why? What were you expecting? :-) Perhaps you wanted to see this:
shown: 1 score: 0 x: 0 vx: -1 y: 0 vy: 0 symbol: _
If it is then I can explain whats going on. As the Anonymous Monk stated SUPER has special meaning inside the class it is used. It is irrelevant what the actual class of the object it is being called against (yes this was an implication that suprised me a little). I dont really see why you are attempting to merge the hashes in init() and not in defaults(). Anyway here is the code I have (minor mods to your stuff, you'll need to uncomment the use() clauses if you dont put it all into one file like I did.

#!/usr/local/bin/perl package Object; use strict; sub new { my ($proto, %args) = @_; my $class = ref $proto || $proto; my $self = bless {}, $class; $self->init(\%args); return $self; } sub init { my ($self, $args) = @_; my $defaults=$self->defaults; if (ref($defaults)) { # This is technically Ok because you are getting # defualts from yourself and accordingly the key=>values # should be correct @{$self}{keys %$defaults}=@{$defaults}{keys %$defaults}; } # If %args are given to new(), use those instead # Unfortunately this is technically bad practice, as the # key=>values will not be coming from yourself (in an abstract sen +se) # accordingly you really should implement code to determine if # the passed keys are in the set of allowed keys. # one way to do this is to make method calls for all legal attribu +tes # and then use the keyname as the methodname. If the arg is not le +gal # perl will throw a runtime error that the method is not found # leaving it as is is asking for hard to identify bugs. # something like this might be in order: # $self->$_($args->{$_}) foreach keys %$args; @{$self}{keys %$args}=@{$args}{keys %$args}; return $self; } 1; package Mover; #use Object; our @ISA = qw(Object); use strict; sub defaults { my $self=shift; 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; package Player; #use Mover; use strict; our @ISA = qw(Mover); sub defaults { my $self=shift; my $defaults =$self->SUPER::defaults(); @{$defaults}{'symbol','score'}=('_',0); return $defaults; } 1; use strict; use warnings; #use Player; my $player = Player->new('vx' => -1); for (keys %$player) { print "$_: ", $player->{$_}, $/; } 1;
A few additional points. If you post this type of thing again please post it int the order I have given. Base class first, with the heirarchy going down (remember a class heirarchy is a tree, and computer geeks draw trees upside down, with the root at the top :-). I also made a point about error checking, for more info please review the posts about this subject in the thread passing subroutine arguments directly into a hash especially the comments inRe: passing subroutine arguments directly into a hash and Re (tilly) 2: passing subroutine arguments directly into a hash. If you have more questions on the subject no doubt Tilly knows of a few more posts on the subject. :-)

HTH

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)


In reply to Re: can('SUPER::defaults') by demerphq
in thread 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.