Expect::Simple(3pm) | User Contributed Perl Documentation | Expect::Simple(3pm) |
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 Expect::Simple object using new. 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 send() commands to the program; the results are available via the before, after, match_str, and match_idx methods. Since Expect 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 not done automatically.
Exceptions will be thrown on error (match with "/Expect::Simple/"). Errors from Expect are available via the error_expect method. More human readable errors are available via the error method.
The connection is automatically broken (by sending the specified disconnect command to the target) when the Expect::Simple object is destroyed.
$obj = Expect::Simple->new( \%attr );
This creates a new object, starting up the program with which to communicate (using the Expect spawn method) and waiting for a prompt. The passed hash reference must contain at least the Prompt, DisconnectCmd, and Cmd 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 => '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.
$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 after, before, etc. methods.
If the command to be run does not exist (or not in the current execution path), it's quite possible that the new 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
http://www.fsf.org/copyleft/gpl.html
Diab Jerius (djerius@cpan.org)
2008-05-06 | perl v5.12.4 |