Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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: "...

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"; }

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015368]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-02-25 19:55 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (314 votes). Check out past polls.