Perl exceptions instead of return values

In some perl scripts, I find myself writing things like:

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

and etc.

I would like to avoid repeating myself by saying or die ... at the end of any possible exception.

Is there a way to force functions like open() , etc., to throw an exception when they fail, and not just a false return value? Then I could catch all the possible exceptions at a time.

+7
source share
1 answer

Yes. It contains a module that causes these commands to die on error; it is called autodie . Add it to the top of the script.

 use autodie; ## It dies. open my $fh, '<', 'nonfile.txt'; 
+13
source

All Articles