AcidHawk has asked for the wisdom of the Perl Monks concerning the following question:
As some-one who has had NO formal programming training, I face this issue often. I have just written some code and tested it successfully. I manage to get perl to do what I need it to. What ALWAYS concerns me though, is that I am never confident in whether or not my code is "Best Practice" (for lack of a better expression). I mean are there better ways of doing certain things...? So here is what I have and would like comment on/correction for.
This code reads in the XML file
<?xml version="1.0"?> <!-- This is XML for the X-CallLogger --> <md_map> <md name="SRVONE"> <Sys>ARS</Sys> <Server>HDSrv</Server> <Schema>Sch1</Schema> <User>Demo</User> <Pass></Pass> <Update_Core>Y</Update_Core> </md> <md name="SRVTWO"> <Sys>ARS</Sys> <Server>HDSrv</Server> <Schema>Sch2</Schema> <User>Demo</User> <Pass></Pass> <Update_Core>Y</Update_Core> </md> <md name="SRVTHREE"> <Sys>ARS</Sys> <Server>HDSrv</Server> <Schema>Sch1</Schema> <User>Demo</User> <Pass>pass</Pass> <Update_Core>N</Update_Core> </md> </md_map>
#! /usr/bin/perl use strict; use warnings; use XML::Simple; my ($mdlist, @mds, @dirs, $key, $callout_loc); $callout_loc = "."; my $file = "./mdlist.xml"; my $xs1 = XML::Simple->new(); eval { $mdlist = $xs1->XMLin($file, forcearray => 1, keyattr => 'name' +) }; if ($@) { print "Problems with Loading XML - $@\n"; exit(); } &Get_MDs(); &Get_Dirs(); #Check if Dir created else create it. map { &checkdir } @mds; ###################################################################### +################## ## ## SUB ROUTINES ## ###################################################################### +################## #Get List of MDs sub Get_MDs { foreach $key (keys (%{$mdlist->{md}})) { push(@mds, $mdlist->{md}->{$key}->{'name'} . $key); } } #Get list of dirs sub Get_Dirs { if ( opendir( MDDIR, "$callout_loc" ) ) { while( my $dir = readdir( MDDIR ) ) { next if( ( "." eq $dir ) || ( ".." eq $dir ) ); push( @dirs, $dir ) if( -d "$callout_loc/$dir" ); } closedir( MDDIR ); } else { #Could not OPEN Dir print "Could not open $callout_loc to check for MD Directories +: $!\n"; exit (0); } } #Check if Dir Exists else Create. sub checkdir { print "Checking DIR for $_ ... "; undef my %dir_exists; for (@dirs) { $dir_exists{$_} = 1 } if (!$dir_exists{$_}) { print "Creating.\n"; mkdir ($_); } else { print " Exists\n"; } }
So Basically I have two questions:
Oh and File::Find was not used as I just could not get my mind around diretory depth issues...
-----
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Read XML, Create Dir if not exist
by seattlejohn (Deacon) on Feb 07, 2003 at 09:32 UTC | |
|
Re: Read XML, Create Dir if not exist
by pike (Monk) on Feb 07, 2003 at 12:00 UTC | |
|
•Re: Read XML, Create Dir if not exist
by merlyn (Sage) on Feb 07, 2003 at 16:02 UTC | |
|
(jeffa) Re: Read XML, Create Dir if not exist
by jeffa (Bishop) on Feb 07, 2003 at 13:47 UTC |