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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.