Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Open2, eval & handle-behaviour

by Fré (Initiate)
on Jan 25, 2013 at 16:34 UTC ( #1015368=perlquestion: print w/ replies, xml ) Need Help??
Fré has asked for the wisdom of the Perl Monks concerning the following question:

I'm getting totally confused with getting my in- & outputs here.
I'm supposed to run some query-stuff in forks, using an old perl 5.6-window$-thing, and its basic pre-installed mods.

Trying a logout first, then a good login and further processing with other commands. It's not supposed to end or return to the shell, just needs to continue, hence my eval-tryout. If 'logout' doesn't find a valid login to finish, then he just asks for a user&passwd, in normal cmdline-situations through STDIN.

So, I've been trying to intercept that behaviour and send back the necessary values trough many different combinations. But I don't seem to manage...
Tried writing to STDIN, tried running from STDOUT, from $chld_frm, tried switching the parameters... Name it. (I actually never really tried mixing all those handles, so I guess that after copy-pasting-changing it's full of mistakes.)
I seem to start mixing all kind of file-handle-variable-types, without ever being able to read & write what open2 should be able to give me.
A prompt "User: ", and couple of milliseconds later "Passw: "...

HOOOOOOW????
So, for Perl's sake, don't get me losing my faith!
use Symbol qw( gensym ); use IPC::Open2 qw(open2); use Env; use IO::Select; use IO::File; use IO::Handle; use Data::Dumper; my ($chld_to,$chld_frm)=map gensym, 1..2; my $pid; print "Logging out first\n"; eval { select STDOUT; $|=1; $chld_to=*STDIN; $chld_frm=*STDOUT; select $chld_to; $|=1; select $chld_frm; $/=":"; $|=1; $pid=open2($chld_frm,$chld_to,"${bin_dir}system.exe","-logout") or + die "Could not launch LOGOUT: $? - $!"; sleep 2; print Dumper $chld_to; my $output=<$chld_frm>; print STDOUT "PID : $pid\n"; print STDOUT "OUTPUT (): $output\n"; if ($output =~ /^user/i) { print $chld_to "${USER}"; } if ($output =~ /^passw/i) { print $chld_to "${PASSW}"; + } waitpid($pid,0); close $chld_to; close $chld_frm; close EXH; }; if ($!) { print "Error loggin out: $? $!\n"; } else { print "Logged out: $? $! \n"; }

Comment on Open2, eval & handle-behaviour
Download Code
Re: Open2, eval & handle-behaviour
by Fré (Initiate) on Jan 29, 2013 at 14:22 UTC

    Seems to be related with some fork-output-issue. Commandline does seem to do it, can't reproduce the same thing in forked-forks. Anyone an idea?

    perl -e "use strict; use IPC::Open2; local (*OUT,*IN); eval{ my $pid=open2(*OUT,*IN,\"gethosts\"); waitpid($pid,0); }; while(<OUT>) { print "Floep: $_\n\";}"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2015-07-03 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls