Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Idiomatic Perl 6 (perl6advent.wordpress.com)
35 points by draegtun on Dec 25, 2011 | hide | past | favorite | 11 comments


Is it me or idiomatic Perl 6 resembles normal Ruby syntax and methods?


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);


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.


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" }


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 :)


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


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


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


I don't know perl. I knew what the first one was doing. I wouldn't know what yours did if I didn't have the context.


I am sorry. I was talking about why _perl_ programer (I am the one) should prefer lambdish syntax over classic one. The way perl functions defined below is pretty standard today:

  sub something {
    my ($a, $b) = @_;
    do_something_with($a, $b);
  }
Parameters in perl functions passed in @_ array. This is also default variable for many internal function, like shift(), which returns first value from array, and shift it to the left by one element. So your function might be defined like this:

  sub something {
    my $a = shift();
    my $b = shift();

    $a + $b;
  }
or:

  print sub { shift() + shift() }->(1,2);
If function do not use return(), last statement result used as return value of the function.

So perl already have everything you need to do lambda-style programing. And I was wondering why new syntax for something already existing. Or I could be wrong if this did not exists before and I am missing something.


because you have an argument list, where you can put lot's of stuff in perl6




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: