GRNOC-Config
view release on metacpan or search on metacpan
lib/GRNOC/Config.pm view on Meta::CPAN
#--------------------------------------------------------------------
#----- Copyright(C) 2015 The Trustees of Indiana University
#--------------------------------------------------------------------
#----- a simple config module for use in all GRNOC programs with passwords
#-----
#---------------------------------------------------------------------
package GRNOC::Config;
use warnings;
use strict;
require Data::Dumper;
require XML::Simple;
require XML::XPath;
require XML::LibXML;
=head1 NAME
GRNOC::Config - The GRNOC Default Config parser
=head1 VERSION
Version 1.0.9
=cut
our $VERSION = '1.0.9';
=head1 SYNOPSIS
A module to allow everyone to access config files in a fairly standard way.
Uses XML::XPath and XML::Simple to parse our XML files, and stores all configs it has access to in this module
Setting debug to true will give you extra debugging output (default is off)
Setting force_array to true will return everything in an array even if there is only 1 object returned (default is on)
When asking for attributes denoted by '@' it will return only the attribute(s) without the hash
use GRNOC::Config;
my $foo = GRNOC::Config->new(config_file => '/etc/grnoc/db.xml', debug => 0, force_array => 0 schema => '/path/to/schema.csd')
my $db_username = $foo->get("/config/db/credentials");
print $db_username->{'user'};
print $db_username->{'password'};
#just 1 attribute
my $user = $foo->get("/config/db/credentials[1]/@user");
my $password = $foo->get("/config/db/credentials[1]/@password");
#if I have more than 1 result I get an array
my $hosts = $foo->get("/config/hosts") or die Dumper($foo->get_error());
foreach my $host ($hosts){
print $host->{'hostname'};
}
##turn debugging on
$foo->{'debug'} = 1;
##get errors
print Dumper($foo->get_errors());
# I always want an array... even if I only get 1 result
$foo->{'force_array'} = 1;
$db_username = $foo->get("/config/db/credentials") or die Dumper($foo->get_error());
print @{db_username}[0]->{'user'}
print @{db_username}[0]->{'password'}
$user = $foo->get("/config/db/credentials[1]/@user") or die Dumper($foo->get_error());
$password = $foo->get("/config/db/credentials[1]/@password") or die Dumper($foo->get_error());
$user = @{$user}[0];
$password = @{$password}[0];
my $valid = $foo->validate();
if(!$valid){
print STDERR Dumper($foo->get_error());
}
my $valid2 = $foo->validate("/path/to/new/schema.xsd");
...
=cut
=head2 getOLD
returns the requested data from the config file must pass in the path of the node/attribute you want
from the XML. Attributes are denoted by '@'
to get an attribute the call would look like
$foo->get("/path/to/@object");
=cut
sub getOLD{
my $self = shift;
my $path = shift;
if($path eq ''){
$self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
return undef;
}
if(!defined($self->{'config'})){
$self->{'error'}{'msg'} = "Config not loaded!!\n";
return undef;
}
my $xp = $self->{'config'};
my $exists;
eval { $exists = $xp->exists($path); }; $self->{'error'}{'error'} = $@ if $@;
if($exists){
if($self->{'debug'}){
print STDERR $path . " exists!";
}
$self->{'error'} = {};
my $nodeset;
eval { $nodeset = $xp->find($path); }; $self->{'error'}{'error'} = $@ if $@;
my @results;
my @nodelist;
eval { @nodelist= $nodeset->get_nodelist; }; $self->{'error'}{'error'} = $@ if $@;
if($#nodelist <= 1 && !($self->{'force_array'})){
my $temp;
eval { $temp = XML::XPath::XMLParser::as_string($nodelist[0]); }; $self->{'error'}{'error'} = $@ if $@;
if($temp =~ /^<.*\/>$/){
return XML::Simple::XMLin($temp,ForceArray => $self->{'force_array'});
( run in 1.669 second using v1.01-cache-2.11-cpan-140bd7fdf52 )