in reply to Read XML, Create Dir if not exist
Much shorter, isn't it? And btw, I tested it, it works. HTH,#!/usr/bin/perl -w use strict; use XML::Twig; my $callout_loc = "."; my $file = "./mdlist.xml"; #create the parser (twig) #and tell it what sub to call if it finds an 'md' element my $twig = new XML::Twig (twig_handlers => {md => \&process_md}); #parse the file #process_md will be called every time an 'md' element is found $twig->parsefile ($file); #that's it exit 0; sub process_md { my ($t, $md) = @_; #get the name attribute my $name = $md->att ('name'); #create the dir unless it already exists my $dirname = "$callout_loc/$name"; unless (-d $dirname) { print "Creating $dirname...\n"; mkdir $dirname or die "Could not create dir $dirname: $!\n"; } else { print "$dirname already exists!\n"; } }
pike
|
|---|