prev Translate Page next

Perl? Which Perl?

http://www.dan.co.jp/~dankogai/
yapcasia2009/perls.html

by Dan the Perl Monger

FYI

All Sample codes are tested on LLEval

Perls

Perl 4

Perl 4

<?php
 print "0xF00" == 3840
?>
perl.com: Programming is Hard, Let's Go Scripting...
By and large PHP seems to be making the same progression of mistakes as early Perl did, only slower.

Perl 4: Forget it!

Perls, practically

Perl 5.6.x

Perl 5.6.x

Perl 5.6.x

Perl 5.6.x: Time to upgrade!

Perl 5.8.x

Perl 5.8.x

Perl 5.8.x

Perl 5.8.x: for Conservative Perl Mongers

Perl 5.10.x

Perl 5.10.x

Perl 5.10.x: for Contemporary Perl Mongers

Perl 6

Which Perl6?

Pugs

Rakudo

http://rakudo.org/how-to-get-rakudo
git clone git://github.com/rakudo/rakudo.git
cd rakudo
perl Configure.pl --gen-parrot
make
make install

Perl5 or Perl6?

Modern Perls vs. Postmodern Perl

$S@i%g*i&l\s

$Perl5 eq $Perl6

my $scalar = 42;
my @array  = (1, 2, 3);
my %hash   = (one => 1, two => 2);
print $scalar, "\n";
print @array, "\n";
print %hash, "\n";

$Perl5 ne $Perl6

my @array  = (1, 2, 3);
my %hash   = (one => 1, two => 2);
print $array[0], "\n";
print $hash{one}, "\n";

$Perl6 ne $Perl5

my @array  = (1, 2, 3);
my %hash   = (one => 1, two => 2);
print @array[0], "\n";
print %hash<one>, "\n";

$Perl6 ne $Perl5

my $aref  = [1, 2, 3];
my $href   = { one => 1, two => 2 };
print $aref[0], "\n";
print $href<one>, "\n";
On Perl6, there are still @arrays and %hashes but you are more likely to use $arrayref and $hashref

Object Orientation

Perl5

#!/usr/bin/perl
package Point;
sub new {
  my $class = shift;
  my $self = bless {}, $class;
  my %arg = @_;
  $self->$_($arg{$_}) for keys %arg;
  $self;
}

sub x { 
  my $self = shift;
  $self->{x} = shift if @_;
  $self->{x}
}

sub y { 
  my $self = shift;
  $self->{y} = shift if @_;
  $self->{y}
}

package Point3D;
use base 'Point';
sub z { 
  my $self = shift;
  $self->{z} = shift if @_;
  $self->{z}
}

package main;
my $p = Point3D->new(x => 1, y => 2, z => 3);
local $, = ", ";
print $p->x, $p->y, $p->z;

Perl 6

class Point {
  has $.x;
  has $.y;
}
class Point3D is Point {
  has $.z;
}
my $a = Point3D.new(x => 1, y => 2, z => 3);
say [$a.x, $a.y, $a.z];

Perl 5 + Moose

#!/usr/bin/perl
package Point;
use Moose;
has 'x' => (is => 'rw');
has 'y' => (is => 'rw');

package Point3D;
use Moose;
extends 'Point';
has 'z' => (is => 'rw');

package main;
my $p = Point3D->new(x => 1, y => 2, z => 3);
local $, = ", ";
print $p->x, $p->y, $p->z;

Anonymous Class -- Perl6

my $c3  = class { has $.x; has $.y; has $.z };
my $q = $c3.new(x => 1, y => 2, z => 3);
say [$q.x, $q.y, $q.z];

Anonymous Class -- Perl5 + Moose

use MooseX::Declare; # Not included in Moose

my $class = class {
  has 'x' => ( is => 'rw');
  has 'y' => ( is => 'rw');
  has 'z' => ( is => 'rw');
};

my $q = $class->new_object(x => 1, y => 2, z => 3);
local $, = ", ";
print $q->x, $q->y, $q->z;

Truly Anonymous Class -- Perl6

my $r = (class{
  has $.x; has $.y; has $.z
}).new(x => 1, y => 2, z => 3);
say [$r.x, $r.y, $r.z];

Truly Anonymous Class -- Perl5 + Moose

use MooseX::Declare; # Danke, rafl!

my $r = (class {
  has 'x' => ( is => 'rw');
  has 'y' => ( is => 'rw');
  has 'z' => ( is => 'rw');
})->new_object(x => 1, y => 2, z => 3);

local $, = ", ";
print $r->x, $r->y, $r->z;
Confession: I had to ask @nothingmuch to come up with solution. Reading POD was not good enough. @nothingmuch is way too much.

Function Orientation (FO)

FO - Perl 5

print sub{ $_[0] * $_[0] }->(10);

FO - Perl 6

say (sub($x){ $x * $x })(10);

FO - JavaScript

print((function(x){ return x * x })(10));
// or alert() on browsers

FO - Perl 6

say ->$x{ $x * $x }(10);

FO - Perl 6

for (1..10) -> $x { say $x * $x };
On Perl 6, every { BLOCK } is a closure.

FO - Perl 6

say { $^x * $^x }(10);

FO - Perl 6

say { $^x ~ $^y ~ $^z }('I', 'love', 'perl');

Anonymous Recursive Function - JS

print((function(n){ 
  return n <= 1 ? n : n * arguments.callee(n-1) 
})(10));

Anonymous Recursive Function - Perl6

say { $^n <= 1 
        ?? $^n 
        !! $^n * &?BLOCK($^n - 1)
}(10);

Anonymous Recursive Function - Perl6

say { $^n <= 1 
        ?? $^n 
        !! $^n * &?BLOCK($^n - 1)
}(10);

Waiting for jnthn to implement &?BLOCK

Reduce Operator - Perl 6

say { [*] (1..$^n) }(10);

Hyper Operator - Perl 6

say [ [1..9] >>*<< [1..9] ];

Hyper Operator - Perl 6

say [ [1..9]>>.sqrt ];

Hyper Operator - Perl 6

say [ [1..9].map({$_ ** 2}) ];

Hyper Operator - Perl 6

say [ [1..9].map:{$_ ** 2} ];

Works on Pugs, not yet on Rakudo

Hyper Operator - Perl 6

say [1..9] >>*<< [1..9];

Junction - Perl 6

my $t = 0;
my $f = 1;
say $t | $f ?? 'true' !! 'false';
say $t & $f ?? 'true' !! 'false';
say $t | $f;
say ($t | $f).perl;

Where're Bitwise Operators?

my $t = 0;
my $f = 1;
say $t |  $f;
say $t +| $f;
say $t &  $f;
say $t +& $f;

Wrap-up

Thank you!

for @question -> $q { $q.answer }