new tickets from slaven
[andk-cpan-tools.git] / bin / mytail-f.pl
blob545f3888577022fa9ee020b0cff462752b71b108
1 #!/usr/bin/perl
3 =pod
5 with help from perlfaq5
7 Displays the whole file with leading line number and timestamp and
8 switches to 'tail -f' mode when the end is reached.
10 =cut
12 use Time::HiRes qw(time sleep);
13 my $curpos = 0;
14 my $line;
15 my $i = 0;
16 my $file = shift or die "Usage";
17 open GWFILE, $file or die "Could not open '$file': $!";
18 my $lines;
19 while (<GWFILE>) {
20 $lines = $.;
22 close GWFILE;
23 open GWFILE, $file or die "Could not open '$file': $!";
24 for (;;) {
25 for ($curpos = tell(GWFILE); $line = <GWFILE>; $curpos = tell(GWFILE)) {
26 if (++$i > $lines - 10) {
27 my @time = localtime;
28 my $localtime = sprintf "%02d:%02d:%02d", @time[2,1,0];
29 my $fractime = time;
30 $fractime =~ s/\d+\.//;
31 $fractime .= "0000";
32 printf "%5d %s.%s %s", $i, $localtime, substr($fractime,0,4), $line;
35 sleep 2;
36 seek(GWFILE, $curpos, 0); # seek to where we had been