PPI::To...:HereDoc(3)

๐Ÿ’– PPI::Token::HereDoc โ€” the manual, but make it slay ๐Ÿ’–

okay bestie, so this is the page for PPI::Token::HereDoc(3) from the User Contributed Perl Documentation, and honestly? it's giving "chaotic but iconic". let's get into it ๐Ÿซถ


โœจ NAME โœจ

PPI::Token::HereDoc โ€” it's the Token class for the here-doc. yes, THAT here-doc. the one that's lowkey a menace to every parser ever. ๐Ÿ’…


๐ŸŒธ INHERITANCE ๐ŸŒธ

the family tree, and it's a whole lineage:

PPI::Token::HereDoc
isa PPI::Token
    isa PPI::Element

so it's a PPI::Token, which is a PPI::Element. nepotism? no, it's just good OOP, periodt. ๐Ÿ‘‘


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

here-docs are incredibly handy when you're writing Perl. like, genuinely obsessed with them. but when you're parsing Perl? they're incredibly tricky, and the main reason is that they do NOT follow the general flow of input. they're the main character who refuses to walk the designated path. ๐Ÿ˜ญ

they jump ahead and nab lines directly off the input buffer. rude, honestly. and here's the thing โ€” whitespace and newlines may not matter in most Perl code, but bestie, they matter in here-docs.

they're also tricky to store as an object. they look sort of like an operator and a string, but they don't act like it. and they have a second section that should be something like a separate token, but isn't, because a string can span from above the here-doc content to below it. it's giving "I contain multitudes". ๐ŸŒˆ

so when parsing, this is what we do:

firstly, the PPI::Token::HereDoc object does not represent the << operator, or the END_FLAG, or the content, or even the terminator.

it represents all of them at once. yeah. the whole squad in one token. no cap. ๐Ÿ’€

the token itself has only the declaration part as its content:

# This is what the content of a HereDoc token is
<<FOO

# Or this
<<"FOO"

# Or even this
<<      'FOO'

that is, the operator, any whitespace separator, and the quoted or bare terminator. so when you call the content method on a HereDoc token, you get '<< "FOO"'.

as for the content and the terminator โ€” when treated purely in content terms, they do not exist. they're not on that axis, bestie. ๐Ÿ™ˆ

the content is made available with the heredoc method, and the name of the terminator with the terminator method.

to make things work in the way you expect, PPI has to play some games when doing line/column location calculation for tokens, and also during the content parsing and generation processes. games! ๐ŸŽ€

documents cannot simply by recreated by stitching together the token contents, and involve a somewhat more expensive procedure, but the extra expense should be relatively negligible unless you are doing huge quantities of them. (yes, the original says "cannot simply by recreated" โ€” we keep the lore verbatim, we do not fix the ancestors ๐Ÿ’œ)

please note that due to the immature nature of PPI in general, we expect HereDocs to be a rich (bad) source of corner-case bugs for quite a while, but for the most part they should more or less DWYM. ๐Ÿ’…

โš ๏ธ for real, that "rich (bad) source of corner-case bugs" line is straight from the source. consider yourself warned, bestie. ๐Ÿ˜ญ

๐ŸŒŸ Comparison to other string types ๐ŸŒŸ

although technically it can be considered a quote, for the time being HereDocs are being treated as a completely separate Token subclass, and will not be found in a search for PPI::Token::Quote or PPI::Token::QuoteLike objects. it's off doing its own thing, main character energy only. โœจ

this may change in the future, with it most likely to end up under QuoteLike. we'll see. ๐Ÿ”ฎ


๐Ÿ’… METHODS ๐Ÿ’…

although it has the standard set of Token methods, HereDoc objects have a relatively large number of unique methods all of their own. like, it's a lot. it's a whole collection. ๐ŸŽ€

๐Ÿ“ heredoc

the heredoc method is the authoritative method for accessing the contents of the HereDoc object. trust her, she knows. ๐Ÿ‘‘

it returns the contents of the here-doc as a list of newline-terminated strings. if called in scalar context, it returns the number of lines in the here-doc, excluding the terminator line.

๐Ÿงš indentation

the indentation method returns the indentation string of an indented here-doc if that can be determined. if the indented here-doc is damaged (say, missing terminator) or the here-doc was not indented, it returns undef.

โญ terminator

the terminator method returns the name of the terminating string for the here-doc.

it returns the terminating string as an unescaped string (in the rare case the terminator has an escaped quote in it).


๐Ÿ”ฅ TO DO ๐Ÿ”ฅ

the wishlist, and we're manifesting:

  • Implement PPI::Token::Quote interface compatibility
  • Check CPAN for any use of the null here-doc or here-doc-in-s///e
  • Add support for the null here-doc
  • Add support for here-doc in s///e

๐Ÿซถ SUPPORT ๐Ÿซถ

see the support section in the main module. that's it, that's the whole vibe. go look there. ๐Ÿ’–


๐ŸŒˆ AUTHOR ๐ŸŒˆ

Adam Kennedy <adamk@cpan.org>


Copyright 2001 - 2011 Adam Kennedy.

this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

the full text of the license can be found in the LICENSE file included with this module.


and that's the whole page, bestie!! ๐ŸŽ€๐Ÿ’–โœจ this was rendered from perl v5.42.0 and dated 2026-01-17, and that's the last line of the original โ€” so consider this your cute sign-off. mwah. ๐Ÿ’…๐Ÿฆ‹๐ŸŒธ

๐Ÿ“ฆ where this page comes from