Hello, Monks, I am trying to make the expect interact with "units" program. But it does not want to match anything, except qr/.*/ and the $exp->before prints nothing. If you want to test, the units can be obtained from http://www.gnu.org/software/units/
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"); exp_continue; } ], [ qr/you want/i , sub { my $exp = shift; print "you want"; $exp->send("m"); exp_continue; }], [ 'eof', sub { my $exp = shift; print "eof"; print +$exp->before(); } ], [ qr/.*/, sub { my $exp = shift; print $exp->error; print $exp->before(); } ] );
The units executed in the terminal will print these
user@dell:~> units 2526 units, 72 prefixes, 56 nonlinear units You have:
In the log it shows this
spawn id(20): Does `' match: pattern #1: -re `(?^i:you have)'? No. pattern #2: -re `(?^i:you want)'? No. pattern #3: -eof `'? No. pattern #4: -re `(?^:.*)'? YES!! Before match string: `' Match string: `' After match string: `' Matchlist: () Calling hook CODE(0x7fd8217449c0)... Returning from expect successfully. spawn id(20): accumulator: `' Closing spawn id(20).
Before match string: `' Why does it show `' ? what is that string ? And what is wrong with the code ? Thanks

In reply to 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.