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
Is (at the moment) equivalent to
use strict;
use warnings;
use feature ':5.10';
use mro 'c3';
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"
$
Read, eval, print loop
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: '$!'";
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!
package Point;
use Moose;
has x => (
is => 'rw',
isa => 'Int',
required => 1,
);
has y => (
is => 'rw',
isa => 'Int',
required => 1,
);
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,
);
package Polygon;
use Moose;
use Point;
has points => (
is => 'ro',
isa => 'ArrayRef[Point]',
default => sub { [] },
traits => [ qw(Array) ],
handles => {
add_point => 'push',
},
);
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;
}
}
Model/View/control based web frame work
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 } ) {
}
}