in reply to [SOLVED] XML::Twig - '#CDATA' attribute in hashref disappears after first call to XML::Twig::Elt->new()
Is this supposed to happen?
Well, it appears to be intentional, the code includes:
$CDATA = '#CDATA'; ... package XML::Twig::Elt; ... sub new ... if( $atts && defined $atts->{$CDATA}) { delete $atts->{$CDATA}; my $cdata= $class->new( $CDATA => @_); return $class->new( $gi, $atts, $cdata); }
So new assumes that it's ok to modify the hashref. Whether this is good design is debatable, but if you want to just work with the module as is, the workaround provided by the AM here creates a shallow copy of the hash, so that new is free to delete keys. Alternatively, you could do what the above code is doing, that is, wrap the data in a CDATA yourself.
|
|---|