in reply to Expect doesn't work as Expected
Your regexp .* is the biggest problem here. .* matches everything, even the empty string! The module goes into an endless loop always matching the empty string and never advancing because nothing is matched or rejected, so new match rounds start at the same place.
If you want to see how it works, change .* to .+ in your original script. Then you will see how it works and you will see sensible values printed for "after match string" and "before match string"
I could get your script to work (although endlessly) by adding \n to your ->send strings and disabling your .* catch all or nothing regexp:
#!/usr/bin/env perl use Expect; #$Expect::Debug = 3; # verbose debug output #$Expect::Log_Stdout = 1; # show chatter for debugging #$Expect::Exp_Internal =1; my $exp = Expect->spawn("units") or die "Cannot spawn unit: $!\n"; $exp->log_stdout(1); $exp->expect(15, [ qr/you have/i => sub { my $exp = shift; #print "you have"; $exp->send("2 km\n"); exp_continue; } ], [ qr/you want/i , sub { my $exp = shift; #print "you want"; $exp->send("m\n"); exp_continue; }], [ 'eof', sub { my $exp = shift; print "eof"; print +$exp->before(); } ], [ qr/blabla.*/, sub { my $exp = shift; print $exp->error; print $exp->before(); exp_continue; } ] );
I suspect you will never get an 'eof' because units runs in an endless loop. You have to send the eof or control-d character to units or simply close the connection when the regexp /you have/ matches a second time, i.e. something like this works:
[ qr/you have/i => sub { my $exp = shift; if (++$counter<=1) { $exp->send("2 km\n"); exp_continue; } else { $exp->soft_close(); + } } ],
PS: Please use 'use warnings;' and 'use strict;'. No modern perl script is complete without them
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Expect doesn't work as Expected
by keenlearner (Acolyte) on Jul 08, 2012 at 06:45 UTC | |
by jethro (Monsignor) on Jul 08, 2012 at 13:37 UTC | |
by keenlearner (Acolyte) on Jul 10, 2012 at 03:10 UTC |