I'm going to ask this question one final time. I asked on perlmonks,
then in #moose, but I cant believe that something is easier in
Class::Prototyped than Moose.
My input data structure is a hash reference and I will be generating
XML from the hash reference.
There are various methods for creating an XML element:
- simple key access: you get the value from the hash refererence and
create a tag from it
- direct: you ignore the hash reference and hard a value to be made as content
- programmatic: a method does some number of computations and returns
the content for a tag
- open ended: a method does whatever it wants with the XML tree and
hash reference with no limits
I have home-grown a way of classifying these methods using
Class::Prototyped, and I have looked at the attribute and Class::MOP
docs, but it seems far far more complicated than my needs.
Of course, I could always transliterate my Class::Prototyped code to
similar Moose:
my $W = XML::Writer->new( DATA_INDENT => 4 , DATA_MODE => 1 ) ;
my $o = Class::Prototyped->new(
xml => $W,
lead => $lead,
getkey => {
Id => 'lead_id' ,
InsuranceType => 'lead_type' ,
first_name => 'fname',
last_name => 'lname',
email => 'email',
phone => 'phone',
phone2 => 'phone2',
address_1_city => 'city',
address_1_state => 'state',
address_1_street1 => 'addr',
address_1_zip => 'zip',
},
direct => { SC => 'ghi',
contact_time => 'Anytime'
} ,
mkdataval => {
'Password' => sub {
my($self)=...@_; rand (1) > 0.5 ?
'Beta_User' : 'prodn_password' } ,
'Leadcount' => sub {
my($self)=...@_; '?' } ,
'Group_Code_List' => sub {
my($self)=...@_; '?' },
own_rent => sub { my($self)=...@_;
$self->lead->{own_home} ?
'Own' : 'Rent' },
}
);
my @schema = sort map { keys %{$o->$_} } qw(getkey direct mkdataval) ;
#qw(Id InsuranceType SC Password Leadcount Group_Code_List
#first_name last_name contact_time email
#);
$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;