On Tue, 21 Feb 2017 05:15:56 -0800, [email protected] wrote:
> EXAMPLE:
> my %hash = foo => 1;
> if %hash<a>:exists {
> say "not gonna print this";
> }
> my $key = 'b';
> if %hash«$key»:exists {
> say "why i'm here";
> }
>
> OUTPUT:
> why i'm here
>
> EXPECTED RESULT:
> (Should not print anything)
>
> FROM #perl6 CHANNEL:
> 14:09 < masak> m: my %hash = foo => 1; my $key = 'b'; if
> %hash«$key»:exists { say "why i'm here" }; say "alive"
> 14:09 <+camelia> rakudo-moar 80e0bc: OUTPUT: «why i'm herealive»
> 14:09 < masak> huh.
> 14:10 < masak> I... I call bug.
> 14:10 < masak> m: my %hash = foo => 1; my $key = 'b'; if
> %hash«$key»:exists { say "why i'm here" }; say %hash.perl
> 14:10 <+camelia> rakudo-moar 80e0bc: OUTPUT: «why i'm here{:foo(1)}»
>
> VERSION INFORMATION
> $ perl6 -v
> This is Rakudo version 2017.01 built on MoarVM version 2017.01
> implementing Perl 6.c.
>
> $ uname -a
> Linux xprs 4.9.9-1-macbook #1 SMP PREEMPT Mon Feb 13 17:07:28 EET 2017
> x86_64 GNU/Linux
>
> Best regards,
>
>
> --
> Jarkko
Thank you for the report. There was a discussion[^1] on the topic later on that
clarified what
is happening in this particular case, and the behaviour is not a bug.
The interpolation syntax you're using doesn't just interpolate the variable's
contents, but also splits them up on whitespace (makes sense; it's as if the
variable's content was
written in «...» directly):
<Zoffix> m: my $x = 'a b'; dd «$x»
<camelia> rakudo-moar 80e0bc: OUTPUT: «slip("a", "b")»
The result is a slip, so that at the end you end up with a nice flat list:
<Zoffix> m: my $x = 'a b'; dd «$x $x»
<camelia> rakudo-moar 80e0bc: OUTPUT: «("a", "b", "a", "b")»
You always get a slip from just «$x», regardless of whether it ends up
as just one or multiple elements:
<Zoffix> m: my $x = 'a'; dd «$x»
<camelia> rakudo-moar 80e0bc: OUTPUT: «slip("a",)»
<Zoffix> m: my $x = 'a b'; dd «$x»
<camelia> rakudo-moar 80e0bc: OUTPUT: «slip("a", "b")»
And if you do meant for the variable to be interpolated as just
one item, then use quotation marks; and in that case it does end up as on Str:
<Zoffix> m: my $x = 'a b'; dd «"$x"»
<camelia> rakudo-moar 80e0bc: OUTPUT: «"a b"»
So now, if we circle back to the original code:
%hash«$key»:exists { say "why i'm here" };
It will always succeed, as long as $key is not an empty string, because you're
always
giving a slice to lookup and getting a list back. And were different semantics
used, you'd
end up with a case where %hash«$key»:exists would tell you whether or not a key
existed,
**but only if that key did not have any whitespace in it**; if it does, then
the answer would
be always true.
The correct syntax you're looking for is:
%hash{$key}:exists { say "why i'm here" };
That way you're passing whatever is in $key literally; so this is the way to
refer to object
hashes as well:
<Zoffix> m: my %h := :{ 42 => 'foo' }; say %h{42} # make a Hash with Int
keys, give Int when looking up key
<camelia> rakudo-moar 80e0bc: OUTPUT: «foo»
<Zoffix> m: my %h := :{ 42 => 'foo' }; say %h<42> # lookup fails, because
we're giving a Str, not Int, key
<camelia> rakudo-moar 80e0bc: OUTPUT: «(Any)»
<Zoffix> m: my %h := :{ 42 => 'foo' }; say %h«42» # same difference
<camelia> rakudo-moar 80e0bc: OUTPUT: «(Any)»
Cheers,
ZZ
[1] https://irclog.perlgeek.de/perl6/2017-02-21#i_14137505