Class::XML 0.06 review

Download
by rbytes.net on

Class::XML is a Perl module for simple XML Abstraction. SYNOPSIS package Foo; use base qw/Class::XML/; __PACKAGE__-

License: Perl Artistic License
File size: 17K
Developer: Matt S Trout
0 stars award from rbytes.net

Class::XML is a Perl module for simple XML Abstraction.

SYNOPSIS

package Foo;

use base qw/Class::XML/;

__PACKAGE__->has_attributes(qw/length colour/);
__PACKAGE__->has_child('bar' => Bar);

package Bar;

use base qw/Class::XML/;

__PACKAGE__->has_parent('foo');
__PACKAGE__->has_attribute('counter');

# Meanwhile, in another piece of code -

my $foo = Foo->new( xml => # Or filename or ioref or parser
qq!< foo length="3m" colour="pink" >< bar / >< /foo >! );

$foo->length; # Returns "3m"
$foo->colour("purple"); # Sets colour to purple

print $foo; # Outputs

my $new_bar = new Bar; # Creates empty Bar node

$new_bar->counter("formica");

$foo->bar($new_bar); # Replaces child

$new_bar->foo->colour; # Returns "purple"

$foo->colour(undef); # Deletes colour attribute

print $foo; # Outputs < foo length="3m" >< bar counter="formica" / >< /foo >

Class::XML is designed to make it reasonably easy to create, consume or modify XML from Perl while thinking in terms of Perl objects rather than the available XML APIs; it was written out of a mixture of frustration that JAXB (for Java) and XMLSerializer (for .Net) provided programming capabilities that simply weren't easy to do in Perl with the existing modules, and the sheer pleasure that I've had using Class::DBI.

The aim is to provide a convenient abstraction layer that allows you to put as much of your logic as you like into methods on a class tree, then throw some XML at that tree and get back a tree of objects to work with. It should also be easy to get started with for anybody familiar with Class::DBI (although I doubt you could simply switch them due to the impedance mismatch between XML and relational data) and be pleasant to use from the Template Toolkit.

Finally, all Class::XML objects are also XML::XPath nodes so the full power of XPath is available to you if Class::XML doesn't provide a shortcut to what you're trying to do (but if you find it doesn't on a regular basis, contact me and I'll see if I can fix that.

Requirements:
Perl

Class::XML 0.06 search tags