G'day Alexander,
"Another thing is order of evaluation. I don't know by heart if Perl guarantees left-to-right order in this case. I can't find a reason why it should not, ... I would probably compare with %hash=( %oldhash, ...) and consider this harmless."
The thing with "%hash = (%default, %overwrite)" is that, given hashes have no inherent order, it could evaluated in various orders like these (additional parentheses added to highlight the contents of the two hashes).
%hash = ( (a => 1, b => 2), (a => 101, b => 102) ); %hash = ( (a => 1, b => 2), (b => 102, a => 101) ); %hash = ( (b => 2, a => 1), (a => 101, b => 102) ); %hash = ( (b => 2, a => 1), (b => 102, a => 101) );
However, regardless of whatever random order %default and %overwrite present themselves in, the key-value pairs of the two hashes won't become intermixed. For example, this evaluation won't occur:
%hash = ( a => 102, b => 2, a => 1, b => 102 );
Right at the end of "perldata: List value constructors" there's:
"If a key appears more than once in the initializer list of a hash, the last occurrence wins: ..."
So, $_[4] would have been the sole value in the %$self hash.
Anyway, that's all rather academic. The sigils on the keys would have been picked up by strict. A naïve fix to declare those variables beforehand would have ended up with "uninitialized value" warnings. And anyway, as you say, it's very bad style to start with: unpacking @_ with five separate shift operations mixed in with other code is pretty much asking for a hard-to-find bug in the future.
I probably would not have written the code like this at all; however, working with what's there, I would have had &Box called more like "Box($class, $args)", where $args was a hashref with the "x y w h" keys. Alternatively, if stuck with a bad but published interface, I might have changed the implementation to, perhaps, something like:
my $class = shift; my $self = {}; @$self{qw{x y w h}} = @_; bless $self, $class;
— Ken
In reply to Re^3: HollyGame gamekit (almost @ CPAN)
by kcott
in thread HollyGame gamekit (almost @ CPAN)
by holyghost
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |