At 3:57 PM -0500 10/24/09, Harry Putnam wrote:
Sorry about being tricky with what was an older thread.  But I suspect
that thread died... and no one noticed there was an unaswered question
still there.


Or no one knew the answer.



Shawn C originally suggested I use File::Tail.  There was a short
exchange about why and then I began trying to use File::Tail but
haven't been successful with it.  The details are below:

 Shawn H Corey <shawnhco...@gmail.com> writes:

   open(FILE,"<./named-pipe") or die "Can't Open ./named-pipe: $!";
   while(<FILE>){
     print;
     if(eof){
       sleep 2;
       seek (FILE,0,1);
     }
   }


I would modify the above a little. You are calling eof after each line, which is unnecessary. The input operator <FILE> will return undef whenever eof will return true, so calling eof is redundant. You can make this a little more efficient with something like the following (untested):

  while(1) {
    while(<FILE>) {
      print;
    }
    sleep 1;
    seek(FILE,0,1);
  }

 >> In this case, the above code is a kludge.  You can tell this because the
 program sleeps, rather than waiting on input.  When a program does
 something to emulate what it really should be doing, it introduces code
 that may not work in all cases.

I'm having trouble with File::Tail... or more likely the way I'm
trying to use it is wrongly setup.

But first about that sleep comment.  As I read a little of File::Tail
and its very likely I'm not really understanding what I'm reading but,
it appears to be saying that it `sleeps' at times...  the times are
a little more sophisticated... but none the less sleep.


I do not agree with that "kludge" comment. When waiting for slow events, you can either use "polling" or "interrupts". Polling means continually sleeping and then checking if the event has occurred. The event in this case is additional data appearing at the end of the file. Interrupt would mean blocking until the event has occurred. That can be accomplished using the Unix select statement. In the general case, interrupts are more efficient than polling, because your process does not execute at all until the event occurs.

In this case, however, your process is waiting on another, slower process: the file writer. If you were to use a select statement, at the cost of increased complexity in your program, you would respond faster to new data. However, sleeping for 1 second and trying to read the file is not going to put a large load on your system. So the simple method using sleep will cost you less than one second every time you exhaust the file input in your reader process. If this is acceptable, then using sleep is OK.

File::Tail does have a select method to improve response, but I think that in most cases that is overkill. As you have pointed out, the normal use of File::Tail involves calling the sleep function.


Now the problem.

I've taken the first examples in the perldoc File::Tail output:
  (http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm)

  use File::Tail;
  $file=File::Tail->new("/some/log/file");
  while (defined($line=$file->read)) {
      print "$line";
  }

That looks OK, and works for me on /var/log/system.log, although the delays are longer using File::Tail than Unix tail. Maybe you should try it on a normal file that you write to occasionally with another Perl program.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to