Language Update: Perl

by @dankogai

Perl ™

Perl ™
by テラインターナショナル!?

商願2007-92708

Perl ™:Don't Panic!

Language Update: Perl 6

FizzBuzz in perl 5

print $_ % 15
  ? $_ % 5
      ? $_ % 3
          ? $_
          : 'Fizz'
      : "Buzz"
  : q{FizzBuzz}, "\n"
  for ( 1 .. 30 );

LLEval

http://colabv6.dan.co.jp/lleval.html
PerlBoth 5 and 6
PHP5
Python2; not 3
Ruby1.8; not 1.9
ClojureSorry.
HTML5THIS SLIDE
ScalaSorry.

FizzBuzz in Perl 6

say $_ % 15
  ?? $_ % 5
      ?? $_ % 3
          ?? $_
          !! 'Fizz'
      !! "Buzz"
  !! q{FizzBuzz}
  for 0 ^..^ 31;

Rakudo *

http://rakudo.org/announce/rakudo-star/2010.07
On behalf of the Rakudo and Perl 6 development teams, I'm happy to announce the July 2010 release of "Rakudo Star", a useful and usable distribution of Perl 6. The tarball for the July 2010 release is available from http://github.com/rakudo/star/downloads.

Rakudo *:Building

lwp-download \
  http://github.com/downloads/rakudo/star/rakudo-star-2010.07.tar.gz
tar zxvf rakudo-star-2010.07.tar.gz
cd rakudo-star-2010.07
cd rakudo
perl Configure.pl --gen-parrot
make
make install # ../install/bin/perl6

FizzBuzz in Perl 6

sub fizz($n) { $n % 3 ?? '' !! 'Fizz' }
sub buzz($n) { $n % 5 ?? '' !! 'Buzz' }
sub fizzbuzz($n) { fizz($n) ~ buzz($n) || $n };
say fizzbuzz($_) for 1..30;

FizzBuzz in Perl 6

sub fizzbuzz($n) { 
    sub fizz { $^n % 3 ?? '' !! 'Fizz' }
    sub buzz { $^n % 5 ?? '' !! 'Buzz' }
    fizz($n) ~ buzz($n) || $n;
}
say fizzbuzz($_) for 1..30;

Scope in Perl 6

sub fizzbuzz($n) { 
    sub fizz { $^n % 3 ?? '' !! 'Fizz' }
    sub buzz { $^n % 5 ?? '' !! 'Buzz' }
    fizz($n) ~ buzz($n) || $n;
}
say fizz(3);

Placeholder in Perl 6

sub fizz-buzz($n) { 
    my &fizz = { $^n % 3 ?? '' !! 'Fizz' };
    my &buzz = { $^n % 5 ?? '' !! 'Buzz' };
    fizz($n) ~ buzz($n) || $n;
}
say fizz-buzz($_) for 1..30;

Exception in Perl 6

sub fizz-buzz($n) { 
    my &fizz = { $^n % 3 ?? '' !! 'Fizz' };
    my &buzz = { $^n % 5 ?? '' !! 'Buzz' };
    fizz($n) ~ buzz($n) || $n;
}
try {
  say fizzbuzz(15);
  say fizz(3);
  CATCH{
    say $!
  }
}

Exception in Perl 6

sub fizz-buzz($n) { 
    my &fizz = { $^n % 3 ?? '' !! 'Fizz' };
    my &buzz = { $^n % 5 ?? '' !! 'Buzz' };
    fizz($n) ~ buzz($n) || $n;
}
my $err = {
  say fizz(3);
}();
say $err.exception;

Lazy list in Perl 6

sub fizz-buzz($n) { 
    my &fizz = { $^n % 3 ?? '' !! 'Fizz' };
    my &buzz = { $^n % 5 ?? '' !! 'Buzz' };
    fizz($n) ~ buzz($n) || $n;
}
my @int = 0..*;
say fizz-buzz(@int[15]);

Smart Match in Perl 6

sub fizz-buzz($n) { 
    my &fizz = { $^n % 3 ?? '' !! 'Fizz' };
    my &buzz = { $^n % 5 ?? '' !! 'Buzz' };
    fizz($n) ~ buzz($n) || $n;
}
say fizz-buzz(15) ~~ any(<Fizz Buzz FizzBuzz>);

Multi dispatch in Perl 6

sub fizz-buzz($n) { 
    my &fizz = { $^n % 3 ?? '' !! 'Fizz' };
    my &buzz = { $^n % 5 ?? '' !! 'Buzz' };
    fizz($n) ~ buzz($n) || $n;
}
multi sub triple(Int $i) { $i * 3 }
multi sub triple(Str $s) { $s ~ $s ~ $s }

say triple fizz-buzz triple 5;

(Hyper)Operator in Perl 6

my &fizz = { $^n % 3 ?? $^n !! 'Fizz' };
my &buzz = { $^n % 5 ?? $^n !! 'Buzz' };

sub fizzbuzz($f, $b){
  $f ~~ Int ?? $b ~~ Int ?? $f !! $b
            !! $b ~~ Int ?? $f !! $f~$b;
}
sub infix:<!>($f,$b){ fizzbuzz($f,$b) }

say fizz(1) ! buzz(1);
say fizz(3) ! buzz(3);
say fizz(5) ! buzz(5);
say fizz(15) ! buzz(15);
#say (1..15).map({fizz($_)}) >>!<< (1..15).map({buzz($_)});

OO in Perl 6

class Fizz {
  has $.n;
  method fizz () { $.n % 3 ?? '' !! 'Fizz' }
}
class Buzz is Fizz {
  method buzz () { $.n % 5 ?? '' !! 'Buzz' }
}
class FizzBuzz is Buzz {
  method fizzbuzz () { self.fizz ~ self.buzz || $.n }
}
say FizzBuzz.new(n => $_).fizzbuzz for 1..15;

OO in Perl 6

class Fizz {
  has $.n;
  method fizz () { $.n % 3 ?? '' !! 'Fizz' }
}
class Buzz is Fizz {
  method buzz () { $.n % 5 ?? '' !! 'Buzz' }
}
class FizzBuzz is Buzz {
  method fizzbuzz () { self.fizz ~ self.buzz || $.n }
}

for [1..15].map({FizzBuzz.new(n => $_)}) {
  say .fizzbuzz;
}

Grammer in Perl 6

http://d.hatena.ne.jp/uasi/20100730/1280472169
grammar CSV {
    regex TOP   { ^ <line> ** \n <empty_line>? $ }
    regex line  { <value> ** ',' }
    regex value {
        | <pure_text>
        | \s* \" <quoted_contents> \" \s*
    }
    regex quoted_contents { <pure_text> ** [ <[,]> | \s | '""' ] }
    regex pure_text       { [<!before <[",]>> \N]+ }
    regex empty_line      { \h* \n }
}
class Actions {
    method TOP($/)   { make $<line>.map: *.ast }
    method line($/)  { make $<value>.map: *.ast }
    method value($/) { make $<pure_text>.chars ?? ~$<pure_text> !! ~$<quoted_contents> }
}
my $csv = 'foo,bar
"quoted
value", "..."';
my $match = CSV.parse($csv, actions => Actions);
say $match.ast.perl;  # (("foo", "bar"), ("quoted\nvalue", "..."))

More Info:

Perl 6
http://perl6.org/
Rakudo
http://rakudo.org/

御礼なう

for @questions {
  .answer;
}