Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Too many "or die" clauses?

by pjf (Curate)
on Sep 09, 2008 at 14:22 UTC ( #710099=note: print w/replies, xml ) Need Help??

in reply to Too many "or die" clauses?

autodie has already been mentioned, but I don't think it's been properly demonstrated just how much it helps in situations like these. Here's the exact same code but using autodie. Note that with a single line you're able to do away with all your die clauses:

use autodie qw(system chdir); # or qw(:all) system("foo --bar baz"); chdir("foobar"); system("foo --bar baz2"); system("foo --bar baz"); chdir(".."); system("bar --foo baz");

Yes, autodie works with system, even though Fatal doesn't. If you wrap the whole thing in an eval block, you can even find out what went wrong, the line number, the arguments passed to the function that died, the return value of the process, whether it died to a signal, and all the other things you want. If you don't wrap it in an eval, autodie will include all the relevant information in your error output.

Unless Rafael has changed his mind, autodie will be included with Perl 5.10.1. Unfortunately it looks like I'm going to miss getting it into 5.8.9 release. In either case, you can download autodie from the CPAN.

Another excellent option would be IPC::System::Simple. It's pure Perl, has no dependencies, and can be used in a very similar method to autodie:

use IPC::System::Simple qw(system); use Fatal qw(chdir); system("foo --bar baz"); chdir("foobar"); system("foo --bar baz2"); system("foo --bar baz"); chdir(".."); system("bar --foo baz");

autodie actually uses IPC::System::Simple underneath to handle calls to system. Given the choice, I'd use autodie, since you can control it with lexical scope, and it provide better inspection of exceptions.

Disclaimer: I am the author of all the modules mentioned in this post, and therefore think highly of them.

All the best,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://710099]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-06-23 09:28 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.