The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

IO::React - Interaction with an IO::Handle

SYNOPSIS

  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 { ... },
            ...);

DESCRIPTION

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.

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.

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.

The set_eof method sets a subroutine to be called when react reaches the end of file on the handle it is reading from.

The set_display method controls whether or not react prints the data it reads to the default output handle.

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.

EXAMPLES

Getting a directory listing via telnet

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

AUTHOR

John Redford, John.Redford@fmr.com

SEE ALSO

IO::Handle, Proc::Spawn