Why?
Is not because I can, is because I need. ;-P
Is for a XML module that need to work in the same time in different ways. For example, in XML::Simple you have a hash tree, where inside it you have some parts that are ARRAY ref, soo you need to know when you have an ARRAY and a HASH to access the data, or you need to configure the parser to force the ARRAY to always have an ARRAY on unique points.
With my idea you can just work directly with the HASH or ARRAY to get the data, make a search, etc... All using the basic Perl syntax for HASH, ARRAY, CODE & SCALAR.
Here's an example of how you can access the data:
my $addr = $xml->{config}{server}->('name','=~','sahara')->{address}
+[0] ;
## the same, it just get the first:
my $addr = $xml->{config}{server}->('name','=~','sahara')->{address}
+ ;
## return all the addresses:
my @addrs = $xml->{config}{server}->('name','=~','sahara')->{address
+} ;
## Get the first server node:
my $server = $xml->{config}{server}[0] ;
## or without [0], since wihtout always return the 1st:
my $server = $xml->{config}{server} ;
## Or get the server that have a specific name,
## when you don't know the order:
my $server = $xml->{config}{server}->('name','eq','sahara') ;
my $addr = $server->{address} ;
## Print the content:
print "Server content: $server\n" ;
__DATA__
<config logdir="/var/log/foo/" debugfile="/tmp/foo.debug">
<server name="sahara" osname="solaris" osversion="2.6">
<address>10.0.0.101</address>
<address>10.0.1.101</address>
</server>
<server name="gobi" osname="irix" osversion="6.5">
<address>10.0.0.102</address>
</server>
<server name="kalahari" osname="linux" osversion="2.0.34">
<address>10.0.0.103</address>
<address>10.0.1.103</address>
</server>
</config>
Note that this styles to get the data are still in plan stage. I'm just finishing the Object::MultiType module to see all the resources that I have to do that... But the styles above are already supported! ;-P
Graciliano M. P.
"The creativity is the expression of the liberty". |