Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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
[Lady_Aleena]: I'm glad I've sort of learned command line grep so I can find where and how I've used this sub.
[Lady_Aleena]: Having to rewrite some parts of my site for mobile devices is such a pain in the butt.
[shmem]: Lady_Aleena: what editor are you using, on what platform?
[Lady_Aleena]: shmem, geany on Debian jessie.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2017-04-27 12:00 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (505 votes). Check out past polls.