http://www.perlmonks.org?node_id=80796

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

I've been banging my head against a wall trying to figure out a problem I first raised here. I have tried eval and some hairier possibilities, but nothing got me the errors/warnings coming from java command line tools like Xalan and FOP . Was it Java, Perl or me? Of course, it was stupid little me.

I figured it was time to take the dive (leap of faith?) into Interprocess Communications and follow tilly's advice. I settled in with dread and excitement to read Lincoln Stein's new book Network Programming with Perl. On page 43 my eyes popped out of my head, when I read, "On UNIX systems, you can use the Bourne shell's output redirection system to combine the subprocess's standard error with its standard output like this: $ls_output = `ls 2>&1`; Now $ls_output will contain both the standard error and the standard output of the command." Thank you Lincoln Stein for that gem (2>&1), that removes a few barriers I've been chipping away at for a few weeks now. I was fearing I was going to have to adopt an all Java solution, which would suck. But alas, Perl (and its community) comes through again.

I'm going to work on some applications with this and post them to the snippets section. Do you see any warning flags with this kind of solution? I'd also like to know if any of you have experience suggesting additions to the perldocs. If this gem had been there and in the right place(s) I would have saved hours of development time.

Get Strong Together!!

Edit: chipmunk 2001-05-15

Replies are listed 'Best First'.
Re: redirecting standard error with backticks
by danger (Priest) on May 16, 2001 at 08:22 UTC
    I'd also like to know if any of you have experience suggesting additions to the perldocs. If this gem had been there and in the right place(s) I would have saved hours of development time.

    It is unfortunate that you didn't find this in the documentation -- my first instinct was to do a quick search of the perlfaqs on 'STDERR', which turned up this entry entitled "How can I capture STDERR from an external command?" in ferlfaq8. The faqs also mention how to submit suggestions for improvements or additions to the faqs if you are so inclined.

    Searching the FAQs is as easy as: perldoc -q STDERR, or you could use my old faqgrep program (which I wrote sometime prior to the -q addition to perldoc, but I still find it more flexible and faster).

    And since I've brought it up, I may as well mention that you can grab the PDF typeset docs of a literate version of this script from my publisher's website if anyone cares to see a tiny example of a literate program (written more as a tutorial than source code documentation).

(tye)Re: redirecting standard error with backticks
by tye (Sage) on May 17, 2001 at 02:30 UTC

    If you wanted to do this is a more portable fashion ("2>&1" won't work for most Win9x users and I'm guessing it work under VMS, MacOS, and several other places that Perl works), I'd use the IPC::Open2 or IPC::Open3 module.

            - tye (but my friends call me "Tye")