On Tue, Jun 19, 2001 at 11:09:26AM -0400, Craig Moynes/Markham/IBM wrote:
> Hi,
>      I am running several commands within my perl script (on AIX 4.3.3).
> These commands include find, chmod, etc.
> What I am wondering is if anyone has a sample script kicking around where
> they execute the command and check the return code, grabbing any output
> (stderr or stdout) and printing it in the error message.
> 
> My problem right now is I am executing the commands and piping the output
> within the open command:
> open( INPUT, "$findCMD|" )
> 
> If the find command fails then i loose the error message ( and the return
> code), but I can read the stdout into perl very easily.
> 
> I'm looking for an small elegant solution (as the script is fairly small).
> But I will use suggestions involving the redirecting of output to seperate
> files if I have to.

As an alternative to the redirection solution posted by Gary Stainburn, you
can also use IPC::Open3 to open handles on each set of output.

By way of example:

    use IPC::Open3 qw(open3);
    use strict;

    open3(
        \*IN, \*OUT, \*ERR,
        'perl', '-wle', 'print STDERR "error"; print STDOUT "output"'
    );

    print "program output: $_" while <OUT>;
    print "program error:  $_" while <ERR>;

It's not as pretty, nor as elegant, as redirection, but it does allow you to
seperate the two sets of output.

There is also the IPC::Run module found on CPAN.  I've never used it, but
from the examples I've seen of its use, it would very likely solve your
problem as well.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Reply via email to