Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Perl Expect

by pryrt (Abbot)
on Jan 11, 2018 at 15:21 UTC ( [id://1207118]=note: print w/replies, xml ) Need Help??


in reply to Re: Perl Expect
in thread Perl Expect

... I suppose you might be able to do something similar...

I haven't used it, but Expect says, for $object->expect($timeout, @match_patterns), "If $timeout is undef Expect will wait forever for a pattern to match". That indicates your supposition is right.

If you want to wait longer, but not forever, you can just set the $timeout accordingly... and maybe use $object->restart_timeout_upon_receive(1), which appears re-start the timer every time new data arrives (so if the command is outputting a word every 15 seconds, and can be considered dead if it goes more than 30 seconds, and the word you're looking for is the fifth word, then you could use a timeout of 30 seconds with the ->restart_timeout_upon_receive(1), and it should ... ah, enough of this "should" stuff:

#!/usr/bin/env perl -l use warnings; use strict; use Expect; foreach my $restart (0, 1) { my $exp = Expect->new(); $exp->restart_timeout_upon_receive($restart); $exp->spawn(qw{perl -le 'foreach(@ARGV) { print $_; sleep 2; } ' w +ait until seeing the fifth word }) or die "cannot spawn: $!\n"; my @return = $exp->expect(4, 'fifth'); print "restart=$restart -> \@return = ", join ', ', map { $_ // '< +undef>' } @return; }
Yep, that timedout on the first run, but continued through to the 'fifth' on the second run.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1207118]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-25 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found