Hello fellow monks, I've been Perl'ing for a long time and maybe I just haven't consumed sufficient caffeine today, but... I am at a loss. I have two hashes:
%A = (a => 1, b => 2, c => 3); %Z = (z => 9, y => 8, x => 7);
and I want to populate an array with all the keys in either. It works great if I do it as two steps:
%combined = (%A, %Z); @all_keys = keys %combined;
No problem. But I want to do it in one step:
@all_keys = keys %{{ %A, %Z }};
that works fine but it's a little extra (and I know my code is soon going to inflicted on a junior dev, so...) and anyway it seems like I ought to be able to do something simpler. So where it blows up is when I try to do:
@all_keys = keys(%A, %Z);
I get

Experimental keys on scalar is now forbidden at - line 3.
Type of arg 1 to keys must be hash or array (not list) at - line 3, near "%Z)"
Execution of - aborted due to compilation errors.

There isn't a scalar in sight, the type of arg1 is definitely a hash, and I am trying to figure out what is going on. I would have expected this just to function as a hash overlay, with the results passed to keys(), wham, bam, eat some jam. OK, maybe keys() is doing something fancy to its args? Functions like pop() know that the first arg is a real array, so maybe keys() is treating this as two args and not doing the overlay (and I've never passed more than one arg to keys()---why would I?---but my various attempts to fool the compiler clarify that the overlay should happen don't work either:
@all_keys = keys((%A, %Z)); @all_keys = keys(%temp::var = (%A, %Z)); @all_keys = keys(my %temp = (%A, %Z)); @all_keys = keys(do { %{{%A, %Z}} });
...not that the last one is "cleaner" that the version above that works

All generate the same error as above, and in fact none but the first work even if I take out the overlay part:
@all_keys = keys(%temp::var = (%A)); @all_keys = keys(my %temp = (%A)); @all_keys = keys(do { %{{%A}} });
What is going on?

In reply to Problem combining hashes by jh

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.