in reply to XML::Simple and variable interpolation

I understand you've got your script working but I just wanted to point out a potential problem with this line:

my $config = XMLin();

Calling XMLin() with absolutely no parameters is a bit of a 'red flag'. In your case, you want the XML file to be found automatically, so leaving the first parameter undefined is OK; but you're expecting the <profile> elements to be 'folded' into a hash keyed on the contents of the 'name' attribute. Unfortunately, if config file is edited and there's only one <profile> element in it, things will go horribly wrong. I'd recommend:

my $config = XMLin(undef, keyattr => { profile => 'name'}, forcearray => [ 'profile' ], );

Read more here.

Replies are listed 'Best First'.
Re: Re: XML::Simple and variable interpolation
by amonotod (Acolyte) on Oct 17, 2003 at 20:22 UTC
    Hi Grant!

    With you being the author of the module, I'm most happy to take your advice. Even though there will be far more than two profiles in the config file, I see your point.

    Now, I have one more question for you: What is the datatype for the $config variable? It was kicking my ass till I figured out how to apply your example from the XML-mail list on Activestate about UUIDs..

    Thanks!
    amonotod

      What is the datatype for the $config variable?

      $config is a scalar (obviously) and contains a reference to a hash*. Hence when you dereference it, you use the arrow notation with curly braces around the hash key, eg: $config->{profile}->{Live}->{dat_file_location}

      Unlike the DOM modules, XML::Simple sticks to the basic Perl data structures so the individual nodes are not objects and therefore have no methods.

      _____
      *There is an obscure condition in which XMLin() would return an array reference, but you're unlikely to happen across that by accident unless you generate your XML by passing XMLout an arrayref.