1Simple(3) User Contributed Perl Documentation 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" machin‐
33 ery; 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 sim‐
40 ulates 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 dis‐
53 connect command to the target) when the Expect::Simple object is
54 destroyed.
55
56 Methods
57
58 new
59 $obj = new Expect::Simple \%attr;
60
61 This creates a new object, starting up the program with which
62 to communicate (using the Expect spawn method) and waiting for
63 a prompt. The passed hash reference must contain at least the
64 Prompt, DisconnectCmd, and Cmd elements. The available
65 attributes are:
66
67 Cmd The command to which to connect. This must be speci‐
68 fied.
69
70 Prompt This specifies one or more prompts to scan for. For a
71 single prompt, the value may be a scalar; for more, or
72 for matching of regular expressions, it should be an
73 array reference. For example,
74
75 Prompt => 'prompt1> '
76 Prompt => [ 'prompt1> ', 'prompt2> ', -re => 'prompt\d+>\s+' ]
77
78 All prompts are taken literally, unless immediately
79 preceded by a "-re" flag, in which case they are regu‐
80 lar expressions.
81
82 DisconnectCmd
83 This is the command to be sent to the target program
84 which will cause it to exit.
85
86 Timeout The time in seconds to wait until giving up on the tar‐
87 get program responding. This is used during program
88 startup and when any commands are sent to the program.
89 It defaults to 1000 seconds.
90
91 Debug The value is passed to Expect via its debug method.
92
93 Verbose This results in various messages printed to the STDERR
94 stream. If greater than 3, it turns on Expect's log‐
95 ging to STDOUT (via the log_stdout Expect method.
96
97 send
98 $obj->send( $cmd );
99 $obj->send( @cmds );
100
101 Send one or more commands to the target. After each command is
102 sent, it waits for a prompt from the target. Only the output
103 resulting from the last command is available via the after,
104 before, etc. methods.
105
106 match_idx
107 This returns a unary based index indicating which prompt (in
108 the list of prompts specified via the "Prompt" attribute to the
109 new method) was received after the last command was sent. It
110 will be undef if none was returned.
111
112 match_str
113 This returns the prompt which was matched after the last com‐
114 mand was sent.
115
116 before This returns the string received before the prompt. If no
117 prompt was seen, it returns all output accumulated. This is
118 usually what the caller wants to parse. Note that the first
119 line will (usually) be the command that was sent to the target,
120 because of echoing. Check this out to be sure!
121
122 after This returns the 'after' string. Please read the Expect docs
123 for more enlightenment.
124
125 error This returns a cleaned up, more humanly readable version of the
126 errors from Expect. It'll be undef if there was no error.
127
128 error_expect
129 This returns the original Expect error.
130
131 expect_handle
132 This returns the Expect object, in case further tweaking is
133 necessary.
134
136 If the command to be run does not exist (or not in the current execu‐
137 tion path), it's quite possible that the new method will not throw an
138 exception. It's up to the caller to make sure that the command will
139 run! There's no known workaround for this.
140
142 This software is released under the GNU General Public License. You
143 may find a copy at
144
145 http://www.fsf.org/copyleft/gpl.html
146
148 Diab Jerius (djerius@cpan.org)
149
150
151
152perl v5.8.8 2002-08-13 Simple(3)