1Expect::Simple(3) User Contributed Perl Documentation Expect::Simple(3)
2
3
4
6 Expect::Simple - wrapper around the Expect module
7
9 use Expect::Simple;
10
11 my $obj = new Expect::Simple
12 { Cmd => [ dmcoords => 'verbose=1', "infile=$infile"],
13 Prompt => [ -re => 'dmcoords>:\s+' ],
14 DisconnectCmd => 'q',
15 Verbose => 0,
16 Debug => 0,
17 Timeout => 100
18 };
19
20 $obj->send( $cmd );
21 print $obj->before;
22 print $obj->after;
23 print $obj->match_str, "\n";
24 print $obj->match_idx, "\n";
25 print $obj->error_expect;
26 print $obj->error;
27
28 $expect_object = $obj->expect_handle;
29
31 "Expect::Simple" is a wrapper around the "Expect" module which should
32 suffice for simple applications. It hides most of the "Expect"
33 machinery; the "Expect" object is available for tweaking if need be.
34
35 Generally, one starts by creating an Expect::Simple object using new.
36 This will start up the target program, and will wait until one of the
37 specified prompts is output by the target. At that point the caller
38 should send() commands to the program; the results are available via
39 the before, after, match_str, and match_idx methods. Since Expect
40 simulates a terminal, there will be extra "\r" characters at the end of
41 each line in the result (on UNIX at least). This is easily fixed:
42
43 ($res = $obj->before) =~ tr/\r//d;
44 @lines = split( "\n", $res );
45
46 This is not done automatically.
47
48 Exceptions will be thrown on error (match with "/Expect::Simple/").
49 Errors from Expect are available via the error_expect method. More
50 human readable errors are available via the error method.
51
52 The connection is automatically broken (by sending the specified
53 disconnect command to the target) when the Expect::Simple object is
54 destroyed.
55
56 Methods
57 new
58 $obj = Expect::Simple->new( \%attr );
59
60 This creates a new object, starting up the program with which
61 to communicate (using the Expect spawn method) and waiting for
62 a prompt. The passed hash reference must contain at least the
63 Prompt, DisconnectCmd, and Cmd elements. The available
64 attributes are:
65
66 Cmd
67 Cmd => $command,
68 Cmd => [ $command, $arg1, $arg2, ... ],
69
70 The command to which to connect. The passed command
71 may either be a scalar or an array.
72
73 Prompt This specifies one or more prompts to scan for. For a
74 single prompt, the value may be a scalar; for more, or
75 for matching of regular expressions, it should be an
76 array reference. For example,
77
78 Prompt => 'prompt1> ',
79 Prompt => [ 'prompt1> ', 'prompt2> ', -re => 'prompt\d+>\s+' ]
80
81 All prompts are taken literally, unless immediately
82 preceded by a "-re" flag, in which case they are
83 regular expressions.
84
85 DisconnectCmd
86 This is the command to be sent to the target program
87 which will cause it to exit.
88
89 RawPty If set, then underlying Expect object's pty mode is set
90 to raw mode (see Expect::raw_pty()).
91
92 Timeout The time in seconds to wait until giving up on the
93 target program responding. This is used during program
94 startup and when any commands are sent to the program.
95 It defaults to 1000 seconds.
96
97 Debug The value is passed to Expect via its debug method.
98
99 Verbose This results in various messages printed to the STDERR
100 stream. If greater than 3, it turns on Expect's
101 logging to STDOUT (via the log_stdout Expect method.
102
103 send
104 $obj->send( $cmd );
105 $obj->send( @cmds );
106
107 Send one or more commands to the target. After each command is
108 sent, it waits for a prompt from the target. Only the output
109 resulting from the last command is available via the after,
110 before, etc. methods.
111
112 match_idx
113 This returns a unary based index indicating which prompt (in
114 the list of prompts specified via the "Prompt" attribute to the
115 new method) was received after the last command was sent. It
116 will be undef if none was returned.
117
118 match_str
119 This returns the prompt which was matched after the last
120 command was sent.
121
122 before This returns the string received before the prompt. If no
123 prompt was seen, it returns all output accumulated. This is
124 usually what the caller wants to parse. Note that the first
125 line will (usually) be the command that was sent to the target,
126 because of echoing. Check this out to be sure!
127
128 after This returns the 'after' string. Please read the Expect docs
129 for more enlightenment.
130
131 error This returns a cleaned up, more humanly readable version of the
132 errors from Expect. It'll be undef if there was no error.
133
134 error_expect
135 This returns the original Expect error.
136
137 expect_handle
138 This returns the Expect object, in case further tweaking is
139 necessary.
140
142 If the command to be run does not exist (or not in the current
143 execution path), it's quite possible that the new method will not throw
144 an exception. It's up to the caller to make sure that the command will
145 run! There's no known workaround for this.
146
148 This software is released under the GNU General Public License. You
149 may find a copy at
150
151 http://www.fsf.org/copyleft/gpl.html
152
154 Diab Jerius (djerius@cpan.org)
155
156
157
158perl v5.34.0 2022-01-21 Expect::Simple(3)