Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Aristotle (5147)

Aristotle
  pagaltzis@gmx.de
http://plasmasturm.org/

Blah blah blah blah blah [technorati.com]

Journal of Aristotle (5147)

Thursday May 19, 2005
08:08 PM

XML recursive directory listing

[ #24781 ]

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. :-)

Nothing for you to see here. Please move along.