Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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:

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,


In reply to Re: Too many "or die" clauses? by pjf
in thread Too many "or die" clauses? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found