XML::P...iteCopy(3pm)

πŸ“¦
version 0.14 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 (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 ✨πŸ’