LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2008, 11:09 PM   #1
DBelcher
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
Trying to capture STDOUT with PERL


Given the following function:

Code:
sub execute($) {
    local $response = qx!$_[0]!;
    local $result = ($? >> 8);

    return ($result,$response);
}
If called with the following argument:

Code:
($Result,$Response) = execute("iwconfig");
I would expect $Response to contain the STDOUT and $Result to contain the STDERR. But that is only partially true. Within the terminal I see the following print out

Code:
lo        no wireless extensions.

etho      no wireless extensions.

wifi0     no wireless extensions.
and when I step through the code, I see that $Response has the rest of the output from the "iwconfig" command:

Code:
ath0        IEE 802.11g ESSID:""  Nickname:""
...etc...
The question that I have is, why can't I capture ALL of the STDOUT? Why does some still escape and get printed to the screen regardless? I've tried backticks, system(), and even piping the output to a file...but none of them work completely to mask all STDOUT from getting printed to the screen. In case you are wonderring, my intention is to create a function that can silently run commands in the background without interfering with main execution by printing crap on the screen. Any ideas?

Thanks,
DBelcher
 
Old 10-16-2008, 11:24 PM   #2
ciotog
Member
 
Registered: Mar 2004
Location: Canada
Distribution: Slackware current
Posts: 728
Blog Entries: 2

Rep: Reputation: 43
Those messages are going to STDERR.

Have a gander at this:
http://perldoc.perl.org/perlfaq8.htm...nal-command%3f
 
Old 10-16-2008, 11:33 PM   #3
DBelcher
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Awesome...I had no idea! I thought STDERR was merely a numeric exit code and didn't realize it could be text. I decided to just use the bash redirection to add the STDERR to STDOUT and capture it that way, like so:

Code:
local $response = qx!$_[0] 2>&1!;
Thank you very much!
DBelcher
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: how do you capture stdout stream from external program? BrianK Programming 8 01-24-2008 08:32 PM
How to capture both stdin and stdout of a child process? jineshkj Linux - Software 2 04-21-2006 06:34 AM
how to reset the STDOUT twice in PERL bahadur Programming 0 07-04-2005 12:17 AM
stdout contents capture arvind_tyche Programming 1 06-24-2004 06:08 AM
stdout capture anb bring to file sqn Programming 2 07-10-2003 07:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration