Compiler::Lexer(3pm)

✨ NAME ✨

so basically, bestie, this is Compiler::Lexer(3pm) from the User Contributed Perl Documentation, and it's a Lexical Analyzer for Perl5. like, it takes your Perl source and hands you back a whole lil' pile of tokens. iconic behavior honestly πŸ¦‹


🌸 SYNOPSIS 🌸

here's the whole vibe, copy-paste ready, no cap:

use Compiler::Lexer;
use Data::Dumper;

my $filename = $ARGV[0];
open my $fh, '<', $filename or die "Cannot open $filename: $!";
my $script = do { local $/; <$fh> };

my $lexer = Compiler::Lexer->new($filename);
my $tokens = $lexer->tokenize($script);
print Dumper $tokens;

my $modules = $lexer->get_used_modules($script);
print Dumper $modules;

πŸŽ€ METHODS πŸŽ€

my $lexer = Compiler::Lexer->new($options);

make yourself a shiny new instance, periodt. and yes you can build the object from $options in a hash reference β€” very main character of it πŸ’…

here's the options list:

  • filename β€” the file name, obviously, she's the star
  • verbose β€” includes token of Pod, Comment and WhiteSpace. so when you turn this on you get the chatty version, pods and comments and whitespace all up in the token list πŸ™ˆ

$lexer->tokenize($script);

get token objects includes parameter of name or type or line and so on. this method requires perl source code in string. that's it, that's the whole thing β€” hand her a string, get tokens back πŸ”₯

$lexer->set_library_path(['path1', 'path2' ...])

set libraries path for reading recursively. Default paths are @INC. so if your modules are living somewhere off the beaten path, this is your moment 🌈

$lexer->recursive_tokenize($script)

get hash reference like { 'module_nameA' => [], 'module_nameB' => [] ... }. This method requires per source code in string 🧚

$lexer->get_used_modules($script);

get names of used module. This method requires perl source code in string. so like, who's she importing? this tells you. obsessed ✨


πŸ‘‘ AUTHOR πŸ‘‘

Masaaki Goshima (goccy) <goccy(at)cpan.org>


πŸ’œ CONTRIBUTORS πŸ’œ

  • tokuhirom: Tokuhiro Matsuno

Copyright (c) 2013, Masaaki Goshima (goccy). All rights reserved.

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


aaand that's the whole page bestie, you ate it πŸ’– this lil' legend ships as perl v5.42.3, dated 2026-08-19, and it's the Compiler::Lexer(3pm) page β€” go tokenize something gorgeous 🫢

πŸ“¦ where this page comes from