metaperl has asked for the wisdom of the Perl Monks concerning the following question:
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 | |
by metaperl (Curate) on Apr 15, 2009 at 22:15 UTC | |
by Your Mother (Archbishop) on Apr 15, 2009 at 22:47 UTC |