Subject: | XML::Simple should check for overloaded '""' before bailing on refs |
Haven't had a chance to check with this 2.x yet.
IMHO, XML::Simple is slightly too simpleminded when bailing out in value_to_xml(). Instead of croak()ing if it encounters a reference to something it doesn't understand, it should check to see if the thing being referred to can be stringified with an overloaded '""'. This would allow more objects to serialised appropriately.
The particular example I'm seeing is with Image::TIFF::Rational, which is generated in some cases in hashes returned by Image::Info. But I'm sure there are others.
Alternatively perhaps XML::Simple could suppose an 'stringifyunknownrefs' option, or similar -- in some cases this would lead to XML that looked like
<foo>Some::Class=HASH(0xmumble)</foo>
but that may be preferable (and more robust) than XML::Simple croaking.
At the moment too work around this I have code that looks like this:
foreach (keys $hashref) {
next if UNIVERSAL::isa($hashref->{$_}, 'ARRAY');
next if UNIVERSAL::isa($hashref->{$_}, 'HASH');
$hashref->{$_} = "$hashref->{$_}";
}
before calling XMLout, but that's ugly and fragile.
I'll try and send patches in a few days if you think this is a good idea.