|laziness, impatience, and hubris|
Re: Too many "or die" clauses?by pjf (Curate)
|on Sep 09, 2008 at 14:22 UTC||Need Help??|
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:
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:
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,
Perl Training Australia