Thursday, December 3, 2015

Perl6 and Hashes

Mostly like Perl 5 hashes, but there are a couple of new tidbits to amuse.

For example, this 100% traditional Perl line works the same as before:

   my %foo = ( a => 1, b => 2 );
   print keys %foo, "\n";

a b

...but you can also do it in this kinesthetically exotic-looking way:

   my %foo = :a(1), :b(2);
   say %foo.keys

a b

And as before, you can use curly braces to get at values (although don't trip over the sigil):

   say %foo{ 'a' };

1

But the bracket-quote mechanism works too.

   say %foo<a>;

1




No comments:

Post a Comment