Config::XMLPerl - Configuration files based in XML, where Perl data codes as XML values are enabled.
This module enable the use of normal XML files as configuration files, but also enable Perl data codes as definition of values.
The XML also don't need to be well formatted, soo you can write by hand a wild XML file and use it normally.
use Config::XMLPerl qw(config_load) ; my $config = config_load("conf.xml") ; ## or: my $config = new Config::XMLPerl("conf.xml") ; my $server = $config->{server} ; my $port = $config->{port} ; my $db_user = $config->{DB}{user} ; my $db_pass = $config->{DB}{pass} ; my $db_host = $config->{DB}{host} ; print $config->{text} ; __DATA__ <config server="domain.foo" por="80"> DB => { user => "foo" , pass => "123" , host => "db.domain.foo"} <text> this is a text content </text> </config>
Loads the specified file and returns a XML::Smart object.
To see how to access a XML::Smart object see: XML::Smart
Can be a FILE path, DATA (SCALAR) or FILEHANDLE (GLOB).
The syntax of the configuration file basically is XML, but that accepts extra things.
<?xml version="1.0">
<.willd/> <n*/>
<tagx arg1=123 href=http:/www arg2="quoted">
To use Perl data structure for the values you just set the values as in the content (one per line):
<perl_code_sample foo="123" bar="456"> hash => { user => "foo" , pass => "123" , host => "db.domain.foo"} list => [qw(a b c d)] string => "some text\n with a new line" time => time() </perl_code_sample>
Note that the keys for the values (hash,list,string,time) need to be a valid Perl word. Soo, need to match with:
qr/^\w+[\w:\.]*$/s
The separator (=>) can be "=>", "=", "->" or ":":
<separators> foo => 'like hash' foo -> 'like OO' foo: 123 foo = 'equal' </separators>
** Note that a Perl code can't use more than one line, soo this syntax is wrong:
<error> invalid => { a => 1 , a => 2 , } </error>
To export a wild XML to a well formatted XML use:
my $config = config_load(q` <config server="domain.foo" port=80> DB => { user => "foo" , pass => 123 , host => "db.domain.foo"} </config> `) ; ## Save to a file: $config->save("ok.xml") ; ## print the file: print $config->data() ;
Output:
<?xml version="1.0" encoding="iso-8859-1" ?> <?meta name="GENERATOR" content="XML::Smart/1.5 Perl/5.006001 [MSWin32]" ?> <config server="domain.foo" port="80"> <DB host="db.domain.foo" pass="123" user="foo"/> </config>
** See XML::Smart for complete use of save() and data.
To evaluate the Perl codes of the XML files, a Safe compartment is used, and only OP that wont change the symbol-table or make CODE call will be enabled.
Soo, basically you are only enabled to create anonymous variables.
OPCODE list:
:base_mem null stub pushmark const defined undef preinc i_preinc predec i_predec postinc i_postinc postdec i_postdec int hex oct abs pow multiply i_multiply divide i_divide modulo i_modulo add i_add subtract i_subtract left_shift right_shift bit_and bit_xor bit_or negate i_negate not complement lt i_lt gt i_gt le i_le ge i_ge eq i_eq ne i_ne ncmp i_ncmp slt sgt sle sge seq sne scmp substr stringify length ord chr ucfirst lcfirst uc lc quotemeta trans chop schop chomp schomp match split list lslice reverse cond_expr flip flop andassign orassign and or xor lineseq scope enter leave setstate rv2cv leaveeval gvsv gv gelem padsv padav padhv padany refgen srefgen ref time sort pack unpack
This module was first created for the XML config files of HPL, and was turned into a Perl Module to be published to the public independent of HPL.
XML::Smart, Safe.
Graciliano M. P. <gm@virtuasites.com.br>
I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Config::XMLPerl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Config::XMLPerl
CPAN shell
perl -MCPAN -e shell install Config::XMLPerl
For more information on module installation, please visit the detailed CPAN module installation guide.