Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Expect.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "EXPECT 1"
132.TH EXPECT 1 "2002-03-19" "perl v5.8.0" "User Contributed Perl Documentation"
133.SH "NAME"
134Expect.pm \- Expect for Perl
135.SH "VERSION"
136.IX Header "VERSION"
1371.15
138.SH "SYNOPSIS"
139.IX Header "SYNOPSIS"
140.Vb 1
141\& use Expect;
142.Ve
143.PP
144.Vb 3
145\& # create an Expect object by spawning another process
146\& my $exp = Expect->spawn($command, @params)
147\& or die "Cannot spawn $command: $!\en";
148.Ve
149.PP
150.Vb 2
151\& # or by using an already opened filehandle
152\& my $exp = Expect->exp_init(\e*FILEHANDLE);
153.Ve
154.PP
155.Vb 5
156\& # if you prefer the OO mindset:
157\& my $exp = new Expect;
158\& $exp->raw_pty(1);
159\& $exp->spawn($command, @parameters)
160\& or die "Cannot spawn $command: $!\en";
161.Ve
162.PP
163.Vb 2
164\& # send some string there:
165\& $exp->send("string\en");
166.Ve
167.PP
168.Vb 2
169\& # or, for the filehandle mindset:
170\& print $exp "string\en";
171.Ve
172.PP
173.Vb 2
174\& # then do some pattern matching with either the simple interface
175\& $patidx = $exp->expect($timeout, @match_patterns);
176.Ve
177.PP
178.Vb 8
179\& # or multi-match on several spawned commands with callbacks,
180\& # just like the Tcl version
181\& $exp->expect($timeout,
182\& [ qr/regex1/ => sub { my $exp = shift;
183\& $exp->send("response\en");
184\& exp_continue; } ],
185\& [ "regexp2" , \e&callback, @cbparms ],
186\& );
187.Ve
188.PP
189.Vb 2
190\& # if no longer needed, do a soft_close to nicely shut down the command
191\& $exp->soft_close();
192.Ve
193.PP
194.Vb 2
195\& # or be less patient with
196\& $exp->hard_close();
197.Ve
198.PP
199Expect.pm is built to either spawn a process or take an existing filehandle
200and interact with it such that normally interactive tasks can be done
201without operator assistance. This concept makes more sense if you are
202already familiar with the versatile Tcl version of Expect.
203The public functions that make up Expect.pm are:
204.PP
205.Vb 34
206\& Expect->new()
207\& Expect::interconnect(@objects_to_be_read_from)
208\& Expect::test_handles($timeout, @objects_to_test)
209\& Expect::version($version_requested | undef);
210\& $object->spawn(@command)
211\& $object->clear_accum()
212\& $object->set_accum($value)
213\& $object->debug($debug_level)
214\& $object->exp_internal(0 | 1)
215\& $object->notransfer(0 | 1)
216\& $object->raw_pty(0 | 1)
217\& $object->stty(@stty_modes) # See the IO::Stty docs
218\& $object->slave()
219\& $object->before();
220\& $object->match();
221\& $object->after();
222\& $object->matchlist();
223\& $object->match_number();
224\& $object->error();
225\& $object->command();
226\& $object->exitstatus();
227\& $object->pty_handle();
228\& $object->do_soft_close();
229\& $object->restart_timeout_upon_receive(0 | 1);
230\& $object->interact($other_object, $escape_sequence)
231\& $object->log_group(0 | 1 | undef)
232\& $object->log_user(0 | 1 | undef)
233\& $object->log_file("filename" | $filehandle | \e&coderef | undef)
234\& $object->manual_stty(0 | 1 | undef)
235\& $object->match_max($max_buffersize or undef)
236\& $object->pid();
237\& $object->send_slow($delay, @strings_to_send)
238\& $object->set_group(@listen_group_objects | undef)
239\& $object->set_seq($sequence,\e&function,\e@parameters);
240.Ve
241.PP
242There are several configurable package variables that affect the behavior of Expect. They are:
243.PP
244.Vb 7
245\& $Expect::Debug;
246\& $Expect::Exp_Internal;
247\& $Expect::Log_Group;
248\& $Expect::Log_Stdout;
249\& $Expect::Manual_Stty;
250\& $Expect::Multiline_Matching;
251\& $Expect::Do_Soft_Close;
252.Ve
253.SH "DESCRIPTION"
254.IX Header "DESCRIPTION"
255The Expect module is a successor of Comm.pl and a descendent of Chat.pl. It
256more closely ressembles the Tcl Expect language than its predecessors. It
257does not contain any of the networking code found in Comm.pl. I suspect this
258would be obsolete anyway given the advent of IO::Socket and external tools
259such as netcat.
260.PP
261Expect.pm is an attempt to have more of a \fIswitch()\fR & case feeling to make
262decision processing more fluid. Three separate types of debugging have
263been implemented to make code production easier.
264.PP
265It is now possible to interconnect multiple file handles (and processes) much
266like Tcl's Expect. An attempt was made to enable all the features of Tcl's
267Expect without forcing Tcl on the victim programmer :\-) .
268.SH "USAGE"
269.IX Header "USAGE"
270.IP "new Expect ()" 4
271.IX Item "new Expect ()"
272Creates a new Expect object, i.e. a pty. You can change parameters on
273it before actually spawning a command. This is important if you want
274to modify the terminal settings for the slave. See \fIslave()\fR below.
275The object returned is actually a reblessed IO::Pty filehandle, so see
276there for additional methods.
277.IP "Expect\->exp_init(\e*FILEHANDLE) \fIor\fR" 4
278.IX Item "Expect->exp_init(*FILEHANDLE) or"
279.PD 0
280.IP "Expect\->init(\e*FILEHANDLE)" 4
281.IX Item "Expect->init(*FILEHANDLE)"
282.PD
283Initializes \f(CW$new_handle_object\fR for use with other Expect functions. It must
284be passed a \fB_reference_\fR to \s-1FILEHANDLE\s0 if you want it to work properly.
285IO::File objects are preferable. Returns a reference to the newly created
286object.
287.ie n .IP "Expect\->spawn($command, @parameters\fR) \fIor" 4
288.el .IP "Expect\->spawn($command, \f(CW@parameters\fR) \fIor\fR" 4
289.IX Item "Expect->spawn($command, @parameters) or"
290.PD 0
291.ie n .IP "$object\->spawn($command, @parameters\fR) \fIor" 4
292.el .IP "$object\->spawn($command, \f(CW@parameters\fR) \fIor\fR" 4
293.IX Item "$object->spawn($command, @parameters) or"
294.ie n .IP "new Expect ($command, @parameters)" 4
295.el .IP "new Expect ($command, \f(CW@parameters\fR)" 4
296.IX Item "new Expect ($command, @parameters)"
297.PD
298Forks and execs \f(CW$command\fR. Returns an Expect object upon success or
299\&\f(CW\*(C`undef\*(C'\fR if the fork was unsuccessful or the command could not be
300found. \fIspawn()\fR passes its parameters unchanged to Perls \fIexec()\fR, so
301look there for detailed semantics.
302.Sp
303Note that if spawn cannot \fIexec()\fR the given command, the Expect object
304is still valid and the next \fIexpect()\fR will see \*(L"Cannot exec\*(R", so you
305can use that for error handling.
306.Sp
307Also note that you cannot reuse an object with an already spawned
308command, even if that command has exited. Sorry, but you have to
309allocate a new object...
310.IP "$object\->debug(0 | 1 | 2 | 3 | undef)" 4
311.IX Item "$object->debug(0 | 1 | 2 | 3 | undef)"
312Sets debug level for \f(CW$object\fR. 1 refers to general debugging
313information, 2 refers to verbose debugging and 0 refers to no
314debugging. If you call \fIdebug()\fR with no parameters it will return the
315current debugging level. When the object is created the debugging
316level will match that \f(CW$Expect::Debug\fR, normally 0.
317.Sp
318The '3' setting is new with 1.05, and adds the additional
319functionality of having the _full_ accumulated buffer printed every
320time data is read from an Expect object. This was implemented by
321request. I recommend against using this unless you think you need it
322as it can create quite a quantity of output under some circumstances..
323.IP "$object\->exp_internal(1 | 0)" 4
324.IX Item "$object->exp_internal(1 | 0)"
325Sets/unsets 'exp_internal' debugging. This is similar in nature to its Tcl
326counterpart. It is extremely valuable when debugging \fIexpect()\fR sequences.
327When the object is created the exp_internal setting will match the value of
328\&\f(CW$Expect::Exp_Internal\fR, normally 0. Returns the current setting if called
329without parameters. It is highly recommended that you make use of the
330debugging features lest you have angry code.
331.IP "$object\->raw_pty(1 | 0)" 4
332.IX Item "$object->raw_pty(1 | 0)"
333Set pty to raw mode before spawning. This disables echoing and \s-1CR\-\s0>\s-1LF\s0
334translation and gives a more pipe-like behaviour. Note that this must
335be set \fIbefore\fR spawning the program.
336.IP "$object\->stty(qw(mode1 mode2...))" 4
337.IX Item "$object->stty(qw(mode1 mode2...))"
338Sets the tty mode for \f(CW$object\fR's associated terminal to the given
339modes. Note that on many systems the master side of the pty is not a
340tty, so you have to modify the slave pty instead, see next item. This
341needs IO::Stty installed, which is no longer required.
342.IP "$object\->\fIslave()\fR" 4
343.IX Item "$object->slave()"
344Returns a filehandle to the slave part of the pty. Very useful in modifying
345the terminal settings:
346.Sp
347.Vb 1
348\& $object->slave->stty(qw(raw -echo));
349.Ve
350.Sp
351Typical values are 'sane', 'raw', and 'raw \-echo'. Note that I
352recommend setting the terminal to 'raw' or 'raw \-echo', as this avoids
353a lot of hassle and gives pipe-like (i.e. transparent) behaviour
354(without the buffering issue).
355.IP "$object\->print(@strings) \fIor\fR" 4
356.IX Item "$object->print(@strings) or"
357.PD 0
358.IP "$object\->send(@strings)" 4
359.IX Item "$object->send(@strings)"
360.PD
361Sends the given strings to the spawned command. Note that the strings
362are not logged in the logfile (see print_log_file) but will probably
363be echoed back by the pty, depending on pty settings (default is echo)
364and thus end up there anyway. This must also be taken into account
365when \fIexpect()\fRing for an answer: the next string will be the command
366just sent. I suggest setting the pty to raw, which disables echo and
367makes the pty transparently act like a bidirectional pipe.
368.ie n .IP "$object\->expect($timeout, @match_patterns)" 4
369.el .IP "$object\->expect($timeout, \f(CW@match_patterns\fR)" 4
370.IX Item "$object->expect($timeout, @match_patterns)"
371or, more like Tcl/Expect,
372.Sp
373.Vb 11
374\& expect($timeout,
375\& '-i', [ $obj1, $obj2, ... ],
376\& [ $re_pattern, sub { ...; exp_continue; }, @subparms, ],
377\& [ 'eof', sub { ... } ],
378\& [ 'timeout', sub { ... }, \e$subparm1 ],
379\& '-i', [ $objn, ...],
380\& '-ex', $exact_pattern, sub { ... },
381\& $exact_pattern, sub { ...; exp_continue_timeout; },
382\& '-re', $re_pattern, sub { ... },
383\& '-i', \e@object_list, @pattern_list,
384\& ...);
385.Ve
386.Sp
387\&\fISimple interface:\fR
388.Sp
389Given \f(CW$timeout\fR in seconds Expect will wait for \f(CW$object\fR's handle to produce
390one of the match_patterns. Due to o/s limitations \f(CW$timeout\fR should be a round
391number. If \f(CW$timeout\fR is 0 Expect will check one time to see if \f(CW$object\fR's
392handle contains any of the match_patterns. If \f(CW$timeout\fR is undef Expect
393will wait forever for a pattern to match.
394.Sp
395If called in a scalar context, \fIexpect()\fR will return the position of
396the matched pattern within \f(CW$match_patterns\fR, or undef if no pattern was
397matched. This is a position starting from 1, so if you want to know
398which of an array of \f(CW@matched_patterns\fR matched you should subtract one
399from the return value.
400.Sp
401If called in an array context \fIexpect()\fR will return
402($matched_pattern_position, \f(CW$error\fR, \f(CW$successfully_matching_string\fR,
403\&\f(CW$before_match\fR, and \f(CW$after_match\fR).
404.Sp
405$matched_pattern_position will contain the value that would have been
406returned if \fIexpect()\fR had been called in a scalar context. \f(CW$error\fR is
407the error that occurred that caused \fIexpect()\fR to return. \f(CW$error\fR will
408contain a number followed by a string equivalent expressing the nature
409of the error. Possible values are undef, indicating no error,
410\&'1:TIMEOUT' indicating that \f(CW$timeout\fR seconds had elapsed without a
411match, '2:EOF' indicating an eof was read from \f(CW$object\fR, '3: spawn
412id($fileno) died' indicating that the process exited before matching
413and '4:$!' indicating whatever error was set in \f(CW$ERRNO\fR during the last
414read on \f(CW$object\fR's handle. All handles indicated by set_group plus
415\&\s-1STDOUT\s0 will have all data to come out of \f(CW$object\fR printed to them
416during \fIexpect()\fR if log_group and log_stdout are set.
417.Sp
418Changed from older versions is the regular expression handling. By
419default now all strings passed to \fIexpect()\fR are treated as literals. To
420match a regular expression pass '\-re' as a parameter in front of the
421pattern you want to match as a regexp.
422.Sp
423Example:
424.Sp
425.Vb 1
426\& $object->expect(15, 'match me exactly','-re','match\es+me\es+exactly');
427.Ve
428.Sp
429This change makes it possible to match literals and regular expressions
430in the same \fIexpect()\fR call.
431.Sp
432Also new is multiline matching. ^ will now match the beginning of
433lines. Unfortunately, because perl doesn't use $/ in determining where
434lines break using $ to find the end of a line frequently doesn't work. This
435is because your terminal is returning \*(L"\er\en\*(R" at the end of every line. One
436way to check for a pattern at the end of a line would be to use \er?$ instead
437of $.
438.Sp
439Example: Spawning telnet to a host, you might look for the escape
440character. telnet would return to you \*(L"\er\enEscape character is
441\&'^]'.\er\en\*(R". To find this you might use \f(CW$match\fR='^Escape char.*\e.\er?$';
442.Sp
443.Vb 1
444\& $telnet->expect(10,'-re',$match);
445.Ve
446.Sp
447\&\fINew more Tcl/Expect\-like interface:\fR
448.Sp
449It's now possible to expect on more than one connection at a time by
450specifying '\f(CW\*(C`\-i\*(C'\fR' and a single Expect object or a ref to an array
451containing Expect objects, e.g.
452.Sp
453.Vb 4
454\& expect($timeout,
455\& '-i', $exp1, @patterns_1,
456\& '-i', [ $exp2, $exp3 ], @patterns_2_3,
457\& )
458.Ve
459.Sp
460Furthermore, patterns can now be specified as array refs containing
461[$regexp, sub { ...}, \f(CW@optional_subprams\fR] . When the pattern matches,
462the subroutine is called with parameters ($matched_expect_obj,
463\&\f(CW@optional_subparms\fR). The subroutine can return the symbol
464`exp_continue' to continue the expect matching with timeout starting
465anew or return the symbol `exp_continue_timeout' for continuing expect
466without resetting the timeout count.
467.Sp
468.Vb 8
469\& $exp->expect($timeout,
470\& [ qr/username: /i, sub { my $self = shift;
471\& $self->send("$username\en");
472\& exp_continue; }],
473\& [ qr/password: /i, sub { my $self = shift;
474\& $self->send("$password\en");
475\& exp_continue; }],
476\& $shell_prompt);
477.Ve
478.Sp
479`expect' is now exported by default.
480.IP "$object\->\fIexp_before()\fR \fIor\fR" 4
481.IX Item "$object->exp_before() or"
482.PD 0
483.IP "$object\->\fIbefore()\fR" 4
484.IX Item "$object->before()"
485.PD
486\&\fIbefore()\fR returns the 'before' part of the last \fIexpect()\fR call. If the last
487\&\fIexpect()\fR call didn't match anything, \fIexp_before()\fR will return the entire
488output of the object accumulated before the \fIexpect()\fR call finished.
489.IP "$object\->\fIexp_after()\fR \fIor\fR" 4
490.IX Item "$object->exp_after() or"
491.PD 0
492.IP "$object\->\fIafter()\fR" 4
493.IX Item "$object->after()"
494.PD
495returns the 'after' part of the last \fIexpect()\fR call. If the last
496\&\fIexpect()\fR call didn't match anything, \fIexp_after()\fR will return \fIundef()\fR.
497.IP "$object\->\fIexp_match()\fR \fIor\fR" 4
498.IX Item "$object->exp_match() or"
499.PD 0
500.IP "$object\->\fImatch()\fR" 4
501.IX Item "$object->match()"
502.PD
503returns the string matched by the last \fIexpect()\fR call, undef if
504no string was matched.
505.IP "$object\->\fIexp_match_number()\fR \fIor\fR" 4
506.IX Item "$object->exp_match_number() or"
507.PD 0
508.IP "$object\->\fImatch_number()\fR" 4
509.IX Item "$object->match_number()"
510.PD
511\&\fIexp_match_number()\fR returns the number of the pattern matched by the last
512\&\fIexpect()\fR call. Keep in mind that the first pattern in a list of patterns is 1,
513not 0. Returns undef if no pattern was matched.
514.IP "$object\->\fIexp_matchlist()\fR \fIor\fR" 4
515.IX Item "$object->exp_matchlist() or"
516.PD 0
517.IP "$object\->\fImatchlist()\fR" 4
518.IX Item "$object->matchlist()"
519.PD
520\&\fIexp_matchlist()\fR returns a list of matched substrings from the brackets
521() inside the regexp that last matched. ($object\->matchlist)[0]
522thus corresponds to \f(CW$1\fR, ($object\->matchlist)[1] to \f(CW$2\fR, etc.
523.IP "$object\->\fIexp_error()\fR \fIor\fR" 4
524.IX Item "$object->exp_error() or"
525.PD 0
526.IP "$object\->\fIerror()\fR" 4
527.IX Item "$object->error()"
528.PD
529\&\fIexp_error()\fR returns the error generated by the last \fIexpect()\fR call if
530no pattern was matched. It is typically useful to examine the value returned by
531\&\fIbefore()\fR to find out what the output of the object was in determining
532why it didn't match any of the patterns.
533.IP "$object\->\fIclear_accum()\fR" 4
534.IX Item "$object->clear_accum()"
535Clear the contents of the accumulator for \f(CW$object\fR. This gets rid of
536any residual contents of a handle after \fIexpect()\fR or \fIsend_slow()\fR such
537that the next \fIexpect()\fR call will only see new data from \f(CW$object\fR. The
538contents of the accumulator are returned.
539.IP "$object\->set_accum($value)" 4
540.IX Item "$object->set_accum($value)"
541Sets the content of the accumulator for \f(CW$object\fR to \f(CW$value\fR. The
542previous content of the accumulator is returned.
543.IP "$object\->\fIexp_command()\fR \fIor\fR" 4
544.IX Item "$object->exp_command() or"
545.PD 0
546.IP "$object\->\fIcommand()\fR" 4
547.IX Item "$object->command()"
548.PD
549\&\fIexp_command()\fR returns the string that was used to spawn the command. Helpful
550for debugging and for reused patternmatch subroutines.
551.IP "$object\->\fIexp_exitstatus()\fR \fIor\fR" 4
552.IX Item "$object->exp_exitstatus() or"
553.PD 0
554.IP "$object\->\fIexitstatus()\fR" 4
555.IX Item "$object->exitstatus()"
556.PD
557Returns the exit status of \f(CW$object\fR (if it already exited).
558.IP "$object\->\fIexp_pty_handle()\fR \fIor\fR" 4
559.IX Item "$object->exp_pty_handle() or"
560.PD 0
561.IP "$object\->\fIpty_handle()\fR" 4
562.IX Item "$object->pty_handle()"
563.PD
564Returns a string representation of the attached pty, for example:
565`spawn \fIid\fR\|(5)' (pty has fileno 5), `handle \fIid\fR\|(7)' (pty was initialized
566from fileno 7) or `\s-1STDIN\s0'. Useful for debugging.
567.IP "$object\->restart_timeout_upon_receive(0 | 1)" 4
568.IX Item "$object->restart_timeout_upon_receive(0 | 1)"
569If this is set to 1, the expect timeout is retriggered whenever something
570is received from the spawned command. This allows to perform some
571aliveness testing and still expect for patterns.
572.Sp
573.Vb 5
574\& $exp->restart_timeout_upon_receive(1);
575\& $exp->expect($timeout,
576\& [ timeout => \e&report_timeout ],
577\& [ qr/pattern/ => \e&handle_pattern],
578\& );
579.Ve
580.Sp
581Now the timeout isn't triggered if the command produces any kind of output,
582i.e. is still alive, but you can act upon patterns in the output.
583.IP "$object\->notransfer(1 | 0)" 4
584.IX Item "$object->notransfer(1 | 0)"
585Do not truncate the content of the accumulator after a match.
586Normally, the accumulator is set to the remains that come after the
587matched string. Note that this setting is per object and not per
588pattern, so if you want to have normal acting patterns that truncate
589the accumulator, you have to add a
590.Sp
591.Vb 1
592\& $exp->set_accum($exp->after);
593.Ve
594.Sp
595to their callback, e.g.
596.Sp
597.Vb 12
598\& $exp->notransfer(1);
599\& $exp->expect($timeout,
600\& # accumulator not truncated, pattern1 will match again
601\& [ "pattern1" => sub { my $self = shift;
602\& ...
603\& } ],
604\& # accumulator truncated, pattern2 will not match again
605\& [ "pattern2" => sub { my $self = shift;
606\& ...
607\& $self->set_accum($self->after());
608\& } ],
609\& );
610.Ve
611.Sp
612This is only a temporary fix until I can rewrite the pattern matching
613part so it can take that additional \-notransfer argument.
614.IP "Expect::interconnect(@objects);" 4
615.IX Item "Expect::interconnect(@objects);"
616Read from \f(CW@objects\fR and print to their \f(CW@listen_groups\fR until an escape sequence
617is matched from one of \f(CW@objects\fR and the associated function returns 0 or undef.
618The special escape sequence '\s-1EOF\s0' is matched when an object's handle returns
619an end of file. Note that it is not necessary to include objects that only
620accept data in \f(CW@objects\fR since the escape sequence is _read_ from an object.
621Further note that the listen_group for a write-only object is always empty.
622Why would you want to have objects listening to \s-1STDOUT\s0 (for example)?
623By default every member of \f(CW@objects\fR _as well as every member of its listen
624group_ will be set to 'raw \-echo' for the duration of interconnection.
625Setting \f(CW$object\fR\->\fImanual_stty()\fR will stop this behavior per object.
626The original tty settings will be restored as interconnect exits.
627.Sp
628For a generic way to interconnect processes, take a look at IPC::Run.
629.IP "Expect::test_handles(@objects)" 4
630.IX Item "Expect::test_handles(@objects)"
631Given a set of objects determines which objects' handles have data ready
632to be read. \fBReturns an array\fR who's members are positions in \f(CW@objects\fR that
633have ready handles. Returns undef if there are no such handles ready.
634.IP "Expect::version($version_requested or undef);" 4
635.IX Item "Expect::version($version_requested or undef);"
636Returns current version of Expect. As of .99 earlier versions are not
637supported. Too many things were changed to make versioning possible.
638.ie n .IP "$object\->interact( ""\e*FILEHANDLE, $escape_sequence"")" 4
639.el .IP "$object\->interact( \f(CW\e*FILEHANDLE, $escape_sequence\fR)" 4
640.IX Item "$object->interact( *FILEHANDLE, $escape_sequence)"
641\&\fIinteract()\fR is essentially a macro for calling \fIinterconnect()\fR for
642connecting 2 processes together. \e*FILEHANDLE defaults to \e*STDIN and
643\&\f(CW$escape_sequence\fR defaults to undef. Interaction ceases when \f(CW$escape_sequence\fR
644is read from \fB\s-1FILEHANDLE\s0\fR, not \f(CW$object\fR. \f(CW$object\fR's listen group will
645consist solely of \e*FILEHANDLE for the duration of the interaction.
646\&\e*FILEHANDLE will not be echoed on \s-1STDOUT\s0.
647.IP "$object\->log_group(0 | 1 | undef)" 4
648.IX Item "$object->log_group(0 | 1 | undef)"
649Set/unset logging of \f(CW$object\fR to its 'listen group'. If set all objects
650in the listen group will have output from \f(CW$object\fR printed to them during
651\&\f(CW$object\fR\->\fIexpect()\fR, \f(CW$object\fR\->\fIsend_slow()\fR, and \f(CW\*(C`Expect::interconnect($object
652, ...)\*(C'\fR. Default value is on. During creation of \f(CW$object\fR the setting will
653match the value of \f(CW$Expect::Log_Group\fR, normally 1.
654.IP "$object\->log_user(0 | 1 | undef) \fIor\fR" 4
655.IX Item "$object->log_user(0 | 1 | undef) or"
656.PD 0
657.IP "$object\->log_stdout(0 | 1 | undef)" 4
658.IX Item "$object->log_stdout(0 | 1 | undef)"
659.PD
660Set/unset logging of object's handle to \s-1STDOUT\s0. This corresponds to Tcl's
661log_user variable. Returns current setting if called without parameters.
662Default setting is off for initialized handles. When a process object is
663created (not a filehandle initialized with exp_init) the log_stdout setting
664will match the value of \f(CW$Expect::Log_Stdout\fR variable, normally 1.
665If/when you initialize \s-1STDIN\s0 it is usually associated with a tty which
666will by default echo to \s-1STDOUT\s0 anyway, so be careful or you will have
667multiple echoes.
668.ie n .IP "$object\->log_file(""filename"" | $filehandle | \e&coderef | undef)" 4
669.el .IP "$object\->log_file(``filename'' | \f(CW$filehandle\fR | \e&coderef | undef)" 4
670.IX Item "$object->log_file(filename | $filehandle | &coderef | undef)"
671Log session to a file. All characters send to or received from the
672spawned process are written to the file. Normally appends to the
673logfile, but you can pass an additional mode of \*(L"w\*(R" to truncate the
674file upon \fIopen()\fR:
675.Sp
676.Vb 1
677\& $object->log_file("filename", "w");
678.Ve
679.Sp
680Returns the logfilehandle.
681.Sp
682If called with an undef value, stops logging and closes logfile:
683.Sp
684.Vb 1
685\& $object->log_file(undef);
686.Ve
687.Sp
688If called without argument, returns the logfilehandle:
689.Sp
690.Vb 1
691\& $fh = $object->log_file();
692.Ve
693.Sp
694Can be set to a code ref, which will be called instead of printing
695to the logfile:
696.Sp
697.Vb 1
698\& $object->log_file(\e&myloggerfunc);
699.Ve
700.IP "$object\->print_log_file(@strings)" 4
701.IX Item "$object->print_log_file(@strings)"
702Prints to logfile (if opened) or calls the logfile hook function.
703This allows the user to add arbitraty text to the logfile. Note that
704this could also be done as \f(CW$object\fR\->log_file\->\fIprint()\fR but would only
705work for log files, not code hooks.
706.IP "$object\->set_seq($sequence, \e&function, \e@function_parameters)" 4
707.IX Item "$object->set_seq($sequence, &function, @function_parameters)"
708During Expect\->\fIinterconnect()\fR if \f(CW$sequence\fR is read from \f(CW$object\fR &function
709will be executed with parameters \f(CW@function_parameters\fR. It is \fB_highly
710recommended_\fR that the escape sequence be a single character since the
711likelihood is great that the sequence will be broken into to separate reads
712from the \f(CW$object\fR's handle, making it impossible to strip \f(CW$sequence\fR from
713getting printed to \f(CW$object\fR's listen group. \e&function should be something
714like 'main::control_w_function' and \f(CW@function_parameters\fR should be an
715array defined by the caller, passed by reference to \fIset_seq()\fR.
716Your function should return a non-zero value if execution of \fIinterconnect()\fR
717is to resume after the function returns, zero or undefined if \fIinterconnect()\fR
718should return after your function returns.
719The special sequence '\s-1EOF\s0' matches the end of file being reached by \f(CW$object\fR.
720See \fIinterconnect()\fR for details.
721.IP "$object\->set_group(@listener_objects)" 4
722.IX Item "$object->set_group(@listener_objects)"
723@listener_objects is the list of objects that should have their handles
724printed to by \f(CW$object\fR when Expect::interconnect, \f(CW$object\fR\->\fIexpect()\fR or
725\&\f(CW$object\fR\->\fIsend_slow()\fR are called. Calling w/out parameters will return
726the current list of the listener objects.
727.IP "$object\->manual_stty(0 | 1 | undef)" 4
728.IX Item "$object->manual_stty(0 | 1 | undef)"
729Sets/unsets whether or not Expect should make reasonable guesses as to
730when and how to set tty parameters for \f(CW$object\fR. Will match
731\&\f(CW$Expect::Manual_Stty\fR value (normally 0) when \f(CW$object\fR is created. If called
732without parameters \fImanual_stty()\fR will return the current manual_stty setting.
733.IP "$object\->match_max($maximum_buffer_length | undef) \fIor\fR" 4
734.IX Item "$object->match_max($maximum_buffer_length | undef) or"
735.PD 0
736.IP "$object\->max_accum($maximum_buffer_length | undef)" 4
737.IX Item "$object->max_accum($maximum_buffer_length | undef)"
738.PD
739Set the maximum accumulator size for object. This is useful if you think
740that the accumulator will grow out of hand during \fIexpect()\fR calls. Since
741the buffer will be matched by every match_pattern it may get slow if the
742buffer gets too large. Returns current value if called without parameters.
743Not defined by default.
744.IP "$object\->notransfer(0 | 1)" 4
745.IX Item "$object->notransfer(0 | 1)"
746If set, matched strings will not be deleted from the accumulator.
747Returns current value if called without parameters. False by default.
748.IP "$object\->\fIexp_pid()\fR \fIor\fR" 4
749.IX Item "$object->exp_pid() or"
750.PD 0
751.IP "$object\->\fIpid()\fR" 4
752.IX Item "$object->pid()"
753.PD
754Return pid of \f(CW$object\fR, if one exists. Initialized filehandles will not have
755pids (of course).
756.ie n .IP "$object\->send_slow($delay, @strings);" 4
757.el .IP "$object\->send_slow($delay, \f(CW@strings\fR);" 4
758.IX Item "$object->send_slow($delay, @strings);"
759print each character from each string of \f(CW@strings\fR one at a time with \f(CW$delay\fR
760seconds before each character. This is handy for devices such as modems
761that can be annoying if you send them data too fast. After each character
762\&\f(CW$object\fR will be checked to determine whether or not it has any new data ready
763and if so update the accumulator for future \fIexpect()\fR calls and print the
764output to \s-1STDOUT\s0 and \f(CW@listen_group\fR if log_stdout and log_group are
765appropriately set.
766.Sh "Configurable Package Variables:"
767.IX Subsection "Configurable Package Variables:"
768.RE
769.IP "$Expect::Debug"
770.IX Item "$Expect::Debug"
771Defaults to 0. Newly created objects have a \f(CW$object\fR\->\fIdebug()\fR value
772of \f(CW$Expect::Debug\fR. See \f(CW$object\fR\->\fIdebug()\fR;
773.RE
774.IP "$Expect::Do_Soft_Close"
775.IX Item "$Expect::Do_Soft_Close"
776Defaults to 0. When destroying objects, soft_close may take up to half
777a minute to shut everything down. From now on, only hard_close will
778be called, which is less polite but still gives the process a chance
779to terminate properly. Set this to '1' for old behaviour.
780.RE
781.IP "$Expect::Exp_Internal"
782.IX Item "$Expect::Exp_Internal"
783Defaults to 0. Newly created objects have a \f(CW$object\fR\->\fIexp_internal()\fR
784value of \f(CW$Expect::Exp_Internal\fR. See \f(CW$object\fR\->\fIexp_internal()\fR.
785.RE
786.IP "$Expect::Log_Group"
787.IX Item "$Expect::Log_Group"
788Defaults to 1. Newly created objects have a \f(CW$object\fR\->\fIlog_group()\fR
789value of \f(CW$Expect::Log_Group\fR. See \f(CW$object\fR\->\fIlog_group()\fR.
790.RE
791.IP "$Expect::Log_Stdout"
792.IX Item "$Expect::Log_Stdout"
793Defaults to 1 for spawned commands, 0 for file handles
794attached with \fIexp_init()\fR. Newly created objects have a
795\&\f(CW$object\fR\->\fIlog_stdout()\fR value of \f(CW$Expect::Log_Stdout\fR. See
796\&\f(CW$object\fR\->\fIlog_stdout()\fR.
797.RE
798.IP "$Expect::Manual_Stty"
799.IX Item "$Expect::Manual_Stty"
800Defaults to 0. Newly created objects have a \f(CW$object\fR\->\fImanual_stty()\fR
801value of \f(CW$Expect::Manual_Stty\fR. See \f(CW$object\fR\->\fImanual_stty()\fR.
802.RE
803.IP "$Expect::Multiline_Matching"
804.IX Item "$Expect::Multiline_Matching"
805.Vb 6
806\& Defaults to 1. Affects whether or not expect() uses the /m flag for
807\&doing regular expression matching. If set to 1 /m is used.
808\& This makes a difference when you are trying to match ^ and $. If
809\&you have this on you can match lines in the middle of a page of output
810\&using ^ and $ instead of it matching the beginning and end of the entire
811\&expression. I think this is handy.
812.Ve
813.SH "CONTRIBUTIONS"
814.IX Header "CONTRIBUTIONS"
815Lee Eakin <leakin@japh.itg.ti.com> has ported the kibitz script
816from Tcl/Expect to Perl/Expect. You can find it in the examples/
817subdir. Thanks Lee!
818.PP
819Historical notes:
820.PP
821There are still a few lines of code dating back to the inspirational
822Comm.pl and Chat.pl modules without which this would not have been possible.
823Kudos to Eric Arnold <Eric.Arnold@Sun.com> and Randal 'Nuke your \s-1NT\s0 box with
824one line of perl code' Schwartz<merlyn@stonehenge.com> for making these
825available to the perl public.
826.PP
827As of .98 I think all the old code is toast. No way could this have been done
828without it though. Special thanks to Graham Barr for helping make sense of
829the IO::Handle stuff as well as providing the highly recommended IO::Tty
830module.
831.SH "REFERENCES"
832.IX Header "REFERENCES"
833Mark Rogaski <rogaski@att.com> wrote:
834.PP
835\&\*(L"I figured that you'd like to know that Expect.pm has been very
836useful to \s-1AT&T\s0 Labs over the past couple of years (since I first talked to
837Austin about design decisions). We use Expect.pm for managing
838the switches in our network via the telnet interface, and such automation
839has significantly increased our reliability. So, you can honestly say that
840one of the largest digital networks in existence (\s-1AT&T\s0 Frame Relay) uses
841Expect.pm quite extensively.\*(R"
842.SH "FAQ \- Frequently Asked Questions"
843.IX Header "FAQ - Frequently Asked Questions"
844This is a growing collection of things that might help.
845Please send you questions that are not answered here to
846RGiersig@cpan.org
847.Sh "What systems does Expect run on?"
848.IX Subsection "What systems does Expect run on?"
849Expect itself doesn't have real system dependencies, but the underlying
850IO::Tty needs pseudoterminals. IO::Stty uses \s-1POSIX\s0.pm and Fcntl.pm.
851.PP
852I have used it on Solaris, Linux and \s-1AIX\s0, others report *BSD and \s-1OSF\s0
853as working. Generally, any modern \s-1POSIX\s0 Unix should do, but there
854are exceptions to every rule. Feedback is appreciated.
855.PP
856See IO::Tty for a list of verified systems.
857.Sh "Can I use this module with ActivePerl on Windows?"
858.IX Subsection "Can I use this module with ActivePerl on Windows?"
859Up to now, the answer was 'No', but this has changed.
860.PP
861You still cannot use ActivePerl, but if you use the Cygwin environment
862(http://sources.redhat.com), which brings its own perl, and have
863the latest IO::Tty (v0.05 or later) installed, it should work (feedback
864appreciated).
865.Sh "The examples in the tutorial don't work!"
866.IX Subsection "The examples in the tutorial don't work!"
867The tutorial is hopelessly out of date and needs a serious overhaul.
868I appologize for this, I have concentrated my efforts mainly on the
869functionality. Volunteers welcomed.
870.Sh "How can I find out what Expect is doing?"
871.IX Subsection "How can I find out what Expect is doing?"
872If you set
873.PP
874.Vb 1
875\& $Expect::Exp_Internal = 1;
876.Ve
877.PP
878Expect will tell you very verbosely what it is receiving and sending,
879what matching it is trying and what it found. You can do this on a
880per-command base with
881.PP
882.Vb 1
883\& $exp->exp_internal(1);
884.Ve
885.PP
886You can also set
887.PP
888.Vb 1
889\& $Expect::Debug = 1; # or 2, 3 for more verbose output
890.Ve
891.PP
892or
893.PP
894.Vb 1
895\& $exp->debug(1);
896.Ve
897.PP
898which gives you even more output.
899.Sh "I am seeing the output of the command I spawned. Can I turn that off?"
900.IX Subsection "I am seeing the output of the command I spawned. Can I turn that off?"
901Yes, just set
902.PP
903.Vb 1
904\& $Expect::Log_Stdout = 0;
905.Ve
906.PP
907to globally disable it or
908.PP
909.Vb 1
910\& $exp->log_stdout(0);
911.Ve
912.PP
913for just that command. 'log_user' is provided as an alias so
914Tcl/Expect user get a \s-1DWIM\s0 experience... :\-)
915.Sh "No, I mean that when I send some text to the spawned process, it gets echoed back and I have to deal with it in the next expect."
916.IX Subsection "No, I mean that when I send some text to the spawned process, it gets echoed back and I have to deal with it in the next expect."
917This is caused by the pty, which has probably 'echo' enabled. A
918solution would be to set the pty to raw mode, which in general is
919cleaner for communication between two programs (no more unexpected
920character translations). Unfortunately this would break a lot of old
921code that sends \*(L"\er\*(R" to the program instead of \*(L"\en\*(R" (translating this
922is also handled by the pty), so I won't add this to Expect just like that.
923But feel free to experiment with \f(CW\*(C`$exp\->raw_pty(1)\*(C'\fR.
924.Sh "How do I send control characters to a process?"
925.IX Subsection "How do I send control characters to a process?"
926A: You can send any characters to a process with the print command. To
927represent a control character in Perl, use \ec followed by the letter. For
928example, control-G can be represented with \*(L"\ecG\*(R" . Note that this will not
929work if you single-quote your string. So, to send control-C to a process in
930\&\f(CW$exp\fR, do:
931.PP
932.Vb 1
933\& print $exp "\ecC";
934.Ve
935.PP
936Or, if you prefer:
937.PP
938.Vb 1
939\& $exp->send("\ecC");
940.Ve
941.PP
942The ability to include control characters in a string like this is provided
943by Perl, not by Expect.pm . Trying to learn Expect.pm without a thorough
944grounding in Perl can be very daunting. We suggest you look into some of
945the excellent Perl learning material, such as the books _Programming Perl_
946and _Learning Perl_ by O'Reilly, as well as the extensive online Perl
947documentation available through the perldoc command.
948.Sh "My script fails from time to time without any obvious reason. It seems that I am sometimes loosing output from the spawned program."
949.IX Subsection "My script fails from time to time without any obvious reason. It seems that I am sometimes loosing output from the spawned program."
950You could be exiting too fast without giving the spawned program
951enough time to finish. Try adding \f(CW$exp\fR\->\fIsoft_close()\fR to terminate the
952program gracefully or do an \fIexpect()\fR for 'eof'.
953.PP
954Alternatively, try adding a 'sleep 1' after you \fIspawn()\fR the program.
955It could be that pty creation on your system is just slow (but this is
956rather improbable if you are using the latest IO\-Tty).
957.Sh "I want to automate password entry for su/ssh/scp/rsh/..."
958.IX Subsection "I want to automate password entry for su/ssh/scp/rsh/..."
959You shouldn't use Expect for this. Putting passwords, especially
960root passwords, into scripts in clear text can mean severe security
961problems. I strongly recommend using other means. For 'su', consider
962switching to 'sudo', which gives you root access on a per-command and
963per-user basis without the need to enter passwords. 'ssh'/'scp' can be
964set up with \s-1RSA\s0 authentication without passwords. 'rsh' can use
965the .rhost mechanism, but I'd strongly suggest to switch to 'ssh'; to
966mention 'rsh' and 'security' in the same sentence makes an oxymoron.
967.PP
968It will work for 'telnet', though, and there are valid uses for it,
969but you still might want to consider using 'ssh', as keeping cleartext
970passwords around is very insecure.
971.Sh "I want to use Expect to automate [anything with a buzzword]..."
972.IX Subsection "I want to use Expect to automate [anything with a buzzword]..."
973Are you sure there is no other, easier way? As a rule of thumb,
974Expect is useful for automating things that expect to talk to a human,
975where no formal standard applies. For other tasks that do follow a
976well-defined protocol, there are often better-suited modules that
977already can handle those protocols. Don't try to do \s-1HTTP\s0 requests by
978spawning telnet to port 80, use \s-1LWP\s0 instead. To automate \s-1FTP\s0, take a
979look at Net::FTP or \f(CW\*(C`ncftp\*(C'\fR (http://www.ncftp.org). You don't use
980a screwdriver to hammer in your nails either, or do you?
981.Sh "I want to log the whole session to a file."
982.IX Subsection "I want to log the whole session to a file."
983Use
984.PP
985.Vb 1
986\& $exp->log_file("filename");
987.Ve
988.PP
989or
990.PP
991.Vb 1
992\& $exp->log_file($filehandle);
993.Ve
994.PP
995or even
996.PP
997.Vb 1
998\& $exp->log_file(\e&log_procedure);
999.Ve
1000.PP
1001for maximum flexibility.
1002.PP
1003Note that the logfile is appended to by default, but you can
1004specify an optional mode \*(L"w\*(R" to truncate the logfile:
1005.PP
1006.Vb 1
1007\& $exp->log_file("filename", "w");
1008.Ve
1009.PP
1010To stop logging, just call it with a false argument:
1011.PP
1012.Vb 1
1013\& $exp->log_file(undef);
1014.Ve
1015.Sh "How can I turn off multi-line matching for my regexps?"
1016.IX Subsection "How can I turn off multi-line matching for my regexps?"
1017To globally unset multi-line matching for all regexps:
1018.PP
1019.Vb 1
1020\& $Expect::Multiline_Matching = 0;
1021.Ve
1022.PP
1023You can do that on a per-regexp basis by stating \f(CW\*(C`(?\-m)\*(C'\fR inside the regexp
1024(you need perl5.00503 or later for that).
1025.Sh "How can I expect on multiple spawned commands?"
1026.IX Subsection "How can I expect on multiple spawned commands?"
1027You can use the \fB\-i\fR parameter to specify a single object or a list
1028of Expect objects. All following patterns will be evaluated against
1029that list.
1030.PP
1031You can specify \fB\-i\fR multiple times to create groups of objects
1032and patterns to match against within the same expect statement.
1033.PP
1034This works just like in Tcl/Expect.
1035.PP
1036See the source example below.
1037.Sh "I seem to have problems with ptys!"
1038.IX Subsection "I seem to have problems with ptys!"
1039Well, pty handling is really a black magic, as it is extremely system
1040dependend. I have extensively revised IO\-Tty, so these problems
1041should be gone.
1042.PP
1043If your system is listed in the \*(L"verified\*(R" list of IO::Tty, you
1044probably have some non-standard setup, e.g. you compiled your
1045Linux-kernel yourself and disabled ptys. Please ask your friendly
1046sysadmin for help.
1047.PP
1048If your system is not listed, unpack the latest version of IO::Tty,
1049do a 'perl Makefile.PL; make; make test; uname \f(CW\*(C`\-a\*(C'\fR' and send me the
1050results and I'll see what I can deduce from that.
1051.Sh "I just want to read the output of a process without \fIexpect()\fPing anything. How can I do this?"
1052.IX Subsection "I just want to read the output of a process without expect()ing anything. How can I do this?"
1053[ Are you sure you need Expect for this? How about \fIqx()\fR or open(\*(L"prog|\*(R")? ]
1054.PP
1055By using expect without any patterns to match.
1056.PP
1057.Vb 3
1058\& $process->expect(undef); # Forever until EOF
1059\& $process->expect($timeout); # For a few seconds
1060\& $process->expect(0); # Is there anything ready on the handle now?
1061.Ve
1062.Sh "Ok, so now how do I get what was read on the handle?"
1063.IX Subsection "Ok, so now how do I get what was read on the handle?"
1064.Vb 1
1065\& $read = $process->before();
1066.Ve
1067.Sh "Where's IO::Pty?"
1068.IX Subsection "Where's IO::Pty?"
1069Find it on \s-1CPAN\s0 as IO\-Tty, which provides both.
1070.Sh "How come when I automate the passwd program to change passwords for me passwd dies before changing the password sometimes/every time?"
1071.IX Subsection "How come when I automate the passwd program to change passwords for me passwd dies before changing the password sometimes/every time?"
1072What's happening is you are closing the handle before passwd exits.
1073When you close the handle to a process, it is sent a signal (\s-1SIGPIPE\s0?)
1074telling it that \s-1STDOUT\s0 has gone away. The default behavior for
1075processes is to die in this circumstance. Two ways you can make this
1076not happen are:
1077.PP
1078.Vb 1
1079\& $process->soft_close();
1080.Ve
1081.PP
1082This will wait 15 seconds for a process to come up with an \s-1EOF\s0 by
1083itself before killing it.
1084.PP
1085.Vb 1
1086\& $process->expect(undef);
1087.Ve
1088.PP
1089This will wait forever for the process to match an empty set of
1090patterns. It will return when the process hits an \s-1EOF\s0.
1091.PP
1092As a rule, you should always \fIexpect()\fR the result of your transaction
1093before you continue with processing.
1094.Sh "How come when I try to make a logfile with \fIlog_file()\fP or \fIset_group()\fP it doesn't print anything after the last time I run \fIexpect()\fP?"
1095.IX Subsection "How come when I try to make a logfile with log_file() or set_group() it doesn't print anything after the last time I run expect()?"
1096Output is only printed to the logfile/group when Expect reads from the
1097process, during \fIexpect()\fR, \fIsend_slow()\fR and \fIinterconnect()\fR.
1098One way you can force this is to make use of
1099.PP
1100.Vb 1
1101\& $process->expect(undef);
1102.Ve
1103.PP
1104and
1105.PP
1106.Vb 1
1107\& $process->expect(0);
1108.Ve
1109.PP
1110which will make \fIexpect()\fR run with an empty pattern set forever or just
1111for an instant to capture the output of \f(CW$process\fR. The output is
1112available in the accumulator, so you can grab it using
1113\&\f(CW$process\fR\->\fIbefore()\fR.
1114.Sh "I seem to have problems with terminal settings, double echoing, etc."
1115.IX Subsection "I seem to have problems with terminal settings, double echoing, etc."
1116Tty settings are a major pain to keep track of. If you find unexpected
1117behavior such as double-echoing or a frozen session, doublecheck the
1118documentation for default settings. When in doubt, handle them
1119yourself using \f(CW$exp\fR\->\fIstty()\fR and \fImanual_stty()\fR functions. As of .98
1120you shouldn't have to worry about stty settings getting fouled unless
1121you use interconnect or intentionally change them (like doing \-echo to
1122get a password).
1123.PP
1124If you foul up your terminal's tty settings, kill any hung processes
1125and enter 'stty sane' at a shell prompt. This should make your
1126terminal manageable again.
1127.PP
1128Note that IO::Tty returns ptys with your systems default setting
1129regarding echoing, \s-1CRLF\s0 translation etc. and Expect does not change
1130them. I have considered setting the ptys to 'raw' without any
1131translation whatsoever, but this would break a lot of existing things,
1132as '\er' translation would not work anymore. On the other hand, a raw
1133pty works much like a pipe and is more \s-1WYGIWYE\s0 (what you get is what
1134you expect), so I suggest you set it to 'raw' by yourself:
1135.PP
1136.Vb 3
1137\& $exp = new Expect;
1138\& $exp->raw_pty(1);
1139\& $exp->spawn(...);
1140.Ve
1141.PP
1142To disable echo:
1143.PP
1144.Vb 1
1145\& $exp->slave->stty(qw(-echo));
1146.Ve
1147.Sh "I'm spawning a telnet/ssh session and then let the user interact with it. But screen-oriented applications on the other side don't work properly."
1148.IX Subsection "I'm spawning a telnet/ssh session and then let the user interact with it. But screen-oriented applications on the other side don't work properly."
1149You have to set the terminal screen size for that. Luckily, IO::Pty
1150already has a method for that, so modify your code to look like this:
1151.PP
1152.Vb 3
1153\& my $exp = new Expect;
1154\& $exp->slave->clone_winsize_from(\e*STDIN);
1155\& $exp->spawn("telnet somehost);
1156.Ve
1157.PP
1158Also, some applications need the \s-1TERM\s0 shell variable set so they know
1159how to move the cursor across the screen. When logging in, the remote
1160shell sends a query (Ctrl\-Z I think) and expects the terminal to
1161answer with a string, e.g. 'xterm'. If you really want to go that way
1162(be aware, madness lies at its end), you can handle that and send back
1163the value in \f(CW$ENV\fR{\s-1TERM\s0}. This is only a hand-waving explanation,
1164please figure out the details by yourself.
1165.Sh "I set the terminal size as explained above, but if I resize the window, the application does not notice this."
1166.IX Subsection "I set the terminal size as explained above, but if I resize the window, the application does not notice this."
1167You have to catch the signal \s-1WINCH\s0 (\*(L"window size changed\*(R"), change the
1168terminal size and propagate the signal to the spawned application:
1169.PP
1170.Vb 4
1171\& my $exp = new Expect;
1172\& $exp->slave->clone_winsize_from(\e*STDIN);
1173\& $exp->spawn("ssh somehost);
1174\& $SIG{WINCH} = \e&winch;
1175.Ve
1176.PP
1177.Vb 5
1178\& sub winch {
1179\& $exp->slave->clone_winsize_from(\e*STDIN);
1180\& kill WINCH => $exp->pid if $exp->pid;
1181\& $SIG{WINCH} = \e&winch;
1182\& }
1183.Ve
1184.PP
1185.Vb 1
1186\& $exp->interact();
1187.Ve
1188.Sh "I noticed that the test uses a string that resembles, but not exactly matches, a well-known sentence that contains every character. What does that mean?"
1189.IX Subsection "I noticed that the test uses a string that resembles, but not exactly matches, a well-known sentence that contains every character. What does that mean?"
1190That means you are anal\-retentive. :\-) [Gotcha there!]
1191.ie n .Sh "I get a ""Could not assign a pty"" error when running as a non-root user on an \s-1IRIX\s0 box?"
1192.el .Sh "I get a ``Could not assign a pty'' error when running as a non-root user on an \s-1IRIX\s0 box?"
1193.IX Subsection "I get a Could not assign a pty error when running as a non-root user on an IRIX box?"
1194The \s-1OS\s0 may not be configured to grant additional pty's (pseudo terminals)
1195to non-root users. /usr/sbin/mkpts should be 4755, not 700 for this
1196to work. I don't know about security implications if you do this.
1197.Sh "How come I don't notice when the spawned process closes its stdin/out/err??"
1198.IX Subsection "How come I don't notice when the spawned process closes its stdin/out/err??"
1199You are probably on one of the systems where the master doesn't get an
1200\&\s-1EOF\s0 when the slave closes stdin/out/err.
1201.PP
1202One possible solution is when you spawn a process, follow it with a
1203unique string that would indicate the process is finished.
1204.PP
1205.Vb 1
1206\& $process = Expect->spawn('telnet somehost; echo ____END____');
1207.Ve
1208.PP
1209And then \f(CW$process\fR\->expect($timeout,'_\|__\|_END_\|__\|_','other','patterns');
1210.SH "Source Examples"
1211.IX Header "Source Examples"
1212.Sh "How to automate login"
1213.IX Subsection "How to automate login"
1214.Vb 39
1215\& my $exp = Expect->spawn("telnet localhost")
1216\& or die "Cannot spawn telnet: $!\en";;
1217\& my $spawn_ok;
1218\& $exp->expect($timeout,
1219\& [
1220\& qr'login: $',
1221\& sub {
1222\& $spawn_ok = 1;
1223\& my $fh = shift;
1224\& $fh->send("$username\en");
1225\& exp_continue;
1226\& }
1227\& ],
1228\& [
1229\& 'Password: $',
1230\& sub {
1231\& my $fh = shift;
1232\& print $fh "$password\en";
1233\& exp_continue;
1234\& }
1235\& ],
1236\& [
1237\& eof =>
1238\& sub {
1239\& if ($spawn_ok) {
1240\& die "ERROR: premature EOF in login.\en";
1241\& } else {
1242\& die "ERROR: could not spawn telnet.\en";
1243\& }
1244\& }
1245\& ],
1246\& [
1247\& timeout =>
1248\& sub {
1249\& die "No login.\en";
1250\& }
1251\& ],
1252\& '-re', qr'[#>:] $', #' wait for shell prompt, then exit expect
1253\& );
1254.Ve
1255.Sh "How to expect on multiple spawned commands"
1256.IX Subsection "How to expect on multiple spawned commands"
1257.Vb 3
1258\& foreach my $cmd (@list_of_commands) {
1259\& push @commands, Expect->spawn($cmd);
1260\& }
1261.Ve
1262.PP
1263.Vb 16
1264\& expect($timeout,
1265\& '-i', \e@commands,
1266\& [
1267\& qr"pattern", # find this pattern in output of all commands
1268\& sub {
1269\& my $obj = shift; # object that matched
1270\& print $obj "something\en";
1271\& exp_continue; # we don't want to terminate the expect call
1272\& }
1273\& ],
1274\& '-i', $some_other_command,
1275\& [
1276\& "some other pattern",
1277\& sub {
1278\& my ($obj, $parmref) = @_;
1279\& # ...
1280.Ve
1281.PP
1282.Vb 5
1283\& # now we exit the expect command
1284\& },
1285\& \e$parm
1286\& ],
1287\& );
1288.Ve
1289.Sh "How to propagate terminal sizes"
1290.IX Subsection "How to propagate terminal sizes"
1291.Vb 4
1292\& my $exp = new Expect;
1293\& $exp->slave->clone_winsize_from(\e*STDIN);
1294\& $exp->spawn("ssh somehost);
1295\& $SIG{WINCH} = \e&winch;
1296.Ve
1297.PP
1298.Vb 5
1299\& sub winch {
1300\& $exp->slave->clone_winsize_from(\e*STDIN);
1301\& kill WINCH => $exp->pid if $exp->pid;
1302\& $SIG{WINCH} = \e&winch;
1303\& }
1304.Ve
1305.PP
1306.Vb 1
1307\& $exp->interact();
1308.Ve
1309.SH "HOMEPAGE"
1310.IX Header "HOMEPAGE"
1311http://sourceforge.net/projects/expectperl/
1312.SH "MAILING LISTS"
1313.IX Header "MAILING LISTS"
1314There are two mailing lists available, expectperl-announce and
1315expectperl\-discuss, at
1316.PP
1317.Vb 1
1318\& http://lists.sourceforge.net/lists/listinfo/expectperl-announce
1319.Ve
1320.PP
1321and
1322.PP
1323.Vb 1
1324\& http://lists.sourceforge.net/lists/listinfo/expectperl-discuss
1325.Ve
1326.SH "AUTHORS"
1327.IX Header "AUTHORS"
1328(c) 1997 Austin Schutz <\fIASchutz@users.sourceforge.net\fR> (retired)
1329.PP
1330\&\fIexpect()\fR interface & functionality enhancements (c) 1999\-2002 Roland Giersig.
1331.PP
1332This module is now maintained by Roland Giersig <\fIRGiersig@cpan.org\fR>
1333.SH "LICENSE"
1334.IX Header "LICENSE"
1335This module can be used under the same terms as Perl.
1336.SH "DISCLAIMER"
1337.IX Header "DISCLAIMER"
1338\&\s-1THIS\s0 \s-1SOFTWARE\s0 \s-1IS\s0 \s-1PROVIDED\s0 ``\s-1AS\s0 \s-1IS\s0'' \s-1AND\s0 \s-1ANY\s0 \s-1EXPRESS\s0 \s-1OR\s0 \s-1IMPLIED\s0
1339\&\s-1WARRANTIES\s0, \s-1INCLUDING\s0, \s-1BUT\s0 \s-1NOT\s0 \s-1LIMITED\s0 \s-1TO\s0, \s-1THE\s0 \s-1IMPLIED\s0 \s-1WARRANTIES\s0 \s-1OF\s0
1340\&\s-1MERCHANTABILITY\s0 \s-1AND\s0 \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0 \s-1ARE\s0 \s-1DISCLAIMED\s0.
1341\&\s-1IN\s0 \s-1NO\s0 \s-1EVENT\s0 \s-1SHALL\s0 \s-1THE\s0 \s-1AUTHORS\s0 \s-1BE\s0 \s-1LIABLE\s0 \s-1FOR\s0 \s-1ANY\s0 \s-1DIRECT\s0, \s-1INDIRECT\s0,
1342\&\s-1INCIDENTAL\s0, \s-1SPECIAL\s0, \s-1EXEMPLARY\s0, \s-1OR\s0 \s-1CONSEQUENTIAL\s0 \s-1DAMAGES\s0 (\s-1INCLUDING\s0,
1343\&\s-1BUT\s0 \s-1NOT\s0 \s-1LIMITED\s0 \s-1TO\s0, \s-1PROCUREMENT\s0 \s-1OF\s0 \s-1SUBSTITUTE\s0 \s-1GOODS\s0 \s-1OR\s0 \s-1SERVICES\s0; \s-1LOSS\s0
1344\&\s-1OF\s0 \s-1USE\s0, \s-1DATA\s0, \s-1OR\s0 \s-1PROFITS\s0; \s-1OR\s0 \s-1BUSINESS\s0 \s-1INTERRUPTION\s0) \s-1HOWEVER\s0 \s-1CAUSED\s0 \s-1AND\s0
1345\&\s-1ON\s0 \s-1ANY\s0 \s-1THEORY\s0 \s-1OF\s0 \s-1LIABILITY\s0, \s-1WHETHER\s0 \s-1IN\s0 \s-1CONTRACT\s0, \s-1STRICT\s0 \s-1LIABILITY\s0, \s-1OR\s0
1346\&\s-1TORT\s0 (\s-1INCLUDING\s0 \s-1NEGLIGENCE\s0 \s-1OR\s0 \s-1OTHERWISE\s0) \s-1ARISING\s0 \s-1IN\s0 \s-1ANY\s0 \s-1WAY\s0 \s-1OUT\s0 \s-1OF\s0 \s-1THE\s0
1347\&\s-1USE\s0 \s-1OF\s0 \s-1THIS\s0 \s-1SOFTWARE\s0, \s-1EVEN\s0 \s-1IF\s0 \s-1ADVISED\s0 \s-1OF\s0 \s-1THE\s0 \s-1POSSIBILITY\s0 \s-1OF\s0 \s-1SUCH\s0
1348\&\s-1DAMAGE\s0.
1349.PP
1350In other words: Use at your own risk. Provided as is. Your mileage
1351may vary. Read the source, Luke!
1352.PP
1353And finally, just to be sure:
1354.PP
1355Any Use of This Product, in Any Manner Whatsoever, Will Increase the
1356Amount of Disorder in the Universe. Although No Liability Is Implied
1357Herein, the Consumer Is Warned That This Process Will Ultimately Lead
1358to the Heat Death of the Universe.