Your problem is that the tool is sending errors out on STDERR while you read STDOUT. If you want to trap the error you will need to trap both filehandles.

This sounds like the kind of thing that I have used IPC::Open3 for in the past. Now be aware that doing this is somewhat tricky. As soon as you use that module you need to figure out how to handle STDIN, STDOUT, and STDERR. Just to make life fun, if are trying to read on one of the pipes while it tries to write on another, you may be waiting for a good while. Also you need to think about the fact that when the poor child dies you need to reap it with wait or waitpid.

This definitely deserves a generic "run_system" command. And since all errors will be coming from there, you want to use Carp and confess to any problems.

Therefore I recommend looking for ways to not have to do that work yourself. (Push it off to the OS. After all that is why you have one!) Personally I would use File::Spec to pick up the devnull function to get null data for STDIN. I would use File::Temp to create a tempfile for STDERR (which you can seek on then read at your leisure), and I would read STDOUT yourself.

Then your pattern looks like this:

  1. Get the system command.
  2. Open the handles it needs.
  3. open3.
  4. read STDOUT.
  5. Do a wait or waitpid to collect the zombie.
  6. Check $? and read what was printed to STDERR. Handle the error if there was one.
  7. Return what came from STDOUT.
In case it isn't obvious, I have written this kind of thing in the past. IIRC it wasn't portable (worked on Linux, but not under Windows, never bothered to find out why) so consider yourself warned that I have never bothered to do this portably. But doing it, even non-portably, was quite instructive...

In reply to Re (tilly) 1: Trapping Java Errors by tilly
in thread Trapping Java Errors by aardvark

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.