MKDoc:...::Token(3pm)
πΈ MKDoc:...::Token β¨
okay bestie, grab a boba because we're about to talk about a whole module β it's MKDoc::...::Token, a lil Perl thingy that lives in the User Contributed Perl Documentation section of your man pages. think of it as one (1) single XML token, in object form. tiny. iconic. does exactly one job. we love her π
π NAME π
MKDoc::XML::Token β it's an XML Token Object. that's the whole vibe. one token, one object, no cap β¨
π SYNOPSIS π
here's the whole flex, verbatim, because we do not paraphrase code in this house:
my $tokens = MKDoc::XML::Tokenizer->process_data ($some_xml);
foreach my $token (@{$tokens})
{
print "'" . $token->as_string() . "' is text\n" if (defined $token->text());
print "'" . $token->as_string() . "' is a self closing tag\n" if (defined $token->tag_self_close());
print "'" . $token->as_string() . "' is an opening tag\n" if (defined $token->tag_open());
print "'" . $token->as_string() . "' is a closing tag\n" if (defined $token->tag_close());
print "'" . $token->as_string() . "' is a processing instruction\n" if (defined $token->pi());
print "'" . $token->as_string() . "' is a declaration\n" if (defined $token->declaration());
print "'" . $token->as_string() . "' is a comment\n" if (defined $token->comment());
print "'" . $token->as_string() . "' is a tag\n" if (defined $token->tag());
print "'" . $token->as_string() . "' is a pseudo-tag (NOT text and NOT tag)\n" if (defined $token->pseudotag());
print "'" . $token->as_string() . "' is a leaf token (NOT opening tag)\n" if (defined $token->leaf());
}
so you loop through your tokens, you ask each one "bestie what ARE you", and she tells you. it's giving β¨ main character energy β¨ but for XML parsing. fr fr.
π SUMMARY π
okay so MKDoc::XML::Token is an object that represents an XML token produced by MKDoc::XML::Tokenizer. that's the whole origin story. no tragic backstory, just tokens π¦
she comes with a whole set of methods so you can figure out what type of token she is, plus some extra ones that help you build a parsed tree β exactly like the ones in MKDoc::XML::TreeBuilder. so like, she's a helper, she's a queen, she does both π
π API π
π my $token = new MKDoc::XML::Token ($string_token);
constructs a brand new MKDoc::XML::Token object. fresh out the box. newborn token, zero thoughts, just vibes π
π my $string_token = $token->as_string();
hands you back the string representation of this token, and the way she does it means that:
MKDoc::XML::Token->new ($token)->as_string eq $token
is a tautology. like, round-trip so clean it's basically a mirror. you put a string in, you get that exact string out. no notes. perfection πΈ
π my $node = $token->leaf();
if this token is NOT an opening tag, this method returns her corresponding node structure β the same thing you'd get from $token->text(), $token->tag_self_close(), etc.
otherwise, bestie, you get undef. π
π my $node = $token->pseudotag();
if this token is a comment, a declaration or a processing instruction, this returns $token->tag_comment(), $token_declaration() or $token->pi() respectively.
otherwise? undef. π
π my $node = $token->tag();
if this token is an opening, closing or self closing tag, you get $token->tag_open(), $token->tag_close() or $token->tag_self_close() respectively.
otherwise: undef. that's the pattern, get used to her π
π my $node = $token->comment();
if this token object represents a declaration, here's the structure you get back:
# this is <!-- I like Pie. Pie is good -->
{
_tag => '~comment',
text => ' I like Pie. Pie is good ',
}
(and yes, the original really does say "declaration" right there in the comment method. we don't fix it, we just report it π)
otherwise: undef.
π my $node = $token->declaration();
if this token object represents a declaration, you get this structure:
# this is <!DOCTYPE foo>
{
_tag => '~declaration',
text => 'DOCTYPE foo',
}
otherwise: undef. classic.
π my $node = $token->pi();
if this token object represents a processing instruction, you get:
# this is <?xml version="1.0" charset="UTF-8"?>
{
_tag => '~pi',
text => 'xml version="1.0" charset="UTF-8"',
}
otherwise: undef. as always β¨
π my $node = $token->tag_open();
if this token object represents an opening tag, you get:
# this is <aTag foo="bar" baz="buz">
{
_tag => 'aTag',
_open => 1,
_close => 0,
foo => 'bar',
baz => 'buz',
}
look at those attributes just... living there as hash keys. obsessed. π
otherwise: undef.
π my $node = $token->tag_close();
if this token object represents a closing tag, you get:
# this is </aTag>
{
_tag => 'aTag',
_open => 0,
_close => 1,
}
notice she flipped _open and _close. balanced. symmetrical. slay πΈ
otherwise: undef.
π my $node = $token->tag_self_close();
if this token object represents a self-closing tag, you get:
# this is <aTag foo="bar" baz="buz" />
{
_tag => 'aTag',
_open => 1,
_close => 1,
foo => 'bar',
baz => 'buz',
}
_open AND _close both true β because she is literally both, she contains multitudes, she's the moment π
otherwise: undef.
π my $node = $token->text();
if this token object represents a piece of text, then that text is what you get back. otherwise: undef.
β οΈ TRAP!$token->text()returns a FALSE value if the text happens to be'0'or''. so like, the text could absolutely be there and you'd still get a falsey answer. so really, bestie, you should use:perl if (defined $token->text()) { ... do stuff... }check withdefined, not truthiness. do not get got. i am begging ππ
π§ NOTES π§
MKDoc::XML::Token works together with MKDoc::XML::Tokenizer β and that combo is the move when building a full tree is not necessary. if you DO need a tree though? go look at MKDoc::XML::TreeBuilder. different tool, different vibe, same family π«Ά
β¨ AUTHOR β¨
Copyright 2003 - MKDoc Holdings Ltd.
Author: Jean-Michel Hiver
This module is free software and is distributed under the same license as Perl itself. Use it at your own risk. (yes bestie, at your OWN risk, we said what we said π )
π SEE ALSO π
MKDoc::XML::TokenizerMKDoc::XML::TreeBuilder
go read them, they're her friends π¦
and that's the whole page, bestie!! this one shipped as perl v5.36.0, dated 2022-10-13, straight out of the MKDoc:...::Token(3pm) man page. mwah. go parse something beautiful πβ¨π
π¦ where this page comes from
- ubuntu noble: libmkdoc-xml-perl 0.75-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmkdoc-xml-perl 0.75-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmkdoc-xml-perl 0.75-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms