Firstly you should run your script with:

use strict; use warnings;

If you had, you would have gotten a message that $exp was out of scope when you tried to print it in the final line of your script. You can also enable the maximum debugging level with $update->debug(3), which would give you more information about what is being read and written to the child process.

Where you use Expect->send(...) in your script, you should actually be using the spawned object. So substitute $update->send(...). But in either case, there is no return value from this method.

You might find the example in the documentation instructive, since it gives a way to wait on multiple responses at once. Without knowing what your test application looks like it's hard to know for sure, but you might be able to use something like:

use strict; use warnings; use Expect; # This example assumes that the test script returns "Next Line" after +"y" is pressed my $login ="/my/app/test autoupdate"; my $update = Expect->spawn($login) or die "Can't run autoupdate : $!\n +"; $update->expect(60, [ qr'Update?', sub { print "Sending y"; (shift)->send("y\r"); exp_ +continue; } ], [ qr'Next Line', sub { print "y was accepted"; } ], [ timeout => sub { print "Drag we timed out"; } ], [ eof => sub { print "Ooops, something went wrong"; } ], );

Notice the exp_continue; as the final statement in the "update" clause; this instructs expect to continue on looking for additional matches. Since there is no exp_continue; in the "Next Line" clause, after matching there, expect ends successfully.


In reply to Re: expect send is not working by Loops
in thread expect send is not working by starzstar

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.