I may be about to commit crimes against CPAN
Simon Wistow
simon at thegestalt.org
Thu Mar 13 19:55:00 GMT 2008
So, I've been pondering writing YA XML module. Actually, I hacked up the
basis over an hour or so yesterday evening just to see if the idea
worked.
Essentially you define a class per element you may receive so
http://code.google.com/apis/gdata/elements.html#gdComments
might be translated into
package gdComments;
use strict;
use base qw(XML::Declare::Element);
sub name { "comments" }
sub namespace { gd => 'http://schemas.google.com/g/2005' }
__PACKAGE__->attributes((
rel => {
type => 'xs:string',
enum => [ map { "http://schemas.google.com/g/2005#$_" }
qw(regular reviews) ],
optional => 1,
},
));
__PACKAGE__->elements((
feedLink => {
type => 'gdFeedLink',
},
));
1;
or similar. gdFeedlink is another module. Then you do
my $comments = gdComments->new;
$comments->rel("http://schemas.google.com/g/2005#regular");
my $feedlink = gdFeedLink->new;
$feedlink->countHint(1);
$feedlink->href("http://example.com");
print "Before:\n".$comments->to_string."\n\n";
$comments->feedLink($feedlink);
print "After:\n".$comments->to_string."\n\n";
attributes have types (string, uri, boolean, integer) and can have
enums. I'm pondering changing things so that attributes and child
elements don't get automatically generated accessors and instead you
have to use
->set($name, $value)
or similar instead.
Parsing and stringification is done via factories. At the moment I have
a ghetto hacked up thing that does it all with strings but
writing a XML::SAX or a XML::LibXML Factory would be really easy. In
fact, it occurs to me that writing a YAML and/or JSON Factory would be
easy too. Maybe I should call it Data::Declare instead.
So, anyway - it was fun to hack it up to this state and see if the proof
of concept works but I can't decide whether to just drop it now or if
it's at all useful.
Thoughts and comments gratefully received,
Simon
More information about the london.pm
mailing list