Section 10.7. Last-Modified Files


10.7. Last-Modified Files

There are lots of reasons to have an ever-updating list of recently modified files: security, for one. However, it's also a useful system for helping you group working files in your mind. I'm forever loosing track of files I'm working onespecially overnightand this feed helps a great deal. For collaborative working, it's a godsend because you can see what other activity is going on automatically. I also have one of these pointed at a shared directory where friends drop music and silly MPEGs. Altogether, for something so simple, it's remarkably useful.

It's a CGI script that takes a single parameter path, which should be equal to the absolute path on your filesystem that you wish to look underfor example, http://www.example.org/lastmodified.cgi?path=/users/ben.

10.7.1. Walking Through the Code

Let's trek once again into the warnings;, strict;, XML::RSS, and CGI, plus Date::Manip for the dateline and File::Find for its directory traversing capabilities. Lovely. All set? Good.

use warnings; use strict; use XML::RSS; use CGI qw(:standard); use File::Find; use Date::Manip;

By now you should be getting the hang of this. We're firing up the CGI module, snaffling the parameter we are passing to it, then setting up the feed. No great mystery here, in other words. Really, this is somewhat the point: feeds are very simple things. It's the ideas of how to use them that are valuable.

my $cgi             = CGI::new( ); my $start_directory = $cgi->param('path'); my $rss = new XML::RSS( version => '2.0' ); $rss->channel(     title       => "Last modified from $start_directory",     link        => "file:/$start_directory",     description => "A list of all of the files in $start_directory, with their modification dates added in" );

Here's the real meat of the script. We're using the find function from the File::Find module, to traverse the directory we've given it and throw each individual file it finds into a small subroutine:

find( \&search_and_rss, $start_directory ); sub search_and_rss {

Here's an interesting thing. You can use File::Find and the standard stat function to grab the last-modified date of each file and then use Date::Manip to convert it from the standard epoch seconds to the required format. Note that, unlike most other date strings, Date::Manip requires the word "epoch" in front of the raw time value to help it along. This caused me to do a lot of shouting before I remembered it.

    my $last_modified_date = ( stat($File::Find::name) )[9];     my $parsed_date        = &ParseDate("epoch $last_modified_date");     my $pubDate            = &UnixDate( $parsed_date, "%g" );

It's then just a matter of creating each item, and, once the whole directory has been traversed, serving up the feed:

    $rss->add_item(         title       => "$File::Find::name",         link        => "file:\/\/$File::Find::name",         description => "$File::Find::name",         pubDate     => "$pubDate",     ); } print header('application/xml'); print $rss->as_string;

10.7.2. The Entire Listing

#!/usr/bin/perl use warnings; use strict; use XML::RSS; use CGI qw(:standard); use File::Find; use Date::Manip; my $cgi             = CGI::new( ); my $start_directory = $cgi->param('path'); my $rss = new XML::RSS( version => '2.0' ); $rss->channel(     title       => "Last modified from $start_directory",     link        => "file:/$start_directory",     description => "A list of all of the files in $start_directory, with their modification dates added in" ); find( \&search_and_rss, $start_directory ); sub search_and_rss {     my $last_modified_date = ( stat($File::Find::name) )[9];     my $parsed_date        = &ParseDate("epoch $last_modified_date");     my $pubDate            = &UnixDate( $parsed_date, "%g" );     $rss->add_item(         title       => "$File::Find::name",         link        => "file:\/\/$File::Find::name",         description => "$File::Find::name",         pubDate     => "$pubDate",     ); } print header('application/xml'); print $rss->as_string;



    Developing Feeds with RSS and Atom
    Developing Feeds with Rss and Atom
    ISBN: 0596008813
    EAN: 2147483647
    Year: 2003
    Pages: 118

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net