metaperl has asked for the wisdom of the Perl Monks concerning the following question:

I'm going to be producing XML based on some specified requirements. I'm pretty happy with using Class::Prototyped to produce the XML, but Moose is en vogue.

I glanced through the meta-programming facilities of Moose and it seems overly complicated for what I'm doing.

Do you see a module/approach that could improve on my approach to producing XML fields?

# produce XML using XML::Writer my $W = XML::Writer->new( DATA_INDENT => 4 , DATA_MODE => 1 ) ; # Here are the tags we must provide in the XML my @schema = qw(Id InsuranceType SC Password Leadcount Group_Code_List first_name last_name ); my $o = Class::Prototyped->new( xml => $W, lead => $lead, # Some XML elements simply need $lead->{$TagName} a +s their content, so we put them here getkey => { Id => 'lead_id' , InsuranceType => 'lead_type' , first_name => 'fname', last_name => 'lname', }, # Some XML elements have hardcoded values direct => { SC => 'ghi' } , # Some elements need to do some computation before +returning the content on the XML tag mkdataval => { 'Password' => sub { my($self)=@_; rand (1) > +0.5 ? 'Beta_User' : 'prodn_password' } , 'Leadcount' => sub { my($self)=@_; int(rand(1 +0)) } , 'Group_Code_List' => sub { my($self)=@_; '?' +}, }, postproc => sub { # dress up the XML, etc } ); $W->startTag('xmlpost'); for my $tag (@schema) { if (my $data_value = $o->direct->{$tag}) { $o->xml->dataElement($tag => $data_value); } elsif (my $lead_key = $o->getkey->{$tag}) { $o->xml->dataElement($tag => $o->lead->{$lead_key}); } elsif (my $closure = $o->mkdataval->{$tag}) { $o->xml->dataElement($tag => $closure->($o)) ; } elsif (my $slot = $o->reflect->getSlot($tag)) { $o->$tag; } } $W->endTag; $W->end;

Replies are listed 'Best First'.
Re: object-oriented XML generation
by Your Mother (Archbishop) on Apr 15, 2009 at 21:31 UTC

    2¢ worth: use XML::LibXML instead. You can validate against a DTD which describes the required elements/attributes/structure and it's already object oriented.

    You can use Moose to wrap up a LibXML object too letting it handle the XML stuff only and doing your dispatch data handling with your own object stuff. Moose isn't trivial to jump into but it's very powerful and many projects that seem straightforward up front end up being much more complex and if you bake in a simplistic approach and it ends up growing complex you'll regret it.

      I cannot make heads of tails of XML::LibXML ... I simply dont understand where it's coming from.
      You can use Moose to wrap up a LibXML object
      Let me take this moment to pimp Perigrins module XML::Toolkit - it looks pretty wild.. somewhat alpha at the moment. But it's definitely moose-based.

        Yeah, it took me quite awhile to dive the LibXML docs and get a real feel for it. It’s easier/better than it seems from a first read of the docs; which are actually pretty good but quite spread out and without any "narrative."

        XML::Toolkit is interesting looking. Thanks for bringing it up.