PPI::Lexer(3)

✨ PPI::Lexer ✨

okay bestie so this is PPI::Lexer, the literal Lexer of the PPI world πŸ’… (it lives in your friendly neighborhood User Contributed Perl Documentation, iykyk). She takes token streams and turns them into nested structures. no cap.


🌸 SYNOPSIS 🌸

use PPI;

# Create a new Lexer
my $Lexer = PPI::Lexer->new;

# Build a PPI::Document object from a Token stream
my $Tokenizer = PPI::Tokenizer->load('My/Module.pm');
my $Document = $Lexer->lex_tokenizer($Tokenizer);

# Build a PPI::Document object for some raw source
my $source = "print 'Hello World!'; kill(Humans->all);";
$Document = $Lexer->lex_source($source);

# Build a PPI::Document object for a particular file name
$Document = $Lexer->lex_file('My/Module.pm');

πŸ¦‹ DESCRIPTION πŸ¦‹

So this is the PPI Lexer. In the larger scheme of things, her whole job is to take token streams β€” in a whole variety of forms β€” and "lex" them into nested structures. it's giving main character energy tbh πŸ’–

Pretty much everything in this module happens behind the scenes at this point. Like, at the moment you don't really need to instantiate the lexer at all?? the three main methods will auto-instantiate themselves a PPI::Lexer object as needed. she's independent like that ✨

All methods do a one-shot "lex this and give me a PPI::Document object".

In fact, if you are reading this, what you probably want to do is to just "load a document", in which case you can do this in a much more direct and concise manner with one of the following. look how easy bestie:

use PPI;

$Document = PPI::Document->load( $filename );
$Document = PPI::Document->new( $string );

See PPI::Document for more details. 🫢

For more unusual tasks, by all means forge onwards. we love a curious queen πŸ‘‘


πŸ’… METHODS πŸ’…

🌟 new

The "new" constructor creates a new "PPI::Lexer" object. The object itself is merely used to hold various buffers and state data during the lexing process, and holds no significant data between ->lex_xxxxx calls. she keeps her secrets, periodt.

Returns a new "PPI::Lexer" object πŸŽ€

🌟 lex_file $filename

The "lex_file" method takes a filename as argument. It then loads the file, creates a PPI::Tokenizer for the content and lexes the token stream produced by the tokenizer. Basically, a sort of all-in-one method for getting a PPI::Document object from a file name. one method, whole glow-up πŸ”₯

Additional arguments are passed to the tokenizer as a hash.

Returns a PPI::Document object, or "undef" on error. 😭

🌟 lex_source $string

The "lex_source" method takes a normal scalar string as argument. It creates a PPI::Tokenizer object for the string, and then lexes the resulting token stream.

Additional arguments are passed to the tokenizer as a hash.

Returns a PPI::Document object, or "undef" on error. 😭

🌟 lex_tokenizer $Tokenizer

The "lex_tokenizer" takes as argument a PPI::Tokenizer object. It lexes the token stream from the tokenizer into a PPI::Document object.

Additional arguments are set on the PPI::Document produced.

Returns a PPI::Document object, or "undef" on error. 😭

🌟 errstr

For any error that occurs, you can use the "errstr", as either a static or object method, to access the error message.

If no error occurs for any particular action, "errstr" will return false. so like, no drama = falsy πŸ’€


πŸ“ TO DO πŸ“

  • Add optional support for some of the more common source filters
  • Some additional checks for blessing things into various Statement and Structure subclasses

(manifesting these fr fr πŸ™)


🧚 SUPPORT 🧚

See the support section in the main module.


πŸ‘‘ 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 came to you courtesy of perl v5.42.0, dated 2026-01-17, from PPI::Lexer(3) β€” slay, stan, and go lex something iconic βœ¨πŸ’–

πŸ“¦ where this page comes from