PerlScholar has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
I'm quite new to perl and at the moment i'm working on a config file. I've looked at a few modules for creating simple configs and XML:Simple is the one which was more easy to understand for me. I've put together a short config but need help with how to access this from my main script i.e how to refer to the variables in my config.Any help would be great. Thanks!

Main script

use XML::Simple;
use Data::Dumper;
my $config = XMLin();
print Dumper($config);

Config.xml

<Config>
<Application Name="App1">
<Location Name="Loc1">
<CDrive>C:\Temp</CDrive>
<ZDrive>Z:\Temp</ZDrive>
</Location>
</Application>
</Config>
Dumper

$VAR1 = {
'Application' => {
'Location' => {
'ZDrive' => 'Z:\\Temp',
'CDrive' => 'C:\\Temp',
'Name' => 'Loc1'
},
'Name' => 'App1'
}
};

Replies are listed 'Best First'.
Re: XML:Simple Config
by dasgar (Priest) on Sep 01, 2010 at 20:25 UTC

    If you look at the output of the Dumper, it will tell you how to access the complex structure. Anything that is in curly brackets is a hash and anything in square brackets is an array.

    I modified your code as follows:

    use strict; use XML::Simple; use Data::Dumper; my $config = XMLin('config.xml'); print Dumper($config); print "$config->{Application}->{Name}\n"; print "$config->{Application}->{Location}->{Name}\n"; print "$config->{Application}->{Location}->{ZDrive}\n"; print "$config->{Application}->{Location}->{CDrive}\n";

    In your sample XML file, there's basically 4 pieces of information stored. The last 4 print statements accesses those pieces. Here's the output.

    $VAR1 = { 'Application' => { 'Location' => { 'ZDrive' => 'Z:\\Temp', 'CDrive' => 'C:\\Temp', 'Name' => 'Loc1' }, 'Name' => 'App1' } }; App1 Loc1 Z:\Temp C:\Temp

    By looking at my modified code and its output, that should help you understand what I'm talking about with using the output from Dumper to decipher the syntax needed to access the complex data structure.

      Exactly what I was looking for. Thanks!

        If you are going to stick with XML::Simple, then you should probably read MJD's tutorial on references and also take a look at this article on avoiding common bugs with using XML::Simple.

        I originally created XML::Simple specifically for the purpose of reading XML config files but I almost never use them any more. It wasn't obvious to me just how bad an idea it was until I did some work with Java :-)

Re: XML:Simple Config
by CountZero (Bishop) on Sep 01, 2010 at 19:09 UTC
    I am probably going against the mainstream, but I like using YAML to write my config files.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: XML:Simple Config
by dHarry (Abbot) on Sep 02, 2010 at 09:02 UTC

    I can't help feeling Config::Simple would ease your life considerably;)

    Q Why make 'Application name' a XML-attribute and 'ZDrive' a XML-element, what's the logic behind it?