package XML::Config; use strict; use XML::LibXML; use vars qw($VERSION); $VERSION = '0.02'; sub new { my ($proto) = shift; my $class = ref($proto) || $proto; # was it a hash or hashref passed. my $options; if (UNIVERSAL::isa($_[0], 'HASH')) { $options = shift; } else { $options = {@_}; } # check if config file defined and exists. my $file = $options->{file} || die(message => 'no config file defined'); (-r $file) || die('could not find config file'); # parse then get xml data root; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($file); my $xml_r = $doc->getDocumentElement; # check to see if config file and has types defined. my $types = $xml_r->findvalue('/config/@types'); if (!defined($types)) { die('config file must contain types definition') }; # load all config data into hash reference my $self = {}; foreach my $type (split /,/, $types) { #trim whitespace from each type $type =~ s/^\s*(.*?)\s*$/$1/; foreach my $node ($xml_r->findnodes('//'.$type)) { if (defined($node->findnodes('@name'))){ if (!defined($node->findvalue('text()')) || $node->findvalue('text()') eq ' ' || !defined($node->findvalue('text()'))) { $self->{$type}->{$node->findvalue('@name')} = undef; } else { $self->{$type}->{$node->findvalue('@name')} = $node->findvalue('text()'); } } } } # Not needed yet (will need to bless later) # bless($self, $class); return $self; } 1; __END__ =head1 NAME B - Simple XML to hash reference conversion. But it's handy! =head1 SYNOPSIS Example XML config file: /xmilo/data foobar this is a message Now write some perl: #!/usr/bin/perl -w use strict; use XML::Config; my $config = XML::Config->new( file => '/home/iordy/public_html/xmilo/data/config.xml' ); my $dir = $config->{directory}->{dir}; #$dir == '/xmilo/data'; my $msg = $config->{message}->{msg}; #$msg == 'this is a message'; my $var = $config->{value}->{val}; #$var == 'foobar'; =head1 DESCRIPTION This module attempts to make loading config files simpler, more uniform and language independant (the config file not the module). Sure it's just an XML to hash reference conversion but it's handy! This module is licensed under the GPL. See the LICENSE section below for more details. =head1 TAGS The config file layout is very simple, there is only one tag that must be in the config file, ironically it's :) /xmilo/data /home/iordy/ /home/iordy/lib =head2 defines a config area of any XML document, this must containt the types="" attribute with a comma delimited list of the types the module will look for. Example: If you plan on using the xml tags: this is my foo this is my bar Then your config tag will look like: config content =head1 METHODS =head2 new() Call new with a filename and it will return a hash of config values: my $config = XML::Config->new( file => '/home/iordy/config.xml' ); =head2 get() At the moment there is not get() method because it's simpler to just return a hash reference. If for some reason it becomes worthwhile it's easy to add later. Example: my $example = $config->{type}->{name}; =head1 WEBSITE You can find out more info here: http://www.iordy.com =head1 AUTHOR Shane Hanna (iordy@iordy.com) =head1 LICENSE XML::Config; Store your stuff in XML. Copyright (C) 2002 Shane Hanna (iordy@iordy.com) This module is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. =cut