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

Hello Respected Monks

Today I felt the urge to write on own tail utiltity (I work on W2k with perl Active State perl 5.8).

So I looked up CPAN and found File::Tail (version .98).

Here is the code I wrote
use warnings; use strict; use File::Tail; my $param = shift; my $file = new File::Tail(name=>$param, maxinterval=>1, adjustafter=>1 +); while(defined(my $line=$file->read)) { print "$line"; }

Now to test this code I create a file called fox.txt and saved it without any data in it.

I ran the script with the command (perl tail.pl fox.txt). Now I added the line "quick brown fox jumps over the lazy dog" to the fox.txt file and saved it. Nothing happened my tail utiltity printed nothing.

Next, I added a new line called "1 2 3" to the fox.txt.

Now the tail utiltity printed my first line "quick brown fox jumps over lazy dog". but not the "1 2 3".

Then I added another line "4 5 6" to the file and saved it. Now the utility printed "1 2 3".

This is not the behavior which I expected. I expected that my utility will print the lines which I added in the most recently saved file. I read the readme file but it didn't say that it is not compatible with my windows platform. So what wrong? why is it not working properly?

Please help.
regards,
Abhishek.

Replies are listed 'Best First'.
Re: File::Tail behaves funny
by The Mad Hatter (Priest) on Apr 07, 2003 at 20:27 UTC
    I just installed it and fiddled around. It seems a little quirky, but as long as you start off with a non-empty file and always leave a newline at the end of the file, it should work ok.

    Hope this helps.

Re: File::Tail behaves funny (final newline)
by tye (Sage) on Apr 07, 2003 at 23:07 UTC

    I think you are not including a final newline. Many Win32 editors don't expect/require a final newline and even display a file containing a single final newline as if it ended with a blank line.

    The Mad Hatter mentions this possibility but I think more stress on this possibility was warranted.

    File::Tail will not give you a line until it sees the end of that line. When it sees a line with no final newline at the end of the file, it assumes that it managed to catch the line in the middle of being written and it waits for the rest of the line to show up. In practice, this is usually the case.

    Most things that write lines include the newline, because it takes too much effort to figure out whether you are writing the last line or not [Also because Unix and at least some other non-Win32 operating systems don't always take kindly to the newline being left of the last line in the file (Win32 seems to go out of its way to make this okay, such as including a blank line before the command prompt -- type "echo hi" as a quick way to see this)].

                    - tye

      You are absolutely right!. The strange behavior is occuring because when I add a line "a b c" i don't press enter after it.

      If I do, then the utiltity works the way I want to (except that I have to wait for 10 seconds to see the changes... I would like to reduce this to one second).

      Also, Does the unix tail -f utiltity also depend on the carriage return at the end of the line?

      regards,
      Abhishek.
Re: File::Tail behaves funny
by BrowserUk (Patriarch) on Apr 07, 2003 at 21:26 UTC

    I was unable to reproduce your problem using AS/5.8 and File::Tail 0.98. How are you appending the lines to the file fox.txt?

    Total speculation: Is it possible that your using an editor that is buffering the output and only updating the file every 10 seconds or so? Are you explicitly re-saving the file after the additions, or are you just adding to the file already opened in the editor and allowing the autosave feature to do the saving?


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: File::Tail behaves funny
by Mr. Muskrat (Canon) on Apr 07, 2003 at 20:31 UTC

    Are you waiting at least 10 seconds before expecting a new line to show up? Why do I ask? You set both maxinterval and adjustafter to 1 second but left interval at the default of 10 seconds.

    I could be way off base though.

      Yes I am waiting for more than 10 seconds. This time I started with a non empty file. (with the string "quick brown fox jumps over lazy dog" already there).

      Now I added a line "1 2 3" and saved the file. The utiltity actually printed a blank line.

      Then I waited for 15 seconds and I added line "a b c d" to the file and saved it. Now the utiltity printed "1 2 3"

      regards,
      Abhishek.