YAWriter(3)
π YAWriter(3) β XML::Handler::YAWriter, aka Yet Another Perl SAX XML Writer π
okay bestie, so this is a page from the User Contributed Perl Documentation section of the manual β and it's about <pink>XML::Handler::YAWriter</pink>, which is, say it with me, yet another Perl SAX XML Writer. iconic name, honestly. let's get into it β¨
πΈ NAME πΈ
XML::Handler::YAWriter β Yet another Perl SAX XML Writer π
π¦ SYNOPSIS π¦
use XML::Handler::YAWriter;
my $ya = new XML::Handler::YAWriter( %options );
my $perlsax = new XML::Parser::PerlSAX( 'Handler' => $ya );
that's it. that's the whole vibe. you make a writer, you hand it to a parser, you go live π
β¨ DESCRIPTION β¨
okay so real talk: YAWriter implements Yet Another XML::Handler::Writer. why does this one exist? two reasons, and they're both extremely valid:
- the author needed a flexible escaping technique π
- and wanted some kind of pretty printing π¦
if you create an instance of YAWriter with no options at all, the default behavior is to produce an array of strings containing the XML right here:
@{$ya->{Strings}}
no cap, that's the default. it just... puts the strings there. bestie behavior π«Ά
π Options π
options are given in the usual 'key' => 'value' idiom, because of course they are π
OutputIO::File β this option tellsYAWriterto use an already-open file for output, instead of using$ya->{Strings}to store the array of strings. and ngl the important part: the only thing the object needs to implement is theprintmethod. so literally anything can be used to receive a stream of strings fromYAWriter. anything. a file, a socket, your weird custom little guy. slay π₯AsFilestring β this option makesstart_documentopen the named file andend_documentclose it. use the literal dash-if you want to print on standard output πAsPipestring β this makesstart_documentopen a pipe andend_documentclose it. the pipe is a normal shell command. secure shell comes in handy here, but heads up: it has a 2GB limit on most systems πAsArrayboolean β forces storage of the XML in$ya->{Strings}, even if theOutputoption is given. it's giving "i know you gave me a file but i'm putting it in the array anyway" πAsStringboolean β causesend_documentto return the complete XML document in a single string. most SAX drivers return the value ofend_documentas a result of theirparsemethod. however β and this is the tea β as this may not work with some combinations of SAX drivers and filters, ajoinof$ya->{Strings}in the controlling method is preferred πEncodingstring β changes the default encoding fromUTF-8to anything you like. you should make sure the given data are already in this encoding, or provide anEscapehash soYAWriterknows about the recoding π§Escapehash β okay so this hash defines substitutions that have to be done to any string β with the exception of theprocessing_instructionanddoctype_declmethods, where (in the author's opinion) escaping of target and data would cause more trouble than necessary. fair enough tbh.
the default value for Escape is:
$XML::Handler::YAWriter::escape = {
'&' => '&',
'<' => '<',
'>' => '>',
'"' => '"',
'--' => '--'
};
YAWriter will use an evaluated sub to make the recoding based on a given Escape hash reasonably fast. future versions may use XS to improve this performance bottleneck. we love a noted bottleneck π
Prettyhash β a hash ofstring => booleantuples, to define the kind of prettyprinting. defaults toundef. possible string values are down below πAddHiddenNewlineboolean β add hidden newline before">"AddHiddenAttrTabboolean β add hidden tabulation for attributesCatchEmptyElementboolean β catch empty Elements, apply"/>"compressionCatchWhiteSpaceboolean β catch whitespace with commentsCompactAttrIndentβ places Attributes on the same line as the ElementIsSGMLboolean β this option causesstart_document,processing_instructionanddoctype_declto appear as SGML. the SGML is still well-formed of course, if your SAX events are well-formed. iykyk π¦NoCommentsboolean β supress CommentsNoDTDboolean β supress DTDNoPIboolean β supress Processing InstructionsNoPrologboolean β supress<?xml ... ?>PrologNoWhiteSpaceboolean β supress WhiteSpace to clean documents from prior pretty printing. we stan a cleanup queen π§ΉPrettyWhiteIndentboolean β add visible indent before any eventstringPrettyWhiteNewlineboolean β add visible newlines before any eventstringSAX1boolean (not yet implemented) β output only SAX1 compliant eventstrings. yeah. it's not implemented yet. don't come for me π
πΈ Notes: πΈ
correct handling ofstart_documentandend_documentis required! like, required required. no skipping. β οΈ
the YAWriter Object initialises its structures during start_document and does its cleanup during end_document. if you forget to call start_document, any other method will break during the run. most likely place is the encode method, trying to eval undef as a subroutine. and if you forget to call end_document, you should not use a single instance of YAWriter more than once. that's the whole warning. it's serious π
for small documents AsArray may be the fastest method and AsString the easiest one to receive the output of YAWriter. but AsString and AsArray may run out of memory with infinite SAX streams. the only method XML::Handler::Writer calls on a given Output object is the print method. so it's easy to use a self-written Output object to improve streaming. go off, write your own little output guy π«Ά
a single instance of XML::Handler::YAWriter is able to produce more than one file in a single run. be sure to provide a fresh IO::File as Output before you call start_document, and close this File after calling end_document. or provide a filename in AsFile, so start_document and end_document can open and close its own filehandle. either way, be responsible β¨
automatic recoding between 8bit and 16bit does not work in any Perl correctly ! yeah. that's the sentence. i'm just the messenger π
the author has Perl-5.00563 at home and there they can specify use utf8; in the right places to make recoding work. but they dislike saying use 5.00555; because many systems run 5.00503. relatable struggle tbh π
if you use some 8bit character set internally and want to use national characters, either state your character as Encoding to be ISO-8859-1, or provide an Escape hash similar to the following:
$ya->{'Escape'} = {
'&' => '&',
'<' => '<',
'>' => '>',
'"' => '"',
'--' => '--'
'ΓΆ' => 'ö'
'Γ€' => 'ä'
'ΓΌ' => 'ü'
'Γ' => 'Ö'
'Γ' => 'Ä'
'Γ' => 'Ü'
'Γ' => 'ß'
};
you may abuse YAWriter to clean whitespace from XML documents π
take a look at test.pl, doing just that with an XML::Edifact message, without querying the DTD. this may work in 99% of the cases where you want to get rid of ignorable whitespace caused by the various forms of pretty printing:
my $ya = new XML::Handler::YAWriter(
'Output' => new IO::File ( ">-" );
'Pretty' => {
'NoWhiteSpace'=>1,
'NoComments'=>1,
'AddHiddenNewline'=>1,
'AddHiddenAttrTab'=>1,
} );
XML::Handler::Writer implements any method XML::Parser::PerlSAX wants. this extends the Java SAX1.0 specification π₯ the author has in mind using Pretty=>SAX1=>1 to disable this feature, if abusing YAWriter for a SAX proxy. future plans, bestie. visionary behavior π
π AUTHOR π
Michael Koehne, Kraehe@Copyleft.De π
π«Ά Thanks π«Ά
"Derksen, Eduard (Enno), CSCIO" <enno@att.com> helped with the Escape hash and gave quite a lot of useful comments. we love an appreciative king πβ¨
β SEE ALSO β
perl and XML::Parser::PerlSAX β go read those next, bestie π¦
π POD ERRORS π
hey! the above document had some coding errors, which are explained below. not me being the one to tell you this π
Around line 465:
Non-ASCII character seen before =encoding in ''ΓΆ''. Assuming CP1252
and that's the whole page, bestie π
this was perl v5.42.0, dated 2013-04-22, and the page itself is YAWriter(3) β mwah, go write some well-formed XML, and remember: call start_document. and call end_document. or else. πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-XML-Handler-YAWriter 0.23-160000.2.2 Β· GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms