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>
It checks for each name attribute and then checks if a corresponding dir exists in the specified path. If a dir is not there is creates it.
#! /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...

-----
Of all the things I've lost in my life, its my mind I miss the most.

In reply to Read XML, Create Dir if not exist by AcidHawk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.