Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expect module 1

Status
Not open for further replies.

LAdProg2005

Programmer
Feb 20, 2006
56
US
I am going through documentation of expect and I came across somethings I do not understand if someone can please help me understand I will appreicate it.

In the below code what does shift signify, i do not understand what it is doing.
Code:
$exp->expect($timeout,
              [ qr/username: /i, sub { my $self = shift;
                                       $self->send("$username\n");
                                       exp_continue; }],
              [ qr/password: /i, sub { my $self = shift;
                                       $self->send("$password\n");
                                       exp_continue; }],
              $shell_prompt);
writing in below manner accomplishes the same result as above?

Code:
$newExpect = new Expect;
$newExpect->expect(5,"username:");
$exp->match();
$exp->send("string(tempUser)\n enter\n ");
If i have gui that allows to input username how can i pass that username to the above code to accept the passed in value from gui? Any pointers will be helpful.

thanks
LAdProg
 
Why are you asking this in a Perl forum? It would be better to ask in the Tcl/Tk forum since that is the language Expect is derived from.

Annihilannic.
 
Your question appears to be answered here:

Expect.pm documentation said:
Furthermore, patterns can now be specified as array refs containing [$regexp, sub { ...}, @optional_subprams] . When the pattern matches, the subroutine is called with parameters ($matched_expect_obj, @optional_subparms). The subroutine can return the symbol `exp_continue' to continue the expect matching with timeout starting anew or return the symbol `exp_continue_timeout' for continuing expect without resetting the timeout count.

So the shift is assigning the reference to the expect object to $self, allowing convenient access to its methods.

If you prefer the "below" manner then use that instead; I imagine the former would be more useful when expecting multiple strings without being certain of the order in which they may arrive.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top