Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: HollyGame gamekit (almost @ CPAN)

by kcott (Archbishop)
on Oct 17, 2017 at 06:47 UTC ( [id://1201479]=note: print w/replies, xml ) Need Help??


in reply to Re^2: HollyGame gamekit (almost @ CPAN)
in thread HollyGame gamekit (almost @ CPAN)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2024-04-16 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found