Galen has asked for the wisdom of the Perl Monks concerning the following question:

I need to exit a loop when getlines times out. The documentation says:
@lines = $obj->getlines([Timeout => $secs,]);
This method reads and returns the next available lines of data from the object. You can use input_record_separator() to change the notion of what separates a line. The default is "\n".

If a line isn't immediately available, this method blocks waiting for one or more lines, or time-out. You can override the object's timeout for this method using $secs. Also see timeout().

On eof a null array is returned. On time-out or other errors, the error mode action is performed.

A null array is returned for both eof and time-out when errmode is not set to "die". Use eof() and timed_out() to distinguish.

And:

timed_out - time-out indicator $boolean = $obj->timed_out; $prev = $obj->timed_out($boolean); This method indicates if a previous read or write method timed-out. With no argument this method returns true if a previous method timed-out. With an argument it sets the indicator. Normally, only internal methods set this indicator.

So how do I make use of this? I want to exit the following loop when timed_out is true, so that I can execute another command.

while (@rlines = $connect->getlines(Timeout =>1)) { print @rlines; }

Timed_out is a boolean value right? According to the info above, I figured I should just be able to say:

while (@rlines = $connect->getlines(Timeout =>1)) { if ($pop->timed_out) { print "Do something else\n"; exit; } print @rlines;

But this isn't working for me. The value of $pop->timed_out is null.

Edit 2001-03-22 by tye

Replies are listed 'Best First'.
Re: timed_out usage?
by clintp (Curate) on Mar 22, 2001 at 22:44 UTC
    Not to be obtuse...but where are you getting this stuff?

    First assumption is that this is from the IO::Socket module, or something subclassed to that (that's the only standard module I know of with a Timeout property like this). I wasn't aware that getlines() (inherited from IO::Handle) took a Timeout parameter. I wasn't aware it took a parameter at all!

    So now I'm really lost.

    Next, you've got a method called timed_out. Where did this come from? It's not a standard module method at all. (grep..grep..grep... nope).

    Care to give a little background on where all of this is coming from? Maybe we can give a little help then. You've got documentation, I just can't figure out where from...

        Sorry, I should have included more of the code.
        use Net::Telnet (); <--some variables defined here--> $connect = new Net::Telnet (Telnetmode => 1); $connect->open(Host => $hostname, Port => $port); # For debugging print "First, login with $act\n"; print "\n"; <---snip---> print "Now for the RTRV command:\n"; print "command is $rtrv\n"; print "\n"; $connect->print("$rtrv"); (@prematch,$match) = $connect->waitfor('/COMPLD/'); while (@rlines = $connect->getlines(Timeout =>1)) { print @rlines; }

        So, you see it uses the perl telnet.pm module. Pretty simple really, but the problem I'm having is that the data stream doesn't flow consistently, and getlines apparently believes it is finished taking data before all the data is actually retrieved. So, I repeatedly call getlines until finally one of the requests gets a blank line. At this point it times out. My problem is that I want to issue one more command after I've received all the data (I need to log out gracefully). But, when the timeout occurs, the script exits. I can't do anything further. There does appear to be a way to check the status of timed_out, which is what I need (I think). If I time out, then log out, don't just exit. Another solution may be to change the Errmode from the default of "die" to something else.

Re: timed_out usage?
by Galen (Beadle) on Mar 22, 2001 at 22:13 UTC
    Oops, ignore the fact that I used $pop in the second snippet. That should be $connect->timed_out.