This is a simple class that solves the problem: "I'm reading from something that sends some input without a newline, and then waits for me to do something". (Common with programs that present interactive prompts, e.g. "tar" on solaris machines may present the prompt "Prepare volume #2 for /dev/rmt/0 and hit return:", with no newline, and wait for a return)

Sure, for the heavy stuff you could use Expect.pm, but this is for when that's overkill, and you'd like to do most reading through <FILEHANDLE>.

The code in "package main" below shows how to use this class. Basically, it lets you wrap a filehandle so that you can call <TIMEOUTFH> on the wrapped result and get lines as though every significant pause were an end-of-line.

This was posted in response to a SoPW post. The original post in that thread may also be used as an example where something like this is useful. However, as it's very likely generally useful in general, I thought I'd put it out here where it could be found.

package TimeOutHandle; use Tie::Handle; use IO::Select; @ISA = qw(Tie::Handle); sub TIEHANDLE { my ($class, $wrappedfh, $timeout) = @_; my ($buffer) = ''; $timeout ||= 5; # default five second timeout return bless [$wrappedfh, $timeout, \$buffer]; } sub READLINE { my $self = shift; my ($inputh, $timeout, $bufref) = @$self; if (length($$bufref)==0) { sysread $inputh,$$bufref,500; if (length($$bufref)==0) {return undef;} } if ($$bufref =~ s{^.*$/}{}) {return $&;} my $s = IO::Select->new(); $s->add($inputh); my @ready = $s->can_read($timeout); while (@ready) { last unless sysread $inputh,$$bufref,500,length($$bufref); if ($$bufref =~ s{^.*$/}{}) {return $&;} @ready = $s->can_read($timeout); } $$bufref =~ s/.+// or return undef; return $&; } package main; # Test code for the above. # feed me some data slowly sub slowdata() { print "a"; sleep 3; print "b"; sleep 6; print "c\nd"; sleep 2; print "e"; } # given a filehandle, tell me what <> does. # Also, give me when <> does it. sub reportlines { my $fh = shift; printf "%02d: __BEGIN__\n", time() % 100; while (<$fh>) { s/\n/\\n/s; printf "%02d: '$_'\n", time() % 100; } printf "%02d: __END__\n", time() % 100; } $|=1; print "Regular filehandle:\n"; open (INDATA, '-|') || do {slowdata();exit();}; reportlines(*INDATA); print "\n5 second timeout:\n"; open (INDATA, '-|') || do {slowdata();exit();}; tie *TIMEOUT5, 'TimeOutHandle', \*INDATA; reportlines(*TIMEOUT5); print "\n1 second timeout:\n"; open (INDATA, '-|') || do {slowdata();exit();}; tie *TIMEOUT1, 'TimeOutHandle', \*INDATA, 1; reportlines(*TIMEOUT1); print "\n10 second timeout:\n"; open (INDATA, '-|') || do {slowdata();exit();}; tie *TIMEOUT10, 'TimeOutHandle', \*INDATA, 10; reportlines(*TIMEOUT10);

In reply to Read a line with <> before \n by fizbin

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.