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.