Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
There's more than one way to do things
 
PerlMonks  

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,


Comment on Re: Too many "or die" clauses?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://710099]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-04-21 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls