Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: Perl 6, defining a hash with interpolated string

by Mr_Micawber (Beadle)
on Aug 04, 2010 at 21:23 UTC ( #852975=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^4: Perl 6, defining a hash with interpolated string
Select or Download Code
Re^5: Perl 6, defining a hash with interpolated string
by molecules (Monk) on Aug 05, 2010 at 11:53 UTC
    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!


        Or to be even more concise, use the notation you showed earlier:
        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'"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://852975]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2015-07-05 19:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls