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.

#!/usr/bin/perl ## PerlTail ## Does a "tail" of a file using seek. use strict; my $bigfile = shift or die "Usage: $0 filename <lines> <linesize>\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 > $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 <BIG>; ## Now we print out the rest of the lines: print while(<BIG>); close(BIG);

Update: 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! :)


In reply to Perltail by turnstep

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.