in reply to can('SUPER::defaults')
I think you can make this work as well as make the code much simpler; others have shown ways to make this work but I didn't think they simplified the code enough :). For example:
package Player; #[...] sub defaults { my $self= shift; return $self->SUPER::defaults(), 'symbol' => '_', 'score' => 0, } #[...] package Mover; #[...] sub defaults { my $self= shift; return $self->SUPER::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? } #[...] package Object; #[...] sub defaults { return; } sub init { my ($self, $args) = @_; my ($defaults)= { $self->SUPER::defaults() }; @{$self}{keys %$defaults}= values %$defaults; # If %args are given to new(), use those instead @{$self}{keys %$args}= values %$args; }
By having an empty defaults() method in the base class, you don't have to call can(). By returning lists rather than references to hashes, you make merging while giving the inherited class precendence trivial (normally I avoid returning lists because I want to allow for cases where the list becomes really huge, but an object with a really huge list of default attributes seems unwise so the code simplicity seems like a net win).
Also, I didn't like your idea of using my and not return when what your routine was doing was just returning a value.
BTW, I applaud your technique of putting extra code in the base class to make the writing of defaults() methods as simple as possible. Unfortunately, the amount of additional code that would be required to make that actually work doesn't seem like a good trade-off to me; which would be basically rewriting can().
- tye (but my friends call me "Tye")
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (tye)Re: can('.. Thinking outside the box.
by demerphq (Chancellor) on Oct 24, 2001 at 02:48 UTC |