Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

Comment on Perl 6, defining a hash with interpolated string
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://852900]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-08-02 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (55 votes), past polls