Redirect stdio into a string

I

Initial-N

Hi all,

How to catch all stdio output into a string?
I am using system() to call a external tool (exe), with some
arguments. Just doing so, it will print out all lot of data (which I
need to process it within the script). I wonder how I can catch all
the stdio ouput and redirect them into a string so that I can parse
it.

Thanks everyone!! I would like much appreciate if you can email at
(e-mail address removed)

Intial-N
 
A

Anno Siegel

Initial-N said:
Hi all,

How to catch all stdio output into a string?
I am using system() to call a external tool (exe), with some
arguments. Just doing so, it will print out all lot of data (which I
need to process it within the script). I wonder how I can catch all
the stdio ouput and redirect them into a string so that I can parse
it.

Read the system() documentation again.
Thanks everyone!! I would like much appreciate if you can email at
(e-mail address removed)

Post here, read here.

Anno
 
P

pkent

How to catch all stdio output into a string?
I am using system() to call a external tool (exe), with some
arguments. Just doing so, it will print out all lot of data (which I
need to process it within the script). I wonder how I can catch all
the stdio ouput and redirect them into a string so that I can parse
it.

It sounds like you want the backticks or qx() operator. This returns the
output of the command, like:

my $x = `ls`;
print length $x;

prints 46 just now.

P
 
B

Bryan Castillo

pkent said:
It sounds like you want the backticks or qx() operator. This returns the
output of the command, like:

my $x = `ls`;
print length $x;

prints 46 just now.

If there is alot of data to be processed it might me better
(more efficient) to open a pipe.

use strict;
use warnings;
use IO::File;

print "Listening ports: ";

my $proc = IO::File->new("netstat -a |") || die;
while (my $line = <$proc>) {
if ($line =~ /TCP\s+\S+:(\S+)\s+\S+\s+LISTENING/) {
print $1, " ";
}
}

print "\n";
$proc->close;

Both, my example and the examples using backticks `'s assume you only
want STDOUT. If you want STDERR (since it looks like you are using
windows - the reference to exe) you may have to look at IPC::Open3.
I don't think <some command> 2>&1 | would work on windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top