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

I'm trying to figure out under what conditions a text file would not be readable on the filesystem while a perl script is writing to that file.

Here is some test code:
=================================== #!/usr/bin/perl -w use strict; print "Begin...\n"; for ( my $i = 0; $i < 5; $i++ ) { print "$i...\n"; sleep 2; } ===================================

I execute this script like this:
$ ./populate.pl > data.txt&


So I background it, because I expect that the script will continue writing while I execute this next command:

$ tail -f data.txt


Now 'tail' just sits there, and doesn't output anything until the populate.pl script is complete.

~HOWEVER~

If I do this with a straight bash script, as follows:
while [ 1 ]; do echo "some text..."; done > data.txt&
..and then do a 'tail -f' on the data.txt file, I can see it's content while the bash script is writing to it.
In both cases I am using '>' to continuously write, and not '>>' to append.

I currently do have other perl scripts which are executed as './script.pl > output.txt', and I am able to do a 'tail' on the output file while the script is running, without any problems.

But the first script above doesn't allow me to tail the data.txt file, so I'm trying to understand the difference, or what would prevent the 'tail' command from working in the first scenario. Anyone have any ideas?

(Please note, I know there are many other ways to accomplish reading from a file while it is being written to; this is just an exercise to increase my understanding of what exactly is going on in this particular situation :)

Bobby

Replies are listed 'Best First'.
Re: Redirecting perl output on the command line
by marto (Cardinal) on Aug 02, 2008 at 18:07 UTC
Re: Redirecting perl output on the command line
by pjotrik (Friar) on Aug 02, 2008 at 18:09 UTC
Re: Redirecting perl output on the command line
by BrowserUk (Patriarch) on Aug 02, 2008 at 18:07 UTC
Re: Redirecting perl output on the command line
by Illuminatus (Curate) on Aug 03, 2008 at 22:28 UTC
    Another quick solution is to write to STDERR directly
    =================================== #!/usr/bin/perl -w use strict; print STDERR "Begin...\n"; for ( my $i = 0; $i < 5; $i++ ) { print STDERR "$i...\n"; sleep 2; } ===================================

    $ ./populate.pl 2> data.txt& (assumes bourne shell or bash)
    STDERR is unbuffered, so you get the behavior you expect