Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

IPC::Open2 failures

by El Linko (Beadle)
on Apr 07, 2005 at 12:54 UTC ( [id://445629]=perlquestion: print w/replies, xml ) Need Help??

El Linko has asked for the wisdom of the Perl Monks concerning the following question:

I need to call an external program from perl,pass it in data and pull back results. This looks like what IPC::Open2 was built for.

I can not seem to work out how to trap errors such as the program not existing or not being executable(or other random stuff such as missing libs). The pod seems to indicate a SIGPIPE signal will happen but the only signal perl is getting is a SIGCLD and this happens for both successful and unsuccessful executions. Is it possible to trap bad executions or will I just have to hope it works?

If this is the best IPC::Open2 can do are there any other core modules that can handle the job better?

Replies are listed 'Best First'.
Re: IPC::Open2 failures
by jbrugger (Parson) on Apr 07, 2005 at 13:38 UTC
    Perhaps IPC::Open3? The same as 2, but with error handling.

    *update* i'm affraid you're right, it doesn't seem to work like expected...
    Using wrong arguments and command, it still returned a proper $pid. The $err remained empty....

    Well, while i'm in hunchmode now anyway (i like to see this fixed), could IPC::Cmd help?

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
      IPC::Cmd seems to use either IPC::Open3 or IPC::Run
      The IPC::Run docs say -

      start() pauses the parent until the child executes the command or CODE reference and propagates any exceptions thrown (including exec() failure) back to the parent. This has several pleasant effects: any exceptions thrown in the child, including exec() failure, come flying out of start() or run() as though they had ocurred in the parent.

      This sounds like what I'm looking for. I'll try it tomorrow and see if lives up to its promises. I would still prefer to do this with a core module if possible and avoid any hassle getting modules installed.

      Edit by tye: Remove PRE tags around long lines

      I think that refers to the fact you can get STDERR using open3 but I'll give a looking at it.
Re: IPC::Open2 failures
by Anonymous Monk on Apr 08, 2005 at 12:01 UTC
    AFAIK IPC::Open2 does a fork/exec and only returns the result of the fork, so it's not really possible to find out if it didn't happen properly. You could do something like
    do { open2(bla, bla, bla) } if (-e $PROGRAM and -x $PROGRAM)
    maybe. That would only eliminate a couple of things and leave plenty of room for $PROGRAM to croak for other reasons, tho. You could also handle the fork yourself, but that might get hairy.
      That is probably what I'll do. Loads of stuff can go wrong but -f and -x covers what is most likely to go wrong. I may throw in a ps | grep $program name to crudely cover anything else.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://445629]
Approved by RolandGunslinger
Front-paged by RolandGunslinger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found