<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="/templates/default/atom.css" type="text/css" ?>

<feed 
   xmlns="http://www.w3.org/2005/Atom"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/">
    <link href="http://www.jasonfried.info/feeds/atom.xml" rel="self" title="JasonFried.info" type="application/atom+xml" />
    <link href="http://jasonfried.info/"                        rel="alternate"    title="JasonFried.info" type="text/html" />
    <link href="http://jasonfried.info/rss.php?version=2.0"     rel="alternate"    title="JasonFried.info" type="application/rss+xml" />
    <title type="html">JasonFried.info</title>
    <subtitle type="html">My Technical Life</subtitle>
    <icon>http://jasonfried.info/templates/square/img/s9y_banner_small.png</icon>
    <id>http://jasonfried.info/</id>
    <updated>2011-07-20T15:44:22Z</updated>
    <generator uri="http://www.s9y.org/" version="1.5-beta1">Serendipity 1.5-beta1 - http://www.s9y.org/</generator>
    <dc:language>en</dc:language>

    <entry>
        <link href="http://jasonfried.info/archives/3-Perl-Tail-f-Replacement.html" rel="alternate" title="Perl Tail -f Replacement" />
        <author>
            <name>Jason Fried</name>
                    </author>
    
        <published>2011-07-20T15:44:22Z</published>
        <updated>2011-07-20T15:44:22Z</updated>
        <wfw:comment>http://jasonfried.info/wfwcomment.php?cid=3</wfw:comment>
    
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://jasonfried.info/rss.php?version=atom1.0&amp;type=comments&amp;cid=3</wfw:commentRss>
    
            <category scheme="http://jasonfried.info/categories/2-Perl" label="Perl" term="Perl" />
    
        <id>http://jasonfried.info/archives/3-guid.html</id>
        <title type="html">Perl Tail -f Replacement</title>
        <content type="xhtml" xml:base="http://jasonfried.info/">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>If you have ever tried to get a job as a Perl programmer you have probably been asked to create a tail replacement. Well I have. I was bored last night so I made one. Its pretty straight forward, the one issue I can see is that of detecting where the 10th line from the end is, for large files my method may be slow. A character by character search from the tail of the file looking for newline characters could be a faster solution.</p>
<pre>#!/usr/bin/perl
use strict;

#Get filename and optional lines to tail from
my $filepath = shift @ARGV;
my $lines = shift @ARGV || 10;

#Get the inital inode and size
our ($inode,$size) = filestats($filepath);

open (FILE,$filepath) or die "Could not open $filepath for reading\n";

my @lineEnds = ();

#Get a list of all the line ending locations
while() {
   push @lineEnds,tell();
}

my $startloc = $lineEnds[($lines*-1)-1];
@lineEnds = (); #No more need;

#Seek to the line requested
seek FILE,$startloc,0;

#Loop Forever, like tail -f
for(;;) {

#Clear Out any EOF flags
seek FILE,0,1; #Seek 0 bytes from current pos

 #Print out any new file contents, or inital requested lines
 while() {
   print;
 }
 sleep(1); #Sleep to allow other procs to run.
 my ($ninode, $nsize) = filestats($filepath); #Get the current inode and file size;

 if ($ninode != $inode) {
    print STDERR "File Rotated Re-opening file @ $filepath\n";
    close(FILE);
    open(FILE,$filepath) or die "File Could Not be Re-Opened!\n";
    $inode = $ninode;
    $nsize = $nsize;
 }

 if($nsize &lt; $size) {
    print STDERR "File Cleared Out Seeking to Top\n";
    seek FILE,0,0; #Seek to Top
    $size = $nsize;
 }

 #Update File size
 $size = $nsize;

}

#Small untility sub for grabing inode and file size;
sub filestats {
    my $file = shift;
    my @stats = stat $file;
    #Inode and file size
    return ($stats[1],$stats[7]);
}

</pre> 
            </div>
        </content>
        
    </entry>
    <entry>
        <link href="http://jasonfried.info/archives/0-unknown.html" rel="alternate" title="" />
        <author>
            <name></name>
                    </author>
    
        <published>2012-02-06T13:19:25Z</published>
        <updated>2012-02-06T13:19:25Z</updated>
        <wfw:comment>http://jasonfried.info/wfwcomment.php?cid=</wfw:comment>
    
        <slash:comments></slash:comments>
        <wfw:commentRss>http://jasonfried.info/rss.php?version=atom1.0&amp;type=comments&amp;cid=</wfw:commentRss>
    
            <category scheme="http://jasonfried.info/categories/1-Code" label="Code" term="Code" />
            <category scheme="http://jasonfried.info/categories/6-Linux" label="Linux" term="Linux" />
            <category scheme="http://jasonfried.info/categories/4-Networking" label="Networking" term="Networking" />
            <category scheme="http://jasonfried.info/categories/5-Operating-Systems" label="Operating Systems" term="Operating Systems" />
            <category scheme="http://jasonfried.info/categories/7-Programming-Contest" label="Programming Contest" term="Programming Contest" />
            <category scheme="http://jasonfried.info/categories/3-Subversion" label="Subversion" term="Subversion" />
    
        <id>http://jasonfried.info/archives/0-guid.html</id>
        <title type="html"></title>
        
    </entry>

</feed>
