9.5. Error handling with eval

Example 9-6. examples/subroutines/eval.pl

#!/usr/bin/perl
use strict;
use warnings;


my $result;
my $x = 19;
my $y = 23;

eval {
    $result = unstable_add_function($x, $y);
    print "unstable done\n";
};
if ($@) {
    chomp $@;
    warn "Exception '$@' received\n";
    $result = slow_but_stable_add($x, $y);
    print "slow done\n";
}

print "Result: $result\n";
    


sub unstable_add_function {
    if (rand() < 0.2) {
        die "broken";
    }
    return $_[0]+$_[1];
}

sub slow_but_stable_add {
    sleep (2);
    return $_[0]+$_[1];
}

If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed