Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from a filehandle for a set period of time

Status
Not open for further replies.

Loon

Programmer
May 24, 2000
100
GB
Folks,<br><br>&nbsp;&nbsp;is there a good/simple way of reading from a file descriptor for up to a certain amount of time. For example if I am using a # as my end of input character and have $read_timer set to 5 seconds:<br><FONT FACE=monospace><br>&nbsp;&nbsp;&nbsp;&nbsp;#Select on STDIN <br>&nbsp;&nbsp;&nbsp;($_nfound,$_timeleft) = select ($_std_in, undef, undef, $read_timer);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;# if we get something...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($_nfound gt &quot;0&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#...read the data on STDIN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$_cdata = &quot;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;read STDIN, $_cdata, 2048;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$msg .= $_cdata;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></font><br>My problem is that once something is on STDIN (I type something and then press &lt;return&gt;) the Perl proggy will wait for the # character before exiting. <br><br>What I need is for the read to exit either on the # character or after 5 seconds. I've tried several things and am left with the idea of taking the system time upon entering the function, adding read timer to it and the checking the system time every iteration through a while loop (reading a character at a time rather than 2048K).<br><br>Will that work? Does anyone have a better way of doing things?<br><br>Many thanks in advance!<br>Loon
 
+5 mins.....<br><br>After a bit of testing I realise that Programming Perl was confusing me, I thought that <FONT FACE=monospace>read</font> would return with the <FONT FACE=monospace>size</font> of the message read after reading <FONT FACE=monospace>end of file</font>.. whereas if you are not using the same <FONT FACE=monospace>end of file</font> marker, <FONT FACE=monospace>read</font> will wait until it has read the size you originally specified.<br><br>Loon
 
I've never tried this, but, can you do something like....<br><FONT FACE=monospace><br>while (&lt;STDIN&gt;)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;$msg .= $_;<br>&nbsp;&nbsp;&nbsp;&nbsp;if ($msg =~ /\#/) { last; } <font color=red># break out of <i>while</i> if we see a '#'</font><br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></font> <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
goBoating,<br><br>&nbsp;&nbsp;&nbsp;&nbsp;that works fine getting the escape character. However, the problem I have remaining is that I want to read input for a set amount of time, e.g. 5 seconds. <br><br>&nbsp;&nbsp;&nbsp;&nbsp;During that time the select() may expire - which is fine and the function exits.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;During that time a string may be read from STDIN terminated by a # which causes the function to exit.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;During that function a string may be read from STDIN that is not terminated by a #. In this case the program hangs on STDIN, when the next carriage return flushes STDIN to my program, the function realises that it has timed out and exits. <br><br>&nbsp;&nbsp;&nbsp;&nbsp;Therefore my problem now is - how do I either flush STDIN from my programs end (I'm not sure this is possible in any language) or have some way of interrupting the STDIN read.. and I don't really want to have to use signals.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;*sigh*<br>Thanks for help!<br>Loon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top