Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Perl 6, defining a hash with interpolated string

by Mr_Micawber (Beadle)
on Aug 04, 2010 at 15:44 UTC ( #852900=perlquestion: print w/replies, xml ) Need Help??
Mr_Micawber has asked for the wisdom of the Perl Monks concerning the following question:

I've been experimenting with using interpolated strings to define hashes. I'm curious why the variation defining %kv below works. There's something about the colon, which I think is a pair constructor (?) and the square brackets that I'm not getting.

#!/usr/bin/perl6 use v6; my ($hkey, $hval ) = <somekey someval>; # simple form: # my %kv = $hkey, $hval; # works my %kv = :[$hkey],$hval; # works # my %kv = :$hkey,$hval; # error # my %kv = :{$hkey},$hval; # _block83 someval say %kv; # somekey someval

Replies are listed 'Best First'.
Re: Perl 6, defining a hash with interpolated string
by molecules (Monk) on Aug 04, 2010 at 17:43 UTC
    I'm not really sure, but it seems that the colon is effectively ignored in that context. Try the following:
    use v6; my $item = :[:[:[$hkey]]]; say $item.perl; # outputs [[["somekey"]]]

      I think I was looking for the interpolating analog of this model for specifying a literal hash pair:

      %kv = :LiteralKey<LiteralValue>
        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"}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://852900]
Approved by ikegami
Front-paged by ikegami
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-04-22 20:40 GMT
Find Nodes?
    Voting Booth?