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


In reply to Re: Expect doesn't work as Expected by jethro
in thread Expect doesn't work as Expected by keenlearner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.