CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/plain;charset=UTF-8
content-encoding: gzip
content-security-policy: default-src 'self'; connect-src 'self' *.google-analytics.com; img-src 'self' data: www.google-analytics.com www.googletagmanager.com; script-src 'self' 'unsafe-inline' www.google-analytics.com www.googletagmanager.com; style-src 'self'; report-uri /csp-reports
accept-ranges: bytes
age: 0
date: Sun, 12 Oct 2025 01:21:20 GMT
via: 1.1 varnish
x-served-by: cache-bom-vanm7210024-BOM
x-cache: MISS
x-cache-hits: 0
x-timer: S1760232080.775909,VS0,VE772
vary: Accept-Encoding
strict-transport-security: max-age=31557600
content-length: 11856
=head1 NAME
perlreapi - Perl regular expression plugin interface
=head1 DESCRIPTION
As of Perl 5.9.5 there is a new interface for plugging and using
regular expression engines other than the default one.
Each engine is supposed to provide access to a constant structure of the
following format:
typedef struct regexp_engine {
REGEXP* (*comp) (pTHX_
const SV * const pattern, const U32 flags);
I32 (*exec) (pTHX_
REGEXP * const rx,
char* stringarg,
char* strend, char* strbeg,
SSize_t minend, SV* sv,
void* data, U32 flags);
char* (*intuit) (pTHX_
REGEXP * const rx, SV *sv,
const char * const strbeg,
char *strpos, char *strend, U32 flags,
struct re_scream_pos_data_s *data);
SV* (*checkstr) (pTHX_ REGEXP * const rx);
void (*free) (pTHX_ REGEXP * const rx);
void (*numbered_buff_FETCH) (pTHX_
REGEXP * const rx,
const I32 paren,
SV * const sv);
void (*numbered_buff_STORE) (pTHX_
REGEXP * const rx,
const I32 paren,
SV const * const value);
I32 (*numbered_buff_LENGTH) (pTHX_
REGEXP * const rx,
const SV * const sv,
const I32 paren);
SV* (*named_buff) (pTHX_
REGEXP * const rx,
SV * const key,
SV * const value,
U32 flags);
SV* (*named_buff_iter) (pTHX_
REGEXP * const rx,
const SV * const lastkey,
const U32 flags);
SV* (*qr_package)(pTHX_ REGEXP * const rx);
#ifdef USE_ITHREADS
void* (*dupe) (pTHX_ REGEXP * const rx, CLONE_PARAMS *param);
#endif
REGEXP* (*op_comp) (...);
=for apidoc_section $regexp
=for apidoc Ay||regexp_engine
When a regexp is compiled, its C field is then set to point at
the appropriate structure, so that when it needs to be used Perl can find
the right routines to do so.
In order to install a new regexp handler, C<$^H{regcomp}> is set
to an integer which (when casted appropriately) resolves to one of these
structures. When compiling, the C method is executed, and the
resulting C structure's engine field is expected to point back at
the same structure.
The pTHX_ symbol in the definition is a macro used by Perl under threading
to provide an extra argument to the routine holding a pointer back to
the interpreter that is executing the regexp. So under threading all
routines get an extra argument.
=head1 Callbacks
=head2 comp
REGEXP* comp(pTHX_ const SV * const pattern, const U32 flags);
Compile the pattern stored in C using the given C and
return a pointer to a prepared C structure that can perform
the match. See L below for an explanation of
the individual fields in the REGEXP struct.
The C parameter is the scalar that was used as the
pattern. Previous versions of Perl would pass two C indicating
the start and end of the stringified pattern; the following snippet can
be used to get the old parameters:
STRLEN plen;
char* exp = SvPV(pattern, plen);
char* xend = exp + plen;
Since any scalar can be passed as a pattern, it's possible to implement
an engine that does something with an array (C<< "ook" =~ [ qw/ eek
hlagh / ] >>) or with the non-stringified form of a compiled regular
expression (C<< "ook" =~ qr/eek/ >>). Perl's own engine will always
stringify everything using the snippet above, but that doesn't mean
other engines have to.
The C parameter is a bitfield which indicates which of the
C flags the regex was compiled with. It also contains
additional info, such as if C routine.
=for apidoc Ayh||struct regexp
=for apidoc Ayh||REGEXP
The REGEXP structure contains all the data that Perl needs to be aware of
to properly work with the regular expression. It includes data about
optimisations that Perl can use to determine if the regex engine should
really be used, and various other control info that is needed to properly
execute patterns in various contexts, such as if the pattern anchored in
some way, or what flags were used during the compile, or if the
program contains special constructs that Perl needs to be aware of.
In addition it contains two fields that are intended for the private
use of the regex engine that compiled the pattern. These are the
C and C members. C is a void pointer to
an arbitrary structure, whose use and management is the responsibility
of the compiling engine. Perl will never modify either of these
values.
/* copied from: regexp.h */
typedef struct regexp {
/*----------------------------------------------------------------------
* Fields required for compatibility with SV types
*/
_XPV_HEAD;
/*----------------------------------------------------------------------
* Operational fields
*/
const struct regexp_engine* engine; /* what engine created this regexp? */
REGEXP *mother_re; /* what re is this a lightweight copy of? */
HV *paren_names; /* Optional hash of paren names */
/*----------------------------------------------------------------------
* Information about the match that the perl core uses to manage things
*/
/* see comment in regcomp_internal.h about branch reset to understand
the distinction between physical and logical capture buffers */
U32 nparens; /* physical number of capture buffers */
U32 logical_nparens; /* logical_number of capture buffers */
I32 *logical_to_parno; /* map logical parno to first physical */
I32 *parno_to_logical; /* map every physical parno to logical */
I32 *parno_to_logical_next; /* map every physical parno to the next
physical with the same logical id */
SSize_t maxlen; /* maximum possible number of chars in string to match */
SSize_t minlen; /* minimum possible number of chars in string to match */
SSize_t minlenret; /* minimum possible number of chars in $& */
STRLEN gofs; /* chars left of pos that we search from */
/* substring data about strings that must appear in
* the final match, used for optimisations */
struct reg_substr_data *substrs;
/* private engine specific data */
void *pprivate; /* Data private to the regex engine which
* created this object. */
U32 extflags; /* Flags used both externally and internally */
U32 intflags; /* Engine Specific Internal flags */
/*----------------------------------------------------------------------
* Data about the last/current match. These are modified during matching
*/
U32 lastparen; /* highest close paren matched ($+) */
U32 lastcloseparen; /* last close paren matched ($^N) */
regexp_paren_pair *offs; /* Array of offsets for (@-) and (@+) */
char **recurse_locinput; /* used to detect infinite recursion, XXX: move to internal */
/*---------------------------------------------------------------------- */
/* offset from wrapped to the start of precomp */
PERL_BITFIELD32 pre_prefix:4;
/* original flags used to compile the pattern, may differ from
* extflags in various ways */
PERL_BITFIELD32 compflags:9;
/*---------------------------------------------------------------------- */
char *subbeg; /* saved or original string so \digit works forever. */
SV_SAVED_COPY /* If non-NULL, SV which is COW from original */
SSize_t sublen; /* Length of string pointed by subbeg */
SSize_t suboffset; /* byte offset of subbeg from logical start of str */
SSize_t subcoffset; /* suboffset equiv, but in chars (for @-/@+) */
/*----------------------------------------------------------------------
* More Operational fields
*/
CV *qr_anoncv; /* the anon sub wrapped round qr/(?{..})/ */
} regexp;
Most of the fields contained in this structure are accessed via macros
with a prefix of C or C. The fields are discussed in more detail
below:
=head2 C
This field points at a C structure which contains pointers
to the subroutines that are to be used for performing a match. It
is the compiling routine's responsibility to populate this field before
returning the regexp object.
Internally this is set to C unless a custom engine is specified in
C<$^H{regcomp}>, Perl's own set of callbacks can be accessed in the struct
pointed to by C.
=for apidoc Amnh||SV_SAVED_COPY
=head2 C
This is a pointer to another struct regexp which this one was derived
from. C objects means that the same regexp pattern can be used in
different contexts at the same time, and as long as match status
information is stored in the structure (there are plans to change this
eventually) we need to support having multiple copies of the structure
in use at the same time. The fields related to the regexp program itself
are copied from the mother_re, and owned by the mother_re, whereas the
match state variables are owned by the struct itself.
=head2 C
This will be used by Perl to see what flags the regexp was compiled
with, this will normally be set to the value of the flags parameter by
the L callback. See the L documentation for
valid flags.
=head2 C C
The minimum string length (in characters) required for the pattern to match.
This is used to
prune the search space by not bothering to match any closer to the end of a
string than would allow a match. For instance there is no point in even
starting the regex engine if the minlen is 10 but the string is only 5
characters long. There is no way that the pattern can match.
C is the minimum length (in characters) of the string that would
be found in $& after a match.
The difference between C and C can be seen in the
following pattern:
/ns(?=\d)/
where the C would be 3 but C would only be 2 as the \d is
required to match but is not actually
included in the matched content. This
distinction is particularly important as the substitution logic uses the
C to tell if it can do in-place substitutions (these can
result in considerable speed-up).
=head2 C
Left offset from pos() to start match at.
=head2 C
Substring data about strings that must appear in the final match. This
is currently only used internally by Perl's engine, but might be
used in the future for all engines for optimisations.
=head2 C, C
These fields are used to keep track of the number of physical and logical
paren capture groups there are in the pattern, which may differ if the
pattern includes the use of the branch reset construct C<(?| ... | ... )>.
For instance the pattern C(?|(foo)|(bar))/> contains two physical capture
buffers, but only one logical capture buffer. Most internals logic in the
regex engine uses the physical capture buffer ids, but the user exposed
logic uses logical capture buffer ids. See the next section for data-structures
that allow mapping from one to the other.
=head2 C, C, C
These fields facilitate mapping between logical and physical capture
buffer numbers. C is an array whose Kth element
contains the lowest physical capture buffer id for the Kth logical
capture buffer. C is an array whose Kth element
contains the logical capture buffer associated with the Kth physical
capture buffer. C is an array whose Kth element
contains the next physical capture buffer with the same logical id, or 0
if there is none.
Note that all three of these arrays are ONLY populated when the pattern
includes the use of the branch reset concept. Patterns which do not use
branch-reset effectively have a 1:1 to mapping between logical and
physical so there is no need for this meta-data.
The following table gives an example of how this works.
Pattern /(a) (?| (b) (c) (d) | (e) (f) | (g) ) (h)/
Logical: $1 $2 $3 $4 $2 $3 $2 $5
Physical: 1 2 3 4 5 6 7 8
Next: 0 5 6 0 7 0 0 0
Also note that the 0th element of any of these arrays is not used as it
represents the "entire pattern".
=head2 C, and C
These fields are used to keep track of: which was the highest paren to
be closed (see L); and which was the most recent paren to be
closed (see L).
=head2 C
The engine's private copy of the flags the pattern was compiled with. Usually
this is the same as C unless the engine chose to modify one of them.
=head2 C
A void* pointing to an engine-defined
data structure. The Perl engine uses the
C structure (see L) but a custom
engine should use something else.
=head2 C
A C structure which defines offsets into the string being
matched which correspond to the C<$&> and C<$1>, C<$2> etc. captures, the
C struct is defined as follows:
typedef struct regexp_paren_pair {
I32 start;
I32 end;
} regexp_paren_pair;
=for apidoc Ayh||regexp_paren_pair
If C<< ->offs[num].start >> or C<< ->offs[num].end >> is C<-1> then that
capture group did not match.
C<< ->offs[0].start/end >> represents C<$&> (or
C<${^MATCH}> under C) and C<< ->offs[paren].end >> matches C<$$paren> where
C<$paren >= 1>.
=head2 C C
Used for optimisations. C holds a copy of the pattern that
was compiled and C its length. When a new pattern is to be
compiled (such as inside a loop) the internal C operator
checks if the last compiled C's C and C
are equivalent to the new one, and if so uses the old pattern instead
of compiling a new one.
In older perls these two macros were actually fields in the structure
with the names C and C respectively.
=head2 C
This is a hash used internally to track named capture groups and their
offsets. The keys are the names of the buffers the values are dualvars,
with the IV slot holding the number of buffers with the given name and the
pv being an embedded array of I32. The values may also be contained
independently in the data array in cases where named backreferences are
used.
=head2 C
Holds information on the longest string that must occur at a fixed
offset from the start of the pattern, and the longest string that must
occur at a floating offset from the start of the pattern. Used to do
Fast-Boyer-Moore searches on the string to find out if its worth using
the regex engine at all, and if so where in the string to search.
=head2 C C C C C
Used during the execution phase for managing search and replace patterns,
and for providing the text for C<$&>, C<$1> etc. C points to a
buffer (either the original string, or a copy in the case of
C), and C is the length of the buffer. The
C and C macros index into this
buffer. as does the data structure returned by C but you
should not use that directly.
=for apidoc Amh||RX_MATCH_COPIED|const REGEXP * rx_sv
In the presence of the C flag, but with the addition of
the C or C flags, an engine
can choose not to copy the full buffer (although it must still do so in
the presence of C or the relevant bits being set in
C). In this case, it may set C to indicate the
number of bytes from the logical start of the buffer to the physical start
(i.e. C). It should also set C, the number of
characters in the offset. The latter is needed to support C<@-> and C<@+>
which work in characters, not bytes.
=for apidoc Amnh ||REXEC_COPY_SKIP_POST
=for apidoc_item ||REXEC_COPY_SKIP_PRE
=for apidoc_item ||REXEC_COPY_STR
=head2 C C
Macros which access the string the C stringifies to. The Perl
engine for example stores C<(?^:eek)> in the case of C .
When using a custom engine that doesn't support the C<(?:)> construct
for inline modifiers, it's probably best to have C stringify to
the supplied pattern, note that this will create undesired patterns in
cases such as:
my $x = qr/a|b/; # "a|b"
my $y = qr/c/i; # "c"
my $z = qr/$x$y/; # "a|bc"
There's no solution for this problem other than making the custom
engine understand a construct like C<(?:)>.
=head2 C
The number of times the structure is referenced. When this falls to 0,
the regexp is automatically freed by a call to C. This should
be set to 1 in each engine's L routine. Note that in older perls
this was a member in the struct called C but in more modern
perls where the regexp structure was unified with the SV structure this
is an alias to SvREFCNT().
=head1 HISTORY
Originally part of L.
=head1 AUTHORS
Originally written by Yves Orton, expanded by Evar ArnfjErE
Bjarmason.
=head1 LICENSE
Copyright 2006 Yves Orton and 2007 Evar ArnfjErE Bjarmason.
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut