Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

expect send is not working

by starzstar (Initiate)
on Aug 02, 2013 at 10:34 UTC ( #1047577=perlquestion: print w/replies, xml ) Need Help??
starzstar has asked for the wisdom of the Perl Monks concerning the following question:

I have the following perl expect script, the send function does not work, basically I need to enter 'y' for an interactive script , but the following script does not send 'y' can some one please check why send does not work. Thanks ..
use Expect; my $login ="/my/app/test autoupdate"; my $update = Expect->spawn($login) or die "Can't run autoupdate : $!\n +"; print "Autoupdate $login\n"; if($update->expect(6,'Update?')){ print "spawn continue"; my $exp = Expect->send("y\r");} else { print "not spawn";} print "Test is $exp";

Replies are listed 'Best First'.
Re: expect send is not working
by Loops (Curate) on Aug 02, 2013 at 11:22 UTC

    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.

      Thanks a lot Loops, it worked fine with your example.
Re: expect send is not working
by choroba (Bishop) on Aug 02, 2013 at 12:39 UTC
    Crossposted at StackOverflow. It is considered polite to inform about crossposting, so that people not attending both sites do not waste their efforts solving a problem already closed at the other end of the Internet.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Sorry, I will make it point to inform about crossposting for future posts.
Re: expect send is not working
by kcott (Chancellor) on Aug 02, 2013 at 11:12 UTC

    G'day starzstar,

    Welcome to the monastery.

    Perhaps you want a newline instead of a carriage return, i.e. "y\n" instead of "y\r".

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047577]
Approved by kcott
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2018-06-20 12:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.