in reply to Use Storable for storing XML::LibXML object

The problem is that an XML::LibXML node is really just a pointer to a libxml struct that lives in XS land. It has no real meaning once the DOM is disposed of.

What to do about it? Firstly, an important question. Say you've stored a node which represents a deeply nested XML element. Once you've retrieved the object from storage, should you only be allowed to query "down" from the node, or should $node->parentNode Just Work™?

Replies are listed 'Best First'.
Re^2: Use Storable for storing XML::LibXML object
by buff (Acolyte) on Jul 05, 2014 at 10:24 UTC
    Thank you. Querying down from the node is enough in my case.

      Then you may be able to define a couple of hooks for freezing and thawing XML::LibXML::Element objects

      # these are completely untested! sub XML::LibXML::Elemment::STORABLE_freeze { my $self = shift; $self->toString; } sub XML::LibXML::Elemment::STORABLE_thaw { my $class = shift; my (undef, $str) = @_; XML::LibXML->parse_xml(string => $str)->documentElement; }

      See the "Hooks" section in the Storable documentation.

        Note that if you serialize two elements from the same document, they will be part of two documents when deserialized.

        Thanks very much for your help. After some tweaking it seems to work. I've ended up with this code:

        sub XML::LibXML::Element::STORABLE_freeze { shift->toString } sub XML::LibXML::Element::STORABLE_attach { 'XML::LibXML'->load_xml(string => pop)->documentElement }

        Thank you very much! I'll give it a try on Monday and post the result here.