XML recursive directory listing

Aristotle on 2005-05-20T01:08:04

After watching a top Pythonista spend so much energy on writing code to do this in Python that it warranted multiple blog entries, I couldn’t resist trying my hand at it in Perl. The result:

#!/usr/bin/perl
use strict;
use warnings;

use File::Find;
use File::Basename;
use XML::Writer;

my $w = XML::Writer->new( OUTPUT => \$_, DATA_MODE => 1, DATA_INDENT => 2 );

$w->xmlDecl( "utf-8" );

find(
    {
        preprocess  => sub { $w->startTag( "directory", name => basename $File::Find::dir ); @_ },
        wanted      => sub { $w->emptyTag( "file", name => $_ ) if -f },
        postprocess => sub { $w->endTag },
    },
    $ARGV[ 0 ]
);

$w->end();

print;

I think I know why O’Reilly’s Perl and XML weighs in at just a mere 224 pages. :-)