[Xml-compile] "inheriting" toXML ?

Mark Overmeer mark at overmeer.net
Tue Oct 23 22:26:13 GMT 2012


* E R (pc88mxer at gmail.com) [121023 19:33]:
> On Tue, Oct 23, 2012 at 2:22 PM, Mark Overmeer <mark at overmeer.net> wrote:
> 
> > Or, you could base all your types of some base-class (very common to
> > have such in OO structures) which implements the identity toXML().
> 
> I've been trying to figure out how to write toXML() to be the
> identity. How would you do that?

The typemap in the writer is rewritten to a hook:

  { type   => $type
  , before =>  sub {
       my ($doc, $values, $path) = @_;
       UNIVERSAL::isa($values, $class) ? $values->toXML($type, $doc) : $values;
    }
  }

which means that anywhere in the data-structure where an element
of type '$type' is used, an object can be specified.  In case there
is an object, it's toXML() method is called.

The CODE references in 'before'-hooks for writers are used to preprocess
the data-structure to be converted, before the standard conversion takes
place.  If your object *is* the data, then you have to return yourself.

Therefore, the identity implementation of toXML() for *writers* should be

   sub toXML() {shift};

When you are not using the writer, or when you do not pass an object of
the correct type in the HASH, this method will never be called.
-- 
Regards,

               MarkOv

------------------------------------------------------------------------
       Mark Overmeer MSc                                MARKOV Solutions
       Mark at Overmeer.net                          solutions at overmeer.net
http://Mark.Overmeer.net                   http://solutions.overmeer.net




More information about the Xml-compile mailing list