... 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.


In reply to Re^2: Perl Expect by pryrt
in thread Perl Expect by Anonymous Monk

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.