Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl exceptions instead of return values

In some perl scripts I find myself writing things like:

open(...) or die $!;
print ... or die $!;

etc.

I'd like to avoid repeating myself saying or die ... at the end of every possible exception.

Is there a way to force functions like open() etc to throw an exception when they error, not just a false return value? Then I could catch all possible exceptions in one go.

like image 720
Clinton Avatar asked Mar 04 '13 23:03

Clinton


1 Answers

Yes. It exists a module that makes those instructions die on an error, it's called autodie. Add it at the beginning of your script.

use autodie;

## It dies.
open my $fh, '<', 'nonfile.txt';
like image 113
Birei Avatar answered Sep 21 '22 08:09

Birei