Breaking out of loop if nothing pending

N

Neil Shadrach

# perl -we 'die q(KO) unless open(my $h,q(tail -f logfile1.txt|));while (<$h>) { print "L:$_"; }'

Can I easily break out of the 'while' loop if nothing is there to be read
or do I need to use a lower level read function to be able to do this?
 
A

Andreas Kahari

# perl -we 'die q(KO) unless open(my $h,q(tail -f logfile1.txt|));while (<$h>) { print "L:$_"; }'

Can I easily break out of the 'while' loop if nothing is there to be read
or do I need to use a lower level read function to be able to do this?


The way "tail -f" works is that it will wait for input until
killed. So what are your criteria for "nothing to be read"?
 
F

Frank Maas

"Neil Shadrach" :
# perl -we 'die q(KO) unless open(my $h,q(tail -f logfile1.txt|));while
( said:
Can I easily break out of the 'while' loop if nothing is there to be read
or do I need to use a lower level read function to be able to do this?

'tail -f' means 'show me the tail of the file and keep on showing me
additions to the file'. Seems to me that in this context there is no such
thing as 'nothing there to read'. Could you elaborate on your needs?

--Frank
 
A

Anno Siegel

Neil Shadrach said:
# perl -we 'die q(KO) unless open(my $h,q(tail -f logfile1.txt|));while
(<$h>) { print "L:$_"; }'

Can I easily break out of the 'while' loop if nothing is there to be read
or do I need to use a lower level read function to be able to do this?

I don't know what you mean by "nothing to read". The job of "tail -f"
is to wait until there *is* something to read, so most of the time there
will be nothing to read.

If you want to modify the behavior of "tail -f", see the _Perl Cookbook_.
It has one or more recipes to emulate "tail -f" in Perl, so that would
be a good starting point.

Anno
 
N

Neil Shadrach

Anno Siegel:
I don't know what you mean by "nothing to read". The job of "tail -f"
is to wait until there *is* something to read, so most of the time there
will be nothing to read.

Clarification
The 'tail -f' is only in my illustrative example.
The actual application in the 'command|' generates output intermittently - so it might
produce 10 lines in quick succession then nothing for half an hour.
So I'd like to read what is there if there is something there
( maybe not everything if time is scarce ) but not waste time
waiting on it if there is nothing. I've done similar in C with low
level calls but my memory is a poor one and I was optimistically
hoping for ( eg ) a filehandle test that would tell me whether there is
something to be read or not. I'd hate to resort to reading lengths of bytes
and losing the readline behavious if it's not necessary.
 
A

Anno Siegel

Neil Shadrach said:
Anno Siegel:


Clarification
The 'tail -f' is only in my illustrative example.
The actual application in the 'command|' generates output intermittently
- so it might
produce 10 lines in quick succession then nothing for half an hour.
So I'd like to read what is there if there is something there
( maybe not everything if time is scarce ) but not waste time
waiting on it if there is nothing. I've done similar in C with low
level calls but my memory is a poor one and I was optimistically
hoping for ( eg ) a filehandle test that would tell me whether there is
something to be read or not. I'd hate to resort to reading lengths of bytes
and losing the readline behavious if it's not necessary.

I'm still not clear what you want, but you may want select (the 4-arg
form, or the module). See "perldoc -f slect".

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top