On Sun, May 04, 2014 at 10:40:16AM +0100, Philip Hazelden wrote:
: Hi,
: 
: I'm trying to create a one-element array containing a hash. I eventually
: managed to do this from the REPL, but when I create a script containing the
: same commands, it doesn't work.
: 
:     $ cat test.p6
:     my %h = (y => 1, x => 1);
:     say [item %h].elems;
:     say [item %h][0].WHAT;
:     $ perl6 test.p6
:     2
:     (Pair)
:     $ cat test.p6 | perl6
:     > my %h = (y => 1, x => 1);
:     ("x" => 1, "y" => 1).hash
:     > say [item %h].elems;
:     1
:     > say [item %h][0].WHAT;
:     (Hash)
:     >
:     $ perl6 -v
:     This is perl6 version 2014.04 built on parrot 6.1.0 revision 0
: 
: `cat test.p6 | perl6` does what I expect it to, but `perl6 test.p6` seems
: to be flattening the hash into the array despite the 'item'.

item as a list operator is going to put %h into a list context and
flatten it into two pairs before the function ever sees it, so the
2/(Pair) is actually correct behavior. What's really going on in
the other case is that the REPL is somehow losing the contents of
%h between the prompts.  You can show this is what is happening by
putting all three lines on a single line, in which case the REPL
version behaves like the run-from-file version.

: Is this a bug, or am I missing something? (I don't claim much understanding
: of how this *should* work, but getting two different behaviours feels
: wrong.)

Yes, but the bug is facing the other direction.  :-)

: On a whim, I tried to replace 'item %h' with '%h.item', and both methods of
: execution now give the results I was expecting, i.e. "1" and "(Hash)".

That avoids the list flattening before itemizing.  You can also just
write $(%h) or even $%h there to have the same effect.

Larry

Reply via email to