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

I am trying to get the error message if top fails. Is there a way to grab STDIN & STDERROR in the (my) open statement?
#!/usr/bin/perl open( CMD, "top -b -n1 |") || die "cannot fork: $!"; while ( <CMD> ) { if ( /^top:/ ) { print "Detected lrk4 version of top\n"; } } close( CMD );

Replies are listed 'Best First'.
Re: Grabbing STDERROR
by btrott (Parson) on Nov 10, 2000 at 07:23 UTC
    Take a look at IPC::Run. I prefer it to IPC::Open3, personally, but that's me. Here's a snippet:
    use IPC::Run qw( run ); my($out, $err); my @top = qw( top -b -n1 ); run \@top, \undef, \$out, \$err or die "top returned $?";
      I just wanted to thank the people that replied. I had thought of the shell redirect (2>&1) but forgot the syntax. I am not able to install a module because this will be run on few thousand machines but I am deffinetly going to take a look at IPC::Open3. And before I forget the shell redirect did work like a charm!
RE: Grabbing STDERROR
by AgentM (Curate) on Nov 10, 2000 at 07:16 UTC
Re: Grabbing STDERROR
by extremely (Priest) on Nov 10, 2000 at 06:59 UTC
    In some shells you can add 2>&1 before the pipe and get both STDERR and STDOUT on STDOUT...

    --
    $you = new YOU;
    honk() if $you->love(perl)