http://www.perlmonks.org?node_id=852965


in reply to Re^2: Perl 6, defining a hash with interpolated string
in thread Perl 6, defining a hash with interpolated string

The last two of the following examples give you interpolation, but it is more of the "normal" interpolation you would expect in Perl.
use v6; my %kv1 = :LiteralKey<LiteralValue>; say :%kv1.perl; my %kv2 = <LiteralKey>,<LiteralValue>; say :%kv2.perl; my $var3 = 'Literal'; my %kv3 = "{$var3}key",'LiteralValue'; say :%kv3.perl; my $var4 = 'LiteralKey'; my %kv4 = "$var4",'LiteralValue'; say :%kv4.perl;
gives you
"kv1" => {"LiteralKey" => "LiteralValue"} "kv2" => {"LiteralKey" => "LiteralValue"} "kv3" => {"Literalkey" => "LiteralValue"} "kv4" => {"LiteralKey" => "LiteralValue"}

Replies are listed 'Best First'.
Re^4: Perl 6, defining a hash with interpolated string
by Mr_Micawber (Beadle) on Aug 04, 2010 at 21:23 UTC

    I noticed playing with this code that the output of .perl differs depending on if the colon is in front of the variable:

    say :%kv4.perl; #"kv1" => {"LiteralKey" => "LiteralValue"} say %kv4.perl; #{"LiteralKey" => "LiteralValue"}

    the colon seems to be plucking the Hash's name and creating a Pair with it.

      Yes that is correct.
      my $name='Larry'; my $pair = :$name; say $pair.perl;
      Outputs:
      "name" => "Larry"


      UPDATE:
      This is especially useful for named parameters in subroutine declarations.

      For example:
      use v6; foo( B => 'there',A => 'howdy'); sub foo(Str :$A, Str :$B){ say "Named parameter A is '$A'"; say "Named parameter B is '$B'"; }
        Thanks, that clears a lot up!