Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^2: Modules which improve system() ?

by pjf (Curate)
on Jun 23, 2006 at 08:34 UTC ( #557119=note: print w/replies, xml ) Need Help??

in reply to Re: Modules which improve system() ?
in thread Modules which improve system() ?

I'm specifically after a module that makes it dead simple to call system() for the most simple cases, and still get proper error-handling. What I essentially want is Fatal for system().

That means we're not capturing output, and we're not doing complicated pipelines. If you want a module that handles all that and more, then there's IPC::Run. It's complete, but it's not simple.

What's proper-error handling for the most simple cases? Well, if your program never started, that's an error. If it got killed by a signal or dumped core, then you probably didn't expact that, so that's an error too. If it returns a non-zero exit status, then that's also an error unless you pass an argument to say otherwise.

As for efficiency, this is a clear case where correctness should easily trump speed. system() is slow to begin with, since there's all that fork-and-exec'ing going on under the hood. Having error-checking that's hard to get wrong (ie, exceptions), and having meaningful error messages (instead of die "Ooops! $! - $?") is going to be much better for correctness and programmer productivity in the long run.

This is very much a case of wanting something that makes simple things easy. We've already got many ways of making the hard things possible.


Replies are listed 'Best First'.
Re^3: Modules which improve system() ?
by xdg (Monsignor) on Jun 23, 2006 at 10:27 UTC

    As I just mentioned in this post, how about IPC::Run3?

    From its Pod:

    compared to system(), qx'', open "...|", open "|...": + redirects more than one file descriptor + returns TRUE on success, FALSE on failure + throws an error if problems occur in the parent process (or the +pre-exec child) + allows a very perlish interface to Perl data structures and subr +outines + allows 1 word invocations to avoid the shell easily: run3 ["foo"]; # does not invoke shell - does not return the exit code, leaves it in $?

    So you could use it like this:

    eval { run3 $program } or die "Errors running $program";

    Its docs leave a bit to be desired, but the current maintainer might be willing to take some doc patches...


    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      I concur with my learned friend on IPC::Run3 as a direct replacement to system(), although it can't do some of the more interactive tricks of IPC::Run.

      Morover, IPC::Run3 works on every single platform flawlessly, and so it makes for a very safe dependency. Some of the others do have the odd platform issue.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-03-19 01:59 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (231 votes). Check out past polls.