<?xml version="1.0" encoding="windows-1252"?>
<node id="63906" title="Perltail" created="2001-03-12 18:05:49" updated="2005-08-15 00:08:25">
<type id="1980">
snippet</type>
<author id="6041">
turnstep</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
&lt;P&gt;A perl program that acts similar to "tail" in unix. 
Just provide the name of the file, an optional 
number of lines, and an optional characters per line.&lt;/P&gt;
</field>
<field name="snippetcode">
&lt;CODE&gt;
#!/usr/bin/perl

## PerlTail
## Does a "tail" of a file using seek.

use strict;

my $bigfile = shift or die "Usage: $0 filename &lt;lines&gt; &lt;linesize&gt;\n";

my $linesback = shift || 10; ## How many "lines" back to read

my $charsperline = shift || 80; ## A rough guess

open(BIG, $bigfile) or die "Could not open $bigfile: $!\n";

## Make a reasonable guess as to how far back to go.
my $gobackchars = $linesback*$charsperline+1;

## Figure out how big it is
my $filesize = -s $bigfile;

## We do not want to go back *too* far:
$gobackchars = $filesize if $gobackchars &gt; $filesize;

seek(BIG,-$gobackchars,2);

## We probably arrived in the middle of a line,
## so throw out everything to the start of a new line
&lt;BIG&gt;;

## Now we print out the rest of the lines:
print while(&lt;BIG&gt;);

close(BIG);
&lt;/CODE&gt;

&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt; There is noting wrong with 
any of the programs/modules already out there. This is 
just a *snippet*, a quick little piece of code I wrote 
that demonstrates a good use of seek. Sheesh! :)&lt;/P&gt;


</field>
</data>
</node>
