PPIx::R...okenizer(3)
๐ PPIx::Regexp::Tokenizer โ the manual page, but make it cute ๐
okay so bestie, gather round ๐ธ this is the manual for PPIx::Regexp::Tokenizer, which lives in the Perl user-contributed docs (that's the PPIx::R...okenizer(3) situation up top, iykyk). It's the thing that tokenizes a regular expression โ like, it takes your regex and chops it into lil pieces so the rest of the machinery can vibe with it. ๐ฆ
โจ NAME โจ
PPIx::Regexp::Tokenizer โ literally just: Tokenize a regular expression. That's the whole job. That's the assignment. ๐
โจ SYNOPSIS โจ
The way you'd actually touch this thing:
use PPIx::Regexp::Dumper;
PPIx::Regexp::Dumper->new( 'qr{foo}smx' )
->print();
short, sweet, iconic โจ
โจ INHERITANCE โจ
Okay so the family tree situation:
PPIx::Regexp::Tokenizeris aPPIx::Regexp::Support. ๐ซถ That's its parent.PPIx::Regexp::Tokenizerhas no descendants. It's the end of the line, main character with no sequels. ๐ญ
โจ DESCRIPTION โจ
This class provides the tokenization of the regular expression. That's it, that's the whole glow-up. ๐
โจ METHODS โจ
This class provides the following public methods. ๐
Real talk though: any method that isn't documented here (or down below under "EXTERNAL TOKENIZERS") is private. And "private" means unsupported in the sense that the author reserves the right to change or remove them without notice. So likeโฆ don't build your whole personality on them, bestie. ๐ฉ
๐ new
my $tokenizer = PPIx::Regexp::Tokenizer->new( 'xyzzy' );
This is a static method and it instantiates the tokenizer. โจ
You must hand it the regular expression to be parsed โ either as a string or as a PPI::Element of some sort. You can also pass optional name/value pairs of arguments. Heads up: the option names are specified without a leading dash. ๐
Here are the supported options, one by one:
default_modifiersarray_reference โ this argument specifies default statement modifiers. It's optional, but if you specify it, it must be an array reference. For the deets, go look at thePPIx::Regexpnew()documentation. ๐ฆencodingname โ this option specifies the encoding of the string to be tokenized. If you set it, anEncode::decodeis done on the string (or on thecontentof the PPI class) before it gets tokenized. ๐ธindex_locationsโ this Boolean option says that the locations of the generated tokens are to be computed. ๐strictboolean โ this option specifies whether tokenization should assumeuse re 'strict';is in effect.
โ ๏ธ The 'strict' pragma was introduced in Perl 5.22, and its documentation says it is experimental, and that there is no commitment to backward compatibility. The same energy applies to the tokenization you get when this option is asserted. So like, it might change on you. ๐
tracenumber โ specifying a positive value for this option causes a trace of the tokenization. ๐ต๏ธ This option is unsupported in the sense that the author reserves the right to alter it without notice.
If this option is unspecified, the value comes from the environment variable PPIX_REGEXP_TOKENIZER_TRACE (scroll to "ENVIRONMENT VARIABLES" for that whole situation). And if that environment variable doesn't exist either? The default is 0. ๐ซถ
๐จ Undocumented options are unsupported. No cap.
The returned value is the instantiated tokenizerโฆ or undef if instantiation failed. ๐ญ And if it did fail, a call to errstr will hand you the reason why.
๐ content
print $tokenizer->content();
This method returns the string being tokenized. If the object was instantiated with a PPI::Element, this will be whatever the PPI::Element->content() method gives you. โจ
๐ default_modifiers
print join ', ', @{ $tokenizer->default_modifiers() };
Returns a reference to a copy of the array that you passed to the default_modifiers argument of new(). ๐ And if you never used that argument to instantiate the object? You get a reference to an empty array. Cool cool cool.
๐ encoding
This method returns the encoding of the data being parsed, if one was set when the class was instantiated. Otherwise it just returns undef. That's it. ๐ฆ
๐ errstr
my $tokenizer = PPIx::Regexp::Tokenizer->new( 'xyzzy' )
or die PPIx::Regexp::Tokenizer->errstr();
A static method that returns an error description if tokenizer instantiation failed. Basically the "what went wrong" receipt. ๐งพ
๐ failures
print $tokenizer->failures(), " tokenization failures\n";
This returns the number of tokenization failures encountered. And how does a tokenization failure show up in the output token stream? As a PPIx::Regexp::Token::Unknown. ๐
๐ modifier
$tokenizer->modifier( 'x' )
and print "Tokenizing an extended regular expression\n";
This method returns true if the given modifier character was found on the end of the regular expression, and false otherwise. โ โ
Now, the plot twist ๐ โ starting with version 0.036_01: if the argument is a single-character modifier followed by an asterisk (the asterisk is intended as a wild card character), the return is the number of times that modifier appears. But heads up, an exception will be thrown if you specify a multi-character modifier (like 'ee*'), or if you specify one of the match semantics modifiers (like 'a*'). ๐ฉ
And one more thing: if it's called by an external tokenizer, this method returns true if the given modifier was true at the current point in the tokenization. ๐
๐ next_token
my $token = $tokenizer->next_token();
Returns the next token in the token streamโฆ or nothing if there are no more tokens. Simple as. โจ
๐ significant
This method exists simply for the convenience of PPIx::Regexp::Dumper. It always returns true. That's the whole method. It's just there being supportive. ๐ซถ
๐ tokens
my @tokens = $tokenizer->tokens();
Returns all remaining tokens in the token stream. ๐
okay that's part one bestie, keep scrolling, we're not done yet ๐
๐ EXTERNAL TOKENIZERS ๐
Okay so bestie, plot twist: this class does very little of its own tokenization. Instead, the token classes contain external tokenization routines, whose name is '__PPIX_TOKENIZER__' concatenated with the current mode of the tokenizer ('regexp' for regular expressions, 'repl' for the replacement string).
These external tokenizers are called as static methods, and passed the PPIx::Regexp::Tokenizer object and the current character in the character stream.
If the external tokenizer wants to make one or more tokens, it returns an array containing either length in characters for tokens of the tokenizer's own class, or the results of one or more make_token calls for tokens of an arbitrary class.
If the external tokenizer is not interested in the characters starting at the current position it simply returns.
The following methods are for the use of external tokenizers, and are not part of the public interface to this class.
๐ capture ๐
if ( $tokenizer->find_regexp( qr{ \A ( foo ) }smx ) ) {
foreach ( $tokenizer->capture() ) {
print "$_\n";
}
}
This method returns all the contents of any capture buffers from the previous call to find_regexp. The first element of the array (i.e. element 0) corresponds to $1, and so on.
The captures are cleared by make_token, as well as by another call to find_regexp.
๐ช cookie ๐ช
$tokenizer->cookie( foo => sub { 1 } );
my $cookie = $tokenizer->cookie( 'foo' );
my $old_hint = $tokenizer->cookie( foo => undef );
This method either creates, deletes, or accesses a cookie.
A cookie is a code reference which is called whenever the tokenizer makes a token. If it returns a false value, it is deleted. Explicitly setting the cookie to undef also deletes it.
When you call $tokenizer->cookie( 'foo' ), the current cookie is returned. If you pass a new value of undef to delete the token, the deleted cookie (if any) is returned.
When the make_token method calls a cookie, it passes it the tokenizer and the token just made. If a token calls a cookie, it is recommended that it merely pass the tokenizer, though of course the token can do whatever it wants.
The cookie mechanism seems to be a bit of a crock, but it appeared to be more work to fix things up in the lexer after the tokenizer got something wrong.
The recommended way to write a cookie is to use a closure to store any necessary data, and have a call to the cookie return the data; otherwise the ultimate consumer of the cookie has no way to access the data. Of course, it may be that the presence of the cookie at a certain point in the parse is all that is required.
๐ expect ๐
$tokenizer->expect( 'PPIx::Regexp::Token::Code' );
This method inserts a given class at the head of the token scan, for the next iteration only. More than one class can be specified. Class names can be abbreviated by removing the leading 'PPIx::Regexp::'.
If no class is specified, this method does nothing.
The expectation lasts from the next time get_token is called until the next time make_token makes a significant token, or until the next expect call if that is done sooner.
๐ find_regexp ๐
my $end = $tokenizer->find_regexp( qr{ \A \w+ }smx );
my ( $begin, $end ) = $tokenizer->find_regexp(
qr{ \A \w+ }smx );
This method finds the given regular expression in the content, starting at the current position. If called in scalar context, the offset from the current position to the end of the matched string is returned. If called in list context, the offsets to both the beginning and the end of the matched string are returned.
๐งฉ find_matching_delimiter ๐งฉ
my $offset = $tokenizer->find_matching_delimiter();
This method is used by tokenizers to find the delimiter matching the character at the current position in the content string. If the delimiter is an opening bracket of some sort, bracket nesting will be taken into account.
When searching for the matching delimiter, the back slash character is considered to escape the following character, so back-slashed delimiters will be ignored. No other quoting mechanisms are recognized, though, so delimiters inside quotes still count. This is actually the way Perl works, as
$ perl -e 'qr<(?{ print "}" })>'
demonstrates.
This method returns the offset from the current position in the content string to the matching delimiter (which will always be positive), or undef if no match can be found.
๐ get_mode ๐
This method returns the name of the current mode of the tokenizer.
๐ฌ get_start_delimiter ๐ฌ
my $start_delimiter = $tokenizer->get_start_delimiter();
This method is used by tokenizers to access the start delimiter for the regular expression.
๐๏ธ get_token ๐๏ธ
my $token = $tokenizer->make_token( 3 );
my @tokens = $tokenizer->get_token();
This method returns the next token that can be made from the input stream. It is not part of the external interface, but is intended for the use of an external tokenizer which calls it after making and retaining its own token to look at the next token ( if any ) in the input stream.
๐จ Fatal error alert: If any external tokenizer calls get_token without first calling make_token, a fatal error occurs; this is better than the infinite recursion which would occur if the condition were not trapped.
An external tokenizer must return anything returned by get_token; otherwise tokens get lost.
๐ซง interpolates ๐ซง
This method returns true if the top-level structure being tokenized interpolates; that is, if the delimiter is not a single quote.
๐ญ make_token ๐ญ
return $tokenizer->make_token( 3, 'PPIx::Regexp::Token::Unknown' );
This method is used by this class (and possibly by individual tokenizers) to manufacture a token. Its arguments are the number of characters to include in the token, and optionally the class of the token. If no class name is given, the caller's class is used. Class names may be shortened by removing the initial 'PPIx::Regexp::', which will be restored by this method.
The token will be manufactured from the given number of characters starting at the current cursor position, which will be adjusted.
If the given length would include characters past the end of the string being tokenized, the length is reduced appropriately. If this means a token with no characters, nothing is returned.
๐ช match ๐ช
if ( $tokenizer->find_regexp( qr{ \A \w+ }smx ) ) {
print $tokenizer->match(), "\n";
}
This method returns the string matched by the previous call to find_regexp.
The match is set to undef by make_token, as well as by another call to find_regexp.
๐งฌ modifier_duplicate ๐งฌ
$tokenizer->modifier_duplicate();
This method duplicates the modifiers on the top of the modifier stack, with the intent of creating a locally-scoped copy of the modifiers. This should only be called by an external tokenizer that is actually creating a modifier scope. In other words, only when creating a PPIx::Regexp::Token::Structure token whose content is '('.
๐๏ธ modifier_modify ๐๏ธ
$tokenizer->modifier_modify( name => $value ... );
This method sets new values for the modifiers in the local scope. Only the modifiers whose names are actually passed have their values changed.
This method is intended to be called after manufacturing a PPIx::Regexp::Token::Modifier token, and passed the results of its modifiers method.
๐ซณ modifier_pop ๐ซณ
$tokenizer->modifier_pop();
This method removes the modifiers on the top of the modifier stack. This should only be called by an external tokenizer that is ending a modifier scope. In other words, only when creating a PPIx::Regexp::Token::Structure token whose content is ')'.
Note that this method will never pop the last modifier item off the stack, to guard against unmatched right parentheses.
๐ modifier_seen ๐
$tokenizer->modifier_seen( 'i' )
and print "/i was seen at some point.\n";
Unlike modifier(), this method returns a true value if the given modifier has been seen in any scope visible from the current location in the parse. There is no magic for group match semantics ( /a, /aa, /d, /l, /u) or modifiers that can be repeated, like /x and /xx, or /e and /ee.
๐๏ธ peek ๐๏ธ
my $character = $tokenizer->peek();
my $next_char = $tokenizer->peek( 1 );
This method returns the character at the given non-negative offset from the current position. If no offset is given, an offset of 0 is used.
If you ask for a negative offset or an offset off the end of the sting, undef is returned.
๐ ppi_document ๐
This method makes a PPI document out of the remainder of the string, and returns it.
โฎ๏ธ prior_significant_token โฎ๏ธ
$tokenizer->prior_significant_token( 'can_be_quantified' )
and print "The prior token can be quantified.\n";
This method calls the named method on the most-recently-instantiated significant token, and returns the result. Any arguments subsequent to the method name will be passed to the method.
Because this method is designed to be used within the tokenizing system, it will die horribly if the named method does not exist.
If called with no arguments at all the most-recently-instantiated significant token is returned.
๐ strict ๐
say 'Parse is ', $tokenizer->strict() ? 'strict' : 'lenient';
This method simply returns true or false, depending on whether the 'strict' option to new() was true or false.
๐ ENVIRONMENT VARIABLES ๐
A tokenizer trace can be requested by setting environment variable PPIX_REGEXP_TOKENIZER_TRACE to a numeric value other than 0. Use of this environment variable is unsupported in the same sense that the "trace" option of "new" is unsupported. Explicitly specifying the "trace" option to "new" overrides the environment variable.
The real reason this is documented is to give the user a way to troubleshoot funny output from the tokenizer.
๐ซถ SUPPORT ๐ซถ
Support is by the author. Please file bug reports at https://github.com/trwyant/perl-PPIx-Regexp/issues or in electronic mail to the author.
๐ AUTHOR ๐
Thomas R. Wyant, III harryfmudd at comcast dot net
๐ COPYRIGHT AND LICENSE ๐
Copyright (C) 2009-2023, 2025-2026 by Thomas R. Wyant, III
This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
And that's the whole vibe, bestie! ๐โจ This page is from perl v5.42.2, dated 2026-06-15, and it's the PPIx::R...okenizer(3) manual. Periodt. ๐ ๐ธ
๐ฆ where this page comes from
- fedora 43: perl-PPIx-Regexp 0.092-1.fc43 ยท GPL-1.0-or-later OR Artistic-1.0-Perl
- fedora 44: perl-PPIx-Regexp 0.092-1.fc44 ยท GPL-1.0-or-later OR Artistic-1.0-Perl
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms