Log in Page Discussion History Go to the site toolbox

Markrh:PerlProgramming:XMLSimpleConfig

From BluWiki

Code, foo.pl

#!perl -w
use strict;
use lib '/home/dir1/dir2/src/perl/lib';

use XML::Simple;
use Data::Dumper;

my $xRef = XMLin();  # no XML file name = use foo.xml as default.
print Dumper($xRef);

my $env = $ARGV[0];

print "db host for $env: [$xRef->{'database'}->{$env}->{'host'}]\n";

XML File, foo.xml

<root>
 <database>
  <dev>
    <host>some_dev_db_host</host>
    <pwd>devPwd</pwd>
    <usr>devUser</usr>
  </dev>

  <cert>
    <host>some_cert_db_host</host>
    <pwd>certPwd</pwd>
    <usr>certUser</usr>
  </cert>

  <prod>
    <host>some_prod_db_host</host>
    <pwd>prodPwd</pwd>
    <usr>prodUser</usr>
  </prod>
 </database>
</root>

Output

./foo.pl dev
$VAR1 = {
          'database' => {
                          'cert' => {
                                      'usr' => 'certUser',
                                      'pwd' => 'certPwd',
                                      'host' => 'some_cert_db_host'
                                    },
                          'prod' => {
                                      'usr' => 'prodUser',
                                      'pwd' => 'prodPwd',
                                      'host' => 'some_prod_db_host'
                                    },
                          'dev' => {
                                     'usr' => 'devUser',
                                     'pwd' => 'devPwd',
                                     'host' => 'some_dev_db_host'
                                   }
                        }
        };
db host for dev: [some_dev_db_host]


Perl Programming, Cookbook

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 1 May 2008, at 12:57.
Disclaimers - About BluWiki