Breaking out of loop if nothing pending

Discussion in 'Perl Misc' started by Neil Shadrach, Nov 7, 2003.

  1. # 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?
    Neil Shadrach, Nov 7, 2003
    1. Advertisements

  2. The way "tail -f" works is that it will wait for input until
    killed. So what are your criteria for "nothing to be read"?
    Andreas Kahari, Nov 7, 2003
    1. Advertisements

  3. Neil Shadrach

    Frank Maas Guest

    "Neil Shadrach" :
    '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 Maas, Nov 7, 2003
  4. Neil Shadrach

    Anno Siegel Guest

    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 Siegel, Nov 7, 2003
  5. Anno Siegel:
    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.
    Neil Shadrach, Nov 7, 2003
  6. Neil Shadrach

    Anno Siegel Guest

    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 Siegel, Nov 7, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.