Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  EXPECT::SIMPLE (3)

.ds Aq ’


Expect::Simple - wrapper around the Expect module



  use Expect::Simple;

  my $obj = new Expect::Simple
        { Cmd => [ dmcoords => verbose=1, "infile=$infile"],
          Prompt => [ -re => dmcoords>:\s+ ],
          DisconnectCmd => q,
          Verbose => 0,
          Debug => 0,
          Timeout => 100

  $obj->send( $cmd );
  print $obj->before;
  print $obj->after;
  print $obj->match_str, "\n";
  print $obj->match_idx, "\n";
  print $obj->error_expect;
  print $obj->error;

  $expect_object = $obj->expect_handle;


Expect::Simple is a wrapper around the Expect module which should suffice for simple applications. It hides most of the Expect machinery; the Expect object is available for tweaking if need be.

Generally, one starts by creating an <B>Expect::SimpleB> object using <B>newB>. This will start up the target program, and will wait until one of the specified prompts is output by the target. At that point the caller should <B>B>send()<B>B> commands to the program; the results are available via the <B>beforeB>, <B>afterB>, <B>match_strB>, and <B>match_idxB> methods. Since <B>ExpectB> simulates a terminal, there will be extra \r characters at the end of each line in the result (on UNIX at least). This is easily fixed:

    ($res = $obj->before) =~ tr/\r//d;
    @lines = split( "\n", $res );

This is <B>notB> done automatically.

Exceptions will be thrown on error (match with /Expect::Simple/). Errors from <B>ExpectB> are available via the <B>error_expectB> method. More human readable errors are available via the <B>errorB> method.

The connection is automatically broken (by sending the specified disconnect command to the target) when the <B>Expect::SimpleB> object is destroyed.



    $obj = Expect::Simple->new( \%attr );

This creates a new object, starting up the program with which to communicate (using the <B>ExpectB> <B>spawnB> method) and waiting for a prompt. The passed hash reference must contain at least the <B>PromptB>, <B>DisconnectCmdB>, and <B>CmdB> elements. The available attributes are:

  Cmd => $command,
  Cmd => [ $command, $arg1, $arg2, ... ],

The command to which to connect. The passed command may either be a scalar or an array.

Prompt This specifies one or more prompts to scan for. For a single prompt, the value may be a scalar; for more, or for matching of regular expressions, it should be an array reference. For example,

  Prompt => prompt1> ,
  Prompt => [ prompt1> , prompt2> , -re => prompt\d+>\s+ ]

All prompts are taken literally, unless immediately preceded by a -re flag, in which case they are regular expressions.

DisconnectCmd This is the command to be sent to the target program which will cause it to exit.
RawPty If set, then underlying <B>ExpectB> object’s pty mode is set to raw mode (see <B>B>Expect::raw_pty()<B>B>).
Timeout The time in seconds to wait until giving up on the target program responding. This is used during program startup and when any commands are sent to the program. It defaults to 1000 seconds.
Debug The value is passed to <B>ExpectB> via its <B>debugB> method.
Verbose This results in various messages printed to the STDERR stream. If greater than 3, it turns on <B>ExpectB>’s logging to STDOUT (via the <B>log_stdoutB> <B>ExpectB> method.


   $obj->send( $cmd );
   $obj->send( @cmds );

Send one or more commands to the target. After each command is sent, it waits for a prompt from the target. Only the output resulting from the last command is available via the <B>afterB>, <B>beforeB>, etc. methods.

match_idx This returns a unary based index indicating which prompt (in the list of prompts specified via the Prompt attribute to the <B>newB> method) was received after the last command was sent. It will be undef if none was returned.
match_str This returns the prompt which was matched after the last command was sent.
before This returns the string received before the prompt. If no prompt was seen, it returns all output accumulated. This is usually what the caller wants to parse. Note that the first line will (usually) be the command that was sent to the target, because of echoing. Check this out to be sure!
after This returns the ’after’ string. Please read the <B>ExpectB> docs for more enlightenment.
error This returns a cleaned up, more humanly readable version of the errors from <B>ExpectB>. It’ll be undef if there was no error.
error_expect This returns the original <B>ExpectB> error.
expect_handle This returns the <B>ExpectB> object, in case further tweaking is necessary.


If the command to be run does not exist (or not in the current execution path), it’s quite possible that the <B>newB> method will not throw an exception. It’s up to the caller to make sure that the command will run! There’s no known workaround for this.


This software is released under the GNU General Public License. You may find a copy at


Diab Jerius (
Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 EXPECT::SIMPLE (3) 2008-05-06

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.