19. use Moose; package Person; use Moose; # now it's a Moose class! # imports strict and warnings ... no Moose ; __PACKAGE__->meta->make_immutable ;
20. Moose attributes has id => ( is => 'ro' , isa => 'Int' , ); has name => ( is => 'rw' , isa => 'Str' , ); my $person = new Person(id => 123, name => ' John '); $person->name('peter'); # OK $person->id(12); # NOK
24. Define your types use Moose::Util::TypeConstraints; subtype 'Telephone' => as 'Str' => where { $_ =~ /^d+$/}; no Moose::Util::TypeConstraints; has telephone => ( is => 'rw', isa => 'Telephone' , ); $person->telephone('35196219374'); # OK $person->telephone('XPTO'); # NOK
25. Moose class Inheritance package User; use Moose; extends 'Person'; has login => ( is => 'rw', isa => 'Str', ); ... my $user = new User(id => 52, login => 'oleber')
42. It is better to die() than to return() in failure. (Paul Fenwick) The Box Jellyfish Saltwater Crocodile Blue Ring Octopus Stone fish Red Back Spider Funnel Web Spider
43. autodie open(my $FH, ">>", $path) or die "Can't open $!"; system($cmd) == 0 or die "system failed: $?"; if ( defined ( my $pid = fork() ) ) { if ( $pid != 0 ) { # parent } else { # child } } else { die "Can't fork"; }
44. autodie use autodie qw(:all); open(my $FH, ">>", $path) or die "Can't open $!"; system($cmd) == 0 or die "system failed: $?"; if ( defined ( my $pid = fork() ) ) { if ( $pid != 0 ) { # parent } else { # child } } else { die "Can't fork"; }
45. autodie use autodie qw(:all); open(my $FH, ">>", $path); system($cmd) == 0 or die "system failed: $?"; if ( defined ( my $pid = fork() ) ) { if ( $pid != 0 ) { # parent } else { # child } } else { die "Can't fork"; }
46. autodie use autodie qw(:all); open(my $FH, ">>", $path); system($cmd); if ( defined ( my $pid = fork() ) ) { if ( $pid != 0 ) { # parent } else { # child } } else { die "Can't fork"; }
47. autodie use autodie qw(:all); open(my $FH, ">>", $path); system($cmd); if ( ( my $pid = fork() ) != 0 ) { # parent } else { # child }
48. autodie error analyse eval { use autodie; open(my $FH, '<', $some_file); while ( my $line = readline $FH ) { ... } close($fh); }; given ($@) { when (undef) { say "No error"; } when ('open') { say "Error from open"; } when (':io') { say "Non-open, IO error."; } when (':all') { say "autodie error." } default { say "Not an autodie error." } }
65. Test::Simple use Test::Simple tests => 2; ok ( 1 + 1 == 2, "sum" ); ok ( 2 * 3 == 5, "multiplication"); prints: 1..2 ok 1 - sum not ok 2 - multiplication # Failed test 'multiplication' # at script.t line 6. # Looks like you failed 1 test of 2.
66. Test::More Complex stuff use Test::More 'no_plan'; ok ( 1 + 1 == 2, "sum" ); is ( 1 + 1, 2, 'add'); isnt ( 2 * 3, 5, 'multiplication'); is_deeply ({1..4}, {1=>2, 3=>4}, "similar hash"); my $str = 'Hello, World!'; like ($str, qr/Hello/, 'match'); unlike ($str, qr/Dude/, 'No way Dude'); use_ok ('Person'); can_ok ('Person', 'name'); ...