tc1364 has asked for the wisdom of the Perl Monks concerning the following question:

In the below code, once one of the error messages have been matched, how do you tell Expect not to try and match anything else because the command prompt is always returned? In short, the command prompt is always matched, $err_flg never gets set regardless if one of the error messages is matched or not.
sub work () { my $err_flg = 0; $exp->send("command\r"); $exp->expect(5, [ qr/ERROR MESSAGE 1/i, sub { my $self = shift; sleep(1); $err_flg = 1; }], [ qr/ERROR MESSAGE 2/i, sub { my $self = shift; sleep(1); $err_flg = 1; }], [ qr/command prompt/i, sub { my $self = shift; sleep(1); $self->send("command\r"); }] ) or return(1); if ($err_flg == 1) { return(1); } return(0); }

Replies are listed 'Best First'.
Re: Expect Trouble
by shmem (Chancellor) on Nov 27, 2006 at 14:52 UTC
    I guess that your error message is not matched due to the new behaviour
    Changed from older versions is the regular expression handling. By default now all strings passed to expect() are treated as literals. To match a regular expression pass '-re' as a parameter in front of the pattern you want to match as a regexp.
    $object->expect(15, 'match me exactly','-re','match\s+me\s+exactly');

    stated in the Expect docs, since you do not reset $err_flag if the prompt is matched.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      No, either of the error messages can be matched fine if present. The trouble is when one of the error messages are matched, the check for $err_flg is being skipped. This may be caused because the command prompt is matched afterwards.
Re: Expect Trouble
by wulvrine (Friar) on Nov 27, 2006 at 16:17 UTC
    tc1364
    I assume you changed the 'command prompt' to be 'command prompt' not ">" or even "#"? Are you sure the error messages are being matched at all? Have you tried adding prints inside the subroutines to validate which subroutine is fired off (indeed if any are fired off). Also, use that print to validate the $err_flg value.
    What do you mean by 'not to try and match anything because the command prompt is always returned'. Could you help us out with a more detailed version of what you want to happen and what you are seeing happen?

    s&&VALKYRIE &&& print $_^q|!4 =+;' *|