?? expect.pod
字號:
Returns the exit status of $object (if it already exited).=item $object->exp_pty_handle() I<or>=item $object->pty_handle()Returns a string representation of the attached pty, for example:`spawn id(5)' (pty has fileno 5), `handle id(7)' (pty was initializedfrom fileno 7) or `STDIN'. Useful for debugging.=item $object->restart_timeout_upon_receive(0 | 1)If this is set to 1, the expect timeout is retriggered whenever somethingis received from the spawned command. This allows to perform somealiveness testing and still expect for patterns. $exp->restart_timeout_upon_receive(1); $exp->expect($timeout, [ timeout => \&report_timeout ], [ qr/pattern/ => \&handle_pattern], );Now the timeout isn't triggered if the command produces any kind of output,i.e. is still alive, but you can act upon patterns in the output.=item $object->notransfer(1 | 0)Do not truncate the content of the accumulator after a match.Normally, the accumulator is set to the remains that come after thematched string. Note that this setting is per object and not perpattern, so if you want to have normal acting patterns that truncatethe accumulator, you have to add a $exp->set_accum($exp->after);to their callback, e.g. $exp->notransfer(1); $exp->expect($timeout, # accumulator not truncated, pattern1 will match again [ "pattern1" => sub { my $self = shift; ... } ], # accumulator truncated, pattern2 will not match again [ "pattern2" => sub { my $self = shift; ... $self->set_accum($self->after()); } ], );This is only a temporary fix until I can rewrite the pattern matchingpart so it can take that additional -notransfer argument.=item Expect::interconnect(@objects);Read from @objects and print to their @listen_groups until an escape sequenceis matched from one of @objects and the associated function returns 0 or undef.The special escape sequence 'EOF' is matched when an object's handle returnsan end of file. Note that it is not necessary to include objects that onlyaccept data in @objects since the escape sequence is _read_ from an object.Further note that the listen_group for a write-only object is always empty.Why would you want to have objects listening to STDOUT (for example)?By default every member of @objects _as well as every member of its listengroup_ will be set to 'raw -echo' for the duration of interconnection. Setting $object->manual_stty() will stop this behavior per object.The original tty settings will be restored as interconnect exits.For a generic way to interconnect processes, take a look at L<IPC::Run>.=item Expect::test_handles(@objects)Given a set of objects determines which objects' handles have data readyto be read. B<Returns an array> who's members are positions in @objects thathave ready handles. Returns undef if there are no such handles ready.=item Expect::version($version_requested or undef);Returns current version of Expect. As of .99 earlier versions are notsupported. Too many things were changed to make versioning possible.=item $object->interact( C<\*FILEHANDLE, $escape_sequence>)interact() is essentially a macro for calling interconnect() forconnecting 2 processes together. \*FILEHANDLE defaults to \*STDIN and $escape_sequence defaults to undef. Interaction ceases when $escape_sequenceis read from B<FILEHANDLE>, not $object. $object's listen group will consist solely of \*FILEHANDLE for the duration of the interaction.\*FILEHANDLE will not be echoed on STDOUT. =item $object->log_group(0 | 1 | undef)Set/unset logging of $object to its 'listen group'. If set all objectsin the listen group will have output from $object printed to them during$object->expect(), $object->send_slow(), and C<Expect::interconnect($object, ...)>. Default value is on. During creation of $object the setting willmatch the value of $Expect::Log_Group, normally 1.=item $object->log_user(0 | 1 | undef) I<or>=item $object->log_stdout(0 | 1 | undef)Set/unset logging of object's handle to STDOUT. This corresponds to Tcl'slog_user variable. Returns current setting if called without parameters.Default setting is off for initialized handles. When a process object iscreated (not a filehandle initialized with exp_init) the log_stdout settingwill match the value of $Expect::Log_Stdout variable, normally 1.If/when you initialize STDIN it is usually associated with a tty whichwill by default echo to STDOUT anyway, so be careful or you will havemultiple echoes.=item $object->log_file("filename" | $filehandle | \&coderef | undef)Log session to a file. All characters send to or received from thespawned process are written to the file. Normally appends to thelogfile, but you can pass an additional mode of "w" to truncate thefile upon open(): $object->log_file("filename", "w");Returns the logfilehandle.If called with an undef value, stops logging and closes logfile: $object->log_file(undef);If called without argument, returns the logfilehandle: $fh = $object->log_file();Can be set to a code ref, which will be called instead of printingto the logfile: $object->log_file(\&myloggerfunc);=item $object->print_log_file(@strings)Prints to logfile (if opened) or calls the logfile hook function.This allows the user to add arbitraty text to the logfile. Note thatthis could also be done as $object->log_file->print() but would onlywork for log files, not code hooks.=item $object->set_seq($sequence, \&function, \@function_parameters)During Expect->interconnect() if $sequence is read from $object &functionwill be executed with parameters @function_parameters. It is B<_highlyrecommended_> that the escape sequence be a single character since the likelihood is great that the sequence will be broken into to separate readsfrom the $object's handle, making it impossible to strip $sequence fromgetting printed to $object's listen group. \&function should be somethinglike 'main::control_w_function' and @function_parameters should be anarray defined by the caller, passed by reference to set_seq().Your function should return a non-zero value if execution of interconnect()is to resume after the function returns, zero or undefined if interconnect()should return after your function returns.The special sequence 'EOF' matches the end of file being reached by $object.See interconnect() for details.=item $object->set_group(@listener_objects)@listener_objects is the list of objects that should have their handles printed to by $object when Expect::interconnect, $object->expect() or$object->send_slow() are called. Calling w/out parameters will returnthe current list of the listener objects.=item $object->manual_stty(0 | 1 | undef)Sets/unsets whether or not Expect should make reasonable guesses as to when and how to set tty parameters for $object. Will match$Expect::Manual_Stty value (normally 0) when $object is created. If calledwithout parameters manual_stty() will return the current manual_stty setting.=item $object->match_max($maximum_buffer_length | undef) I<or>=item $object->max_accum($maximum_buffer_length | undef)Set the maximum accumulator size for object. This is useful if you thinkthat the accumulator will grow out of hand during expect() calls. Sincethe buffer will be matched by every match_pattern it may get slow if thebuffer gets too large. Returns current value if called without parameters.Not defined by default.=item $object->notransfer(0 | 1)If set, matched strings will not be deleted from the accumulator.Returns current value if called without parameters. False by default.=item $object->exp_pid() I<or>=item $object->pid()Return pid of $object, if one exists. Initialized filehandles will not havepids (of course).=item $object->send_slow($delay, @strings);print each character from each string of @strings one at a time with $delayseconds before each character. This is handy for devices such as modemsthat can be annoying if you send them data too fast. After each character$object will be checked to determine whether or not it has any new data readyand if so update the accumulator for future expect() calls and print the output to STDOUT and @listen_group if log_stdout and log_group areappropriately set.=back=head2 Configurable Package Variables:=item $Expect::DebugDefaults to 0. Newly created objects have a $object->debug() valueof $Expect::Debug. See $object->debug(); =item $Expect::Do_Soft_CloseDefaults to 0. When destroying objects, soft_close may take up to halfa minute to shut everything down. From now on, only hard_close willbe called, which is less polite but still gives the process a chanceto terminate properly. Set this to '1' for old behaviour. =item $Expect::Exp_InternalDefaults to 0. Newly created objects have a $object->exp_internal()value of $Expect::Exp_Internal. See $object->exp_internal().=item $Expect::IgnoreEintrDefaults to 0. If set to 1, when waiting for new data, Expect willignore EINTR errors and restart the select() call instead.=item $Expect::Log_GroupDefaults to 1. Newly created objects have a $object->log_group()value of $Expect::Log_Group. See $object->log_group().=item $Expect::Log_StdoutDefaults to 1 for spawned commands, 0 for file handlesattached with exp_init(). Newly created objects have a$object->log_stdout() value of $Expect::Log_Stdout. See$object->log_stdout().=item $Expect::Manual_SttyDefaults to 0. Newly created objects have a $object->manual_stty()value of $Expect::Manual_Stty. See $object->manual_stty().=item $Expect::Multiline_Matching Defaults to 1. Affects whether or not expect() uses the /m flag fordoing regular expression matching. If set to 1 /m is used. This makes a difference when you are trying to match ^ and $. Ifyou have this on you can match lines in the middle of a page of outputusing ^ and $ instead of it matching the beginning and end of the entireexpression. I think this is handy.=head1 CONTRIBUTIONSLee Eakin <leakin@japh.itg.ti.com> has ported the kibitz scriptfrom Tcl/Expect to Perl/Expect. Jeff Carr <jcarr@linuxmachines.com> provided a simple example of howhandle terminal window resize events (transmitted via the WINCHsignal) in a ssh session.You can find both scripts in the examples/ subdir. Thanks to both!Historical notes:There are still a few lines of code dating back to the inspirationalComm.pl and Chat.pl modules without which this would not have been possible.Kudos to Eric Arnold <Eric.Arnold@Sun.com> and Randal 'Nuke your NT box withone line of perl code' Schwartz<merlyn@stonehenge.com> for making theseavailable to the perl public.As of .98 I think all the old code is toast. No way could this have been donewithout it though. Special thanks to Graham Barr for helping make sense ofthe IO::Handle stuff as well as providing the highly recommended IO::Tty module.=head1 REFERENCESMark Rogaski <rogaski@att.com> wrote:"I figured that you'd like to know that Expect.pm has been very useful to AT&T Labs over the past couple of years (since I first talked to Austin about design decisions). We use Expect.pm for managing the switches in our network via the telnet interface, and such automation has significantly increased our reliability. So, you can honestly say that one of the largest digital networks in existence (AT&T Frame Relay) uses Expect.pm quite extensively."=head1 FAQ - Frequently Asked QuestionsThis is a growing collection of things that might help.Please send you questions that are not answered here toRGiersig@cpan.org=head2 What systems does Expect run on?Expect itself doesn't have real system dependencies, but the underlyingIO::Tty needs pseudoterminals. IO::Stty uses POSIX.pm and Fcntl.pm.I have used it on Solaris, Linux and AIX, others report *BSD and OSFas working. Generally, any modern POSIX Unix should do, but thereare exceptions to every rule. Feedback is appreciated.See L<IO::Tty> for a list of verified systems.=head2 Can I use this module with ActivePerl on Windows?Up to now, the answer was 'No', but this has changed.You still cannot use ActivePerl, but if you use the Cygwin environment(http://sources.redhat.com), which brings its own perl, and havethe latest IO::Tty (v0.05 or later) installed, it should work (feedbackappreciated).=head2 The examples in the tutorial don't work!The tutorial is hopelessly out of date and needs a serious overhaul.I appologize for this, I have concentrated my efforts mainly on thefunctionality. Volunteers welcomed.=head2 How can I find out what Expect is doing?If you set $Expect::Exp_Internal = 1;Expect will tell you very verbosely what it is receiving and sending,what matching it is trying and what it found. You can do this on aper-command base with $exp->exp_internal(1);You can also set $Expect::Debug = 1; # or 2, 3 for more verbose outputor $exp->debug(1);which gives you even more output.=head2 I am seeing the output of the command I spawned. Can I turn that off?Yes, just set $Expect::Log_Stdout = 0;to globally disable it or $exp->log_stdout(0);for just that command. 'log_user' is provided as an alias soTcl/Expect user get a DWIM experience... :-)=head2 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.This is caused by the pty, which has probably 'echo' enabled. Asolution would be to set the pty to raw mode, which in general iscleaner for communication between two programs (no more unexpectedcharacter translations). Unfortunately this would break a lot of oldcode that sends "\r" to the program instead of "\n" (translating thisis also handled by the pty), so I won't add this to Expect just like that.But feel free to experiment with C<$exp-E<gt>raw_pty(1)>.=head2 How do I send control characters to a process?A: You can send any characters to a process with the print command. Torepresent a control character in Perl, use \c followed by the letter. Forexample, control-G can be represented with "\cG" . Note that this will notwork if you single-quote your string. So, to send control-C to a process in$exp, do: print $exp "\cC";Or, if you prefer: $exp->send("\cC");The ability to include control characters in a string like this is providedby Perl, not by Expect.pm . Trying to learn Expect.pm without a thoroughgrounding in Perl can be very daunting. We suggest you look into some ofthe excellent Perl learning material, such as the books _Programming Perl_and _Learning Perl_ by O'Reilly, as well as the extensive online Perldocumentation available through the perldoc command.=head2 My script fails from time to time without any obvious reason. It seems that I am sometimes loosing output from the spawned program.You could be exiting too fast without giving the spawned programenough time to finish. Try adding $exp->soft_close() to terminate theprogram gracefully or do an expect() for 'eof'.Alternatively, try adding a 'sleep 1' after you spawn() the program.It could be that pty creation on your system is just slow (but this israther improbable if you are using the latest IO-Tty).
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -