Solved

Capturing STDERR Output

Posted on 1999-01-21
2
692 Views
Last Modified: 2008-03-10
I have a process that return text using STDERR.
I need to run something like:
@out=`process command`;
To get the output, but this gets only STDOUT.
How can I get the STDERR like that in an Easy way?
0
Comment
Question by:eofek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Expert Comment

by:thoellri
ID: 1210227
From "perldoc perlfaq8" :

     With any of these, you can change file descriptors before
     the call:

         open(STDOUT, ">logfile");
         system("ls");

     or you can use Bourne shell file-descriptor redirection:

         $output = `$cmd 2>some_file`;
         open (PIPE, "cmd 2>some_file |");

     You can also use file-descriptor redirection to make STDERR
     a duplicate of STDOUT:

         $output = `$cmd 2>&1`;
         open (PIPE, "cmd 2>&1 |");

Hope this helps
   Tobias

0
 

Accepted Solution

by:
meyoz earned 50 total points
ID: 1210228
Use this (I've checked it myself - it's working):

$pipename="pipename";
system("mkfifo -m 600 $pipename"); || die("Cannot make fifo: $!\n");
if (!fork()) {
  close(STDOUT);
  open(STDOUT,">$pipename");
  close(STDERR);
  open(STDERR,">$pipename");
  system("cmd"); # place your command here
  exit(0);
} else {
  open(CATCH,"<$pipename");
# now you have STD(OUT,ERR) of cmd in CATCH
# or you can do so:
#  @out=`cat $pipename`
}
unlink("$pipename");

You will get mixed STDOUT and STDERR
I think it's clear how to capture STDOUT and STDERR separately :)

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question