IO::React - Interaction with an IO::Handle
use IO::React; my $r = new IO::React($fh); $r->set_wait(30); # Seconds $r->set_timeout(sub { ... }); $r->set_eof(sub { ... }); $r->set_display(1); # Boolean $r->write("...", ...); $r->react('WAIT' => sub { ... }, 'TIMEOUT' => sub { ... }, 'EOF' => sub { ... }, 'pattern1' => sub { ... }, 'pattern2' => sub { ... }, ...);
IO::React provides an expect-like interface for interacting with whatever may be connected to a handle. The main routine is the react method, which calls subroutines based on matching patterns provided as arguments.
IO::React
react
There are four methods for controlling the default behaviour of an IO::React object.
The set_wait method controls the default waiting period for react to read data that matches one of the patterns it is looking for.
set_wait
The set_timeout method sets a subroutine to be called when the waiting period for react expires. If the timeout subroutine returns a defined value, that value will be used as a new waiting period.
set_timeout
The set_eof method sets a subroutine to be called when react reaches the end of file on the handle it is reading from.
set_eof
The set_display method controls whether or not react prints the data it reads to the default output handle.
set_display
Because IO::React uses the select perl function, it is not safe to use buffered io routines on the handle it is processing. For convienience, IO::React provides the <write> method to call syswrite appropriately on the handle.
select
syswrite
This is a sample program that would login to a system using telnet and run ls.
use IO::React; use Proc::Spawn; my $Prompt = "\\\$"; my $Account = "XXX"; my $Password = "XXX"; my ($pid, $fh) = spawn_pty("telnet localhost"); my $react = new IO::React($fh); $react->set_display(1); $react->set_wait(10); # React to login prompt $react->react( WAIT => 30, 'ogin:' => sub { $react->write("$Account\n") }, 'refused' => sub { print "Server not responding\n"; exit 1 } ) || die "React Failed"; # React to password prompt $react->react( 'word:' => sub { $react->write("$Password\n") } ) || die "React Failed"; # React to failure or shell prompt $react->react( 'incorrect' => sub { print "\nWrong Account/Password\n"; exit 1 }, $Prompt => sub { $react->write("ls\n") }, ); # React to shell prompt $react->react( WAIT => 60, $Prompt => sub { $react->write("exit\n"); }, );
John Redford, John.Redford@fmr.com
IO::Handle, Proc::Spawn
To install IO::React, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IO::React
CPAN shell
perl -MCPAN -e shell install IO::React
For more information on module installation, please visit the detailed CPAN module installation guide.