Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Get result STDOUT of exe into scalar/array instead of file

by juo (Curate)
on Jan 21, 2004 at 02:44 UTC ( [id://322766]=perlquestion: print w/replies, xml ) Need Help??

juo has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easy way to get the STDOUT of running an exe program in windows straight into a scalar or an array instead it being printed to the STDOUT or first write it to a file and then read the file. In the example below I will run a program lmutil.exe which will return something to the screen. I am able to send that to a file instead of to the screen but I also want to skip the file and get it straight into my code. Is this possible ?

system(($FindBin::Bin."\\lib\\lmutil lmstat -c $VALOR_LICENSE_FILE")." + > ".($FindBin::Bin."\\lib\\result.txt"));

Replies are listed 'Best First'.
Re: Get result STDOUT of exe into scalar/array instead of file
by Zaxo (Archbishop) on Jan 21, 2004 at 03:03 UTC

    A piped open will do what you want,

    my $result; { local $/; open my $exe, '-|', $cmdstring or die $!; $result = <$exe>; close $exe or die $!; }
    Or else you can use backticks, my $result = `$cmdstring`;

    After Compline,
    Zaxo

Re: Get result STDOUT of exe into scalar/array instead of file
by pg (Canon) on Jan 21, 2004 at 03:05 UTC

    Update:

    Just to give an example of Open3:

    use IPC::Open3; use strict; use warnings; my $pid = open3(\*WRITER, \*READER, \*ERROR, "netstat"); while (my $line = <READER>) { print $line; } waitpid($pid, 0);

    Original

    If you are running your program thru a shell that supports pipe (like win32, unix etc.), there is almost no extra coding on your side:

    use strict; use warnings; while (<>) { print "[", $_, "]\n";#in your case, you would save this to an arra +y, or if you want scalar reset local $/ }

    For example, if you are running win32, you can do something like this:

    dir|perl -w foo.pl

    If you really mean STDOUT, not a mixture of STDOUT and STDERR, then take a look at IPC::Open3.

Re: Get result STDOUT of exe into scalar/array instead of file
by Roger (Parson) on Jan 21, 2004 at 03:29 UTC
    A simple way to capture STDOUT of a program into a scalar is with the qx or ` (backquote) operators. See perlop for more details.
    my $command = "$FindBin::Bin\\lib\\lmutil lmstat -c $VALOR_LICENSE_FIL +E"; my $result = qx/$command/; # qx / / # or my $result = `$command`; # backquote ` `

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://322766]
Approved by xenchu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-26 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found