Modern Perl

Peter Makholm

Copenhagen.pm

New features in Perl 5.10


use feature ':5.10';

given($_) {
    when (/^abc/) { $abc = 1; }
    when (/^def/) { $def = 1; }
    when (/^xyz/) { $xyz = 1; }
    default { $nothing = 1; }
}

say "Hello world!";

sub count {
   state $i = 0;
   return $i++;
}

use Modern::Perl


  use Modern::Perl

Is (at the moment) equivalent to


  use strict;
  use warnings;
  use feature ':5.10';
  use mro 'c3';

local::lib

Easy way to install local librares

$ perl -Mlocal::lib=/tmp/perl
Attempting to create directory /tmp/perl
Attempting to create file /tmp/perl/.modulebuildrc
export MODULEBUILDRC="/tmp/perl/.modulebuildrc"
export PERL_MM_OPT="INSTALL_BASE=/tmp/perl"
export PERL5LIB="/tmp/perl/lib/perl5:/tmp/perl/lib/perl5/i486-linux-gnu-thread-multi:$PERL5LIB"
export PATH="/tmp/perl/bin:$PATH"
$

Devel::REPL

Read, eval, print loop

Die automatically


use autodie; 

open(my $fh, "<", $filename); # No need to check!

Is equivalent to


open(my $fh, "<", $filename)
    or die "Can't open '$filename' for reading: '$!'";

The Klingon Language:

Hey you, program in it!

use Lingua::tlhInganHol::yIghun;

###### mI'wa'DIchvaD 'eratoSHeneS HeghqaD #####

mI'tInwIj wa'vatlh nob!
mI'wa'DIchmeywIj cha' mI'tIn chen nob!
{
        mI'wa'DIchmeyvaD { 'oH gheD chuv! } mI'wa'DIchmey tIwIv yInob!
        gheD <<\n>> ghItlh!
} gheDvaD mI'wa'DIchmeyvaD yInIH yInob teHtaHvIS!

Devel::Declare

  • Adding new keywords to perl
  • deep black magic
  • fancy new syntax...

Moose

  • Modern object system for Perl 5
    • Less trivial code
    • Sort of type constraints
    • Introspection
    • Roles

... simple example


package Point;
use Moose;

has x => (
    is => 'rw',
    isa => 'Int',
    required => 1,
);

has y => (
    is => 'rw',
    isa => 'Int',
    required => 1,
);

... type coercions


package Line;
use Moose;
use Moose::Util::TypeConstraints;
use Point;

coerce 'Point' 
    => from 'ArrayRef[Int]'
        => via { Point->new( x => $_->[0], y => $_->[1] ) };

has start => (
    is => 'rw',
    isa => 'Point',
    coerce => 1,
);
has end => (
    is => 'rw',
    isa => 'Point',
    coerce => 1,
);

... less trivial code


package Polygon;

use Moose;
use Point;

has points => (
    is => 'ro',
    isa => 'ArrayRef[Point]',
    default => sub { [] },
    traits => [ qw(Array) ],
    handles => {
        add_point => 'push',
    },
);

... fancy syntax


use MooseX::Declare;

class PointX {
        has x => (
            is => 'rw',
            isa => 'Int',
            required => 1,
        );
        method add (PointX $add) {
            $self->x( $self->x + $add->x );
            $self->y( $self->y + $add->y );
            return $self;
        }
}

Catalyst

Model/View/control based web frame work

TryCatch


use TryCatch;

sub foo {
    my ($self) = @_;

    try {
        die Some::Class->new(code => 404 ) if $self->not_found;
        return "return value from foo";
    }
    catch (Some::Class $e where { $_->code > 100 } ) {
    }
}