On Thu, Apr 16, 2009 at 11:08:11AM -0400, Terrence Brannon wrote:
> my @schema = sort map { keys %{$o->$_} } qw(getkey direct mkdataval) ;
> $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;
These are the only bits that have anything to do with OO; your "object", above,
is just a glorified hash, allowing you to do $o->direct instead of
$o->{direct}.
You could encapsulate the XML writing inside a couple of methods:
sub data_value_for {
my ($self, $input, $tag) = @_;
# dispatch based on direct/getkey/whatever
}
sub write_xml {
my ($self, $input) = @_;
my @schema = sort map { keys %{$input->{$_}} } ...;
$self->xml->startTag('xmlpost');
$self->xml->dataElement($_ => $self->data_value_for($input, $_))
for @schema;
$self->xml->endTag;
$self->xml->end;
}
That big hashref you're passing in isn't going to change, though, and I don't
think it really needs to. It's input, not object state.
Anyway, none of this has anything to do with Moose, it's just basic OO.
hdp.