Skip to content

Instantly share code, notes, and snippets.

@jose-neta
Created November 19, 2013 17:14
Show Gist options
  • Save jose-neta/7548869 to your computer and use it in GitHub Desktop.
Save jose-neta/7548869 to your computer and use it in GitHub Desktop.
go or not go to the beach
#! /usr/bin/env perl
use strict;
use warnings;
use feature 'say';
{
package Beach;
use Moo;
use Try::Tiny;
use Carp;
sub go {
# Eventually raises an exception.
die "It's raining! Stopped" if int( rand 2 );
return "GO!";
};
sub not_go {
my $self = shift;
my %args = @_;
my $reason = $args{ reason };
return
sprintf( "KO because: %s ",
( $reason ? $reason : 'dunno' ) );
} ## end sub not_go
around 'go' => sub {
my $orig = shift;
my $self = shift;
try {
$self->$orig();
}
catch {
chomp; # avoid line break of $_
return $self->not_go( reason => $_ );
# use 'return' to make your program continue besides the exception.
# otherwise use 'croak'
};
};
1;
}
my $res;
my $b = new Beach();
for ( 1..10 ) {
say $b->go();
}
$b->zebruh();
print "I'm still alive!";
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment