XML::P...iteCopy(3pm)
π XML::Parser::LiteCopy β the vibe check π
okay bestie, sit down, hydrate, because we're talking about XML::Parser::LiteCopy, a Lightweight regexp-based XML parser from the User Contributed Perl Documentation. it's giving tiny, it's giving regex-powered, it's giving "works on Perl 5.6 and above and honestly who knows what it does on your machine." let's get into it π¦
β¨ SYNOPSIS β¨
the two ways to do the thing, and yes they're both cute:
use XML::Parser::LiteCopy;
$p1 = new XML::Parser::LiteCopy;
$p1->setHandlers(
Start => sub { shift; print "start: @_\n" },
Char => sub { shift; print "char: @_\n" },
End => sub { shift; print "end: @_\n" },
);
$p1->parse('<foo id="me">Hello World!</foo>');
$p2 = new XML::Parser::LiteCopy
Handlers => {
Start => sub { shift; print "start: @_\n" },
Char => sub { shift; print "char: @_\n" },
End => sub { shift; print "end: @_\n" },
}
;
$p2->parse('<foo id="me">Hello <bar>cruel</bar> World!</foo>');
look at that second one. nested tags. bar inside foo. we love a glow-up π
πΈ DESCRIPTION πΈ
so this Perl module implements an XML parser with an interface similar to XML::Parser. not all the callbacks are supported, but you should be able to use it exactly the way you use XML::Parser, no cap. it does this by using experimental regexp features, which means two things:
- it'll only work on Perl 5.6 and above
- it may behave differently on different platforms π
β οΈ real talk, bestie: you cannot use regular expressions or split inside callbacks. this is not a vibe thing, it's a limitation of perl's regular expression implementation (which is not re-entrant). periodt.
π SUBROUTINES/METHODS π
new
the Constructor. the main character. β¨
as (almost) all SOAP::Lite constructors do, new() returns the object called on when called as an object method. which means the following is effectively a no-op if $obj is an object:
$obj = $obj->new();
new accepts a single named parameter, "Handlers", with a hash ref as its value:
my $parser = XML::Parser::Lite->new(
Handlers => {
Start => sub { shift; print "start: @_\n" },
Char => sub { shift; print "char: @_\n" },
End => sub { shift; print "end: @_\n" },
}
);
the handlers you give it get passed straight to setHandlers. handing off duties, we love to see it π«Ά
setHandlers
sets (or resets) the parsing handlers. it accepts a hash with the handler names and handler code references as parameters. pass undef instead of a code reference and the handler gets replaced by a no-op β like, respectfully, ghosted. π
these are the handlers you can set:
Init
Start
Char
End
Final
CData
Doctype
Comment
PI
all other handlers are ignored. sorry to them.
and if you call setHandlers with no parameters at all? it resets all handlers to no-ops. full factory reset energy π
parse
parses the XML you hand it. and here's the plot twist: unlike XML::Parser's parse method, this parse() only parses strings. no files, no handles, just vibes and a string.
π¦ Handler methods π¦
Init
called before parsing starts. do all your necessary initializations here. it's the getting-ready-with-me moment π
Start
called at the start of each XML node. see XML::Parser for details.
Char
called for each character sequence. heads up though β it may be called multiple times for the characters contained in an XML node (even for every single character π). so your implementation has to make sure it captures all the characters. don't be the person who drops a letter.
End
called at the end of each XML node. see XML::Parser for details
Comment
see XML::Parser for details
PI
see XMLDecl in XML::Parser for details, but also includes other processing instructions. overachiever, honestly. π
Doctype
see XML::Parser for details
Final
called at the end of the parsing process. do any necessary cleanup here. closing shift, wipe down the counters, clock out π
π₯ SEE ALSO π₯
XML::Parser
π COPYRIGHT π
Copyright (C) 2000-2007 Paul Kulchenko. All rights reserved.
Copyright (C) 2008 Martin Kutter. All rights reserved.
Copyright (C) 2009 Cal Henderson. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This parser is based on "shallow parser" http://www.cs.sfu.ca/~cameron/REX.html Copyright (c) 1998, Robert D. Cameron.
π AUTHOR π
Paul Kulchenko (paulclinger@yahoo.com)
Martin Kutter (martin.kutter@fen-net.de)
Additional handlers supplied by Adam Leggett.
Further modifications by Cal Henderson.
and that's the whole page, bestie π this one was perl v5.36.0, dated 2022-11-19 β go forth and parse, you absolute icon β¨π
π¦ where this page comes from
- debian bookworm: libxml-parser-lite-tree-perl 0.14-3 Β· Artistic-2.0
- debian trixie: libxml-parser-lite-tree-perl 0.14-3 Β· Artistic-2.0
- ubuntu noble: libxml-parser-lite-tree-perl 0.14-3 Β· Artistic-2.0
- ubuntu resolute: libxml-parser-lite-tree-perl 0.14-3 Β· Artistic-2.0
- ubuntu stonking: libxml-parser-lite-tree-perl 0.14-3 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms