
Idiomatic Perl 6 - draegtun
http://perl6advent.wordpress.com/2011/12/23/day-23-idiomatic-perl-6/
======
gioele
Is it me or idiomatic Perl 6 resembles normal Ruby syntax and methods?

~~~
draegtun
There has been lots of cross pollination between Ruby and Perl (both 5 & 6)
over the years so I'm not surprised!

And its not just an historical thing, for eg. the stabby lambda added to Ruby
1.9 has been part of the Perl6 specs for years (but called _Pointy Blocks_ ) -
<http://perlcabal.org/syn/S06.html#%22Pointy_blocks%22>

    
    
      # Ruby 1.9
      x = -> a, b { a + b }
      puts x.call(1,2)
    
      # Perl6
      my $x = -> $a, $b { $a + $b };
      say $x(1,2);

~~~
hippich
Probably offtopic, but why use "stabby lambda"/"pointy blocks" instead of
functions? like this:

    
    
      my $x = sub { my ($a, $b) = @_; $a + $b };
      print $x->(1,2); 
    

i see this "lambdish" syntax is shorter, but definitily not clearly for
someone from outside.

~~~
draegtun
Perl6 _pointy block_ is just a synonym for a function. So below is exactly
same as my _pointy block_ example...

    
    
      my $x = sub ($a, $b) { $a + $b };
    

And typically with Perl TIMTOWTDI :)

    
    
      my $x = -> { $^a + $^b };
    

Personally I prefer the _pointy block_ especially how it (visually) fits in
with other Perl6 syntax constructs, for eg:

    
    
      for 1..10 -> $odd, $even { say "odd $odd, even $even" }

~~~
hippich
Thank you for clarifying. Last example looks nice and make visual sense :)

Could you point me somewhere or give a name for $^a/$^b? Want to find out how
does it work :)

~~~
draegtun
Generically these are called twigils, ie. it has a second sigil.

This second sigil describes what it does _differently_ to single sigil
variable. For eg. an asterisk denotes a contextual twigil variable...

    
    
      $*IN     # equiv to STDIN in perl5 
      $*OUT    # and STDOUT
    

The ^ twigils denote _placeholder variables_. In Perl5 there is already
implicily defined package variables called $a & $b so that you can do...

    
    
      sort { $a <=> $b } @array;
    

In Perl 6 these are replaced with the ^ twigils so you can do...

    
    
      sort { $^a <=> $^b }, @array;
    

or even...

    
    
      sort { $^x <=> $^y }, @array;
    

ref: <http://perlgeek.de/en/article/5-to-6#post_15> &
<http://perlcabal.org/syn/S06.html#Placeholder_variables>

BTW, Perl6 Placeholders have been ported back to Perl5 -
<https://metacpan.org/module/Perl6::Placeholders>

~~~
hippich
Thank you once again for detailed explanation. :) I am perl5 developer, but
trying to keep an eye on all developments in perl6 :)

~~~
draegtun
You're very welcome. I'm also a Perl5 developer and love playing with Perl6
(Rakudo) in my spare time.

