Hi
Thanks, but I need to preserve the value returned by $mycommand also. I
guess using backticks won't allow me to do that .
Mostly what I need to do is read from STDOUT into a variable. But I don't
know how to do that.
Mayank

-----Original Message-----
From: Brett W. McCoy [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, November 28, 2001 11:56 AM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: Redirecting STDOUT to a variable...


On Wed, 28 Nov 2001 [EMAIL PROTECTED] wrote:

> I am looking to run a command using perl and get its return value as well
as
> its STDOUT. Currently I am doing is
>
> $result = system("$myCommand >out.txt");
> open(FILE,"< out.txt");
>
> and then processing the data from the file. This is terribly slow for my
> application. Is there some way where I can redirect the output directly to
a
> variable and not have to do the file thing.

Certainly.  Use the backtick operator to slurp all of the output at once,
use a scalar to store it all in one string, or put it into list context
and dump each line into an array.

my $result = `$myCommand`;
my @result = `$myCommand`;

If you think the output is going to be big, or you want to process each
line of output as it occurs, you can do this:

open CMD, "$myCommand |" or die "couldn't fork: $!\n";

while(<CMD>) {

        do something if /some pattern to match/;
}

close CMD;

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
The finest eloquence is that which gets things done.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to