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
    Well, the list approach is what I would consider to be 'thinking outside of the box'. I see a hash and I think how to manipulate it. But as you have shown what I really should have been thinking is 'do you even need to pass around a hash at all?'. Also, Im not really sure why, but even though I know the list to hash idea, and use it occasionally, I almost never use it to kill duplicates, another example of getting stuck inside the box. :-) Again on a similar note I must must must remember that wonderful keyword values(). I use keys() all the time, but almost never use values().

    What all this comes to is one of the most fascinating things about Perl. Even though you know ten ways to do things, and do them regularly, theres alway a command, module, or tactic (for lack of a better word) lurking off to the side of box. Occasionally you notice the item on your own, either through research or luck, but more often because someone elses box contained the item center stage. Which brings me to perlmonks. This is the place where you are practically guaranteed to learn something new, every day. And thats cause there are a bunch of people here who seem to think outside the box pretty often.

    Thanks for the lesson Tye, chromatic (umm, everyone else as well :-) and good question kwof.

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