golden.radish has asked for the wisdom of the Perl Monks concerning the following question:
Context: The purpose of this code is simply to prepend a timestamp (with ~microsecond accuracy) to any lines, entries, stdout, whatever you'd care to feed the script.
Observe the microsecond value on the output (it will be something like this, not exactly):#!/usr/bin/perl #timestamp.pl ### use Time::HiRes qw(gettimeofday); $|=1; while (<>) { $timestamp = sprintf("%d.%06d", gettimeofday()); print "$timestamp $_"; } exit; --- touch testfile tail -f testfile | timestamp.pl _in another window_ echo 'test' >> testfile (wait a while) echo 'test' >> testfile (wait a few seconds) echo 'test' >> testfile (wait a little while) echo 'test' >> testfile
1265405350.420517 test 1265405354.420852 test 1265405356.421037 test 1265405362.421530 test
The microsecond value from 'gettimeofday()' increments by a small amount each time a new line is added, and entirely inaccurately. As you add more output to the file, you'll see that the microsecond value increments for every entry, but entirely not what it should be.
Now contrast this output with the behavior when you simply press enter a few times if you run the script by itself:
:~/bin/timestamp.pl 1265405711.519744 1265405711.784186 1265405711.939817 1265405712.144596 1265405712.355313 1265405712.648788
Which is working as expected. The values increment, but not predictably, and certainly not by very small values as observed above.
Any thoughts would be greatly appreciated. What is causing this strange behavior when piping the output from a tail -f into the script?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Time::HiRes strange behavior
by markkawika (Monk) on Feb 05, 2010 at 22:35 UTC | |
by Anonymous Monk on Feb 05, 2010 at 23:12 UTC | |
|
Re: Time::HiRes strange behavior
by BrowserUk (Patriarch) on Feb 05, 2010 at 23:04 UTC | |
by rubasov (Friar) on Feb 06, 2010 at 15:26 UTC | |
by BrowserUk (Patriarch) on Feb 06, 2010 at 15:40 UTC | |
|
Re: Time::HiRes strange behavior
by rubasov (Friar) on Feb 05, 2010 at 23:48 UTC | |
by golden.radish (Initiate) on Feb 06, 2010 at 02:22 UTC | |
by rubasov (Friar) on Feb 06, 2010 at 12:26 UTC | |
by golden.radish (Initiate) on Feb 06, 2010 at 14:55 UTC |