"XML is a binary format" - not to nitpick (except I will), but that's just not right. From http://www.w3.org/XML/:
Extensible Markup Language (XML) is a simple, very flexible text format derived from SGML (ISO 8879).
(emphasis mine)
Obviously at some point the XML text has to be encoded to a binary format.
XML::LibXML's
toString method (when called on a document) does do that, so at that point, you are indeed dealing with binary data and should turn off any PerlIO layers on your output handle, as you did in your example. I didn't realize that
$doc->toString returned binary data.