Solved

Capturing STDERR Output

Posted on 1999-01-21
2
685 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script help 5 233
crawling gofundme 4 121
problem with using the glob function 1 64
Replace  text in a file 2 97
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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

862 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now