kwoff has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: can('SUPER::defaults')
by chromatic (Archbishop) on Oct 23, 2001 at 09:23 UTC | |
by pike (Monk) on Oct 23, 2001 at 16:14 UTC | |
by demerphq (Chancellor) on Oct 23, 2001 at 16:25 UTC | |
by chromatic (Archbishop) on Oct 23, 2001 at 23:03 UTC | |
by jackdied (Monk) on Oct 24, 2001 at 02:55 UTC | |
|
Re: can('SUPER::defaults')
by demerphq (Chancellor) on Oct 23, 2001 at 14:34 UTC | |
|
(tye)Re: can('SUPER::defaults')
by tye (Sage) on Oct 23, 2001 at 18:18 UTC | |
by demerphq (Chancellor) on Oct 24, 2001 at 02:48 UTC | |
|
Re: can('SUPER::defaults')
by Anonymous Monk on Oct 23, 2001 at 10:15 UTC | |
by chromatic (Archbishop) on Oct 23, 2001 at 23:14 UTC | |
|
Re: can('SUPER::defaults')
by kwoff (Friar) on Oct 23, 2001 at 21:51 UTC |