in reply to assign open2 output to variable

That output is being sent to STDERR rather than STDOUT. You can easily merge the two using open3 while still avoiding to spawn a needless shell.

use IPC::Open3 qw( open3 ); my @command = qw( plink -ssh -pw passwd user@host -batch exit ); open3(\*CIN, \*COUT, \*COUT, @command); close CIN; my $xs = <COUT>; print "the output is :".$xs; close COUT;

Note that the order of open3's first two argument is different than open2's.

Replies are listed 'Best First'.
Re^2: assign open2 output to variable
by adrivez (Initiate) on Mar 29, 2014 at 02:09 UTC
    THANKS!! this is great, i can get what i needed, i'm able to read through line by line what are the outputs. However, how will i be able to output the stream in real time while i'm collecting the data in CIN? Otherwise i'll have to flush out all outputs at one shot when the plink process is done? for this example, i'll use "ls" :
    my $command = 'link -ssh -pw password_here user_name@host -batch'; $pid= open3(\*CIN, \*COUT, \*COUT, $command); print CIN "ls\n"; print CIN "exit\n"; close CIN; my @xs = <COUT>; foreach $x(@xs) { print "\noutput:".$x."\n"; } close COUT; waitpid( $pid, 0 );
      The following will print the output as its produced:
      while (<COUT>) { print; }

      Well, it prints it a line at a time. sysread can truly get it as its produced.

      Of course, plink might buffer its output. Lots of applications do when they're output isn't a terminal. If it does, you'll have to look if it provides a way to be told not to.