X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlreapi.pod;h=03996fd51edc9d863548ad968a638f992f921828;hb=6d1e6673d7386f4f9139111a6e44d555b8252741;hp=1a170ffe311da5e55289b475fd3b4d8713bc68a0;hpb=882227b7f0b6e1ca62725268e60a7fd0211899ca;p=p5sagit%2Fp5-mst-13.2.git
diff --git a/pod/perlreapi.pod b/pod/perlreapi.pod
index 1a170ff..03996fd 100644
--- a/pod/perlreapi.pod
+++ b/pod/perlreapi.pod
@@ -4,9 +4,11 @@ perlreapi - perl regular expression plugin interface
=head1 DESCRIPTION
-As of Perl 5.9.5 there is a new interface for using other regexp engines than
-the default one. Each engine is supposed to provide access to a constant
-structure of the following format:
+As of Perl 5.9.5 there is a new interface for plugging and using other
+regular expression engines 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);
@@ -24,15 +26,17 @@ structure of the following format:
SV const * const value);
I32 (*numbered_buff_LENGTH) (pTHX_ REGEXP * const rx, const SV * const sv,
const I32 paren);
- SV* (*named_buff_FETCH) (pTHX_ REGEXP * const rx, SV * const sv,
- const U32 flags);
+ 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
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 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
@@ -59,7 +63,7 @@ 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 stringifed pattern, the following snippet can
+the start and end of the stringified pattern, the following snippet can
be used to get the old parameters:
STRLEN plen;
@@ -73,81 +77,130 @@ 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 paramater is a bitfield which indicates which of the
-C flags the regex was compiled with. In addition it contains
-info about whether C
- RXf_PMf_KEEPCOPY
+
+=back
+
+Additional flags:
+
+=over 4
=item RXf_PMf_LOCALE
-Makes C use the locale dependant definition of whitespace under C when RXf_SKIPWHITE or RXf_WHITE is in effect. Under ASCII whitespace is
-defined as per L, and by the internal macros
-C under UTF-8 and C under C.
+Set if C is in effect. If present in C<< rx->extflags >>
+C will use the locale dependent definition of whitespace under
+when RXf_SKIPWHITE or RXf_WHITE are in effect. Under ASCII whitespace
+is defined as per L, and by the internal
+macros C under UTF-8 and C under C.
-=item RXf_PMf_MULTILINE
+=item RXf_UTF8
-The C flag, this ends up being passed to C by
-C regardless of the engine.
+Set if the pattern is L, set by Perl_pmruntime.
-=item RXf_PMf_SINGLELINE
+A regex engine may want to set or disable this flag during
+compilation. The perl engine for instance may upgrade non-UTF-8
+strings to UTF-8 if the pattern includes constructs such as C<\x{...}>
+that can only match Unicode values.
-The C flag. Guaranteed not to be used outside the regex engine.
+=item RXf_SPLIT
-=item RXf_PMf_FOLD
+If C is invoked as C or with no arguments (which
+really means C, see L), perl will
+set this flag. The regex engine can then check for it and set the
+SKIPWHITE and WHITE extflags. To do this the perl engine does:
-The C flag. Guaranteed not to be used outside the regex engine.
+ if (flags & RXf_SPLIT && r->prelen == 1 && r->precomp[0] == ' ')
+ r->extflags |= (RXf_SKIPWHITE|RXf_WHITE);
-=item RXf_PMf_EXTENDED
+=back
-The C flag. Guaranteed not to be used outside the regex
-engine. However if present on a regex C<#> comments will be stripped
-by the tokenizer regardless of the engine currently in use.
+These flags can be set during compilation to enable optimizations in
+the C operator.
-=item RXf_PMf_KEEPCOPY
+=over 4
-The C flag.
+=item RXf_SKIPWHITE
-=item RXf_UTF8
+If the flag is present in C<< rx->extflags >> C will delete
+whitespace from the start of the subject string before it's operated
+on. What is considered whitespace depends on whether the subject is a
+UTF-8 string and whether the C flag is set.
-Set if the pattern is L, set by Perl_pmruntime.
+If RXf_WHITE is set in addition to this flag C will behave like
+C under the perl engine.
-=back
+=item RXf_START_ONLY
+
+Tells the split operator to split the target string on newlines
+(C<\n>) without invoking the regex engine.
+
+Perl's engine sets this if the pattern is C^/> (C), even under C^/s>, see L. Of course a
+different regex engine might want to use the same optimizations
+with a different syntax.
-In general these flags should be preserved in C<< rx->extflags >>
-after compilation, although it is possible the regex includes
-constructs that changes them. The perl engine for instance may upgrade
-non-utf8 strings to utf8 if the pattern includes constructs such as
-C<\x{...}> that can only match unicode values. RXf_SKIPWHITE should
-always be preserved verbatim in C<< regex->extflags >>.
+=item RXf_WHITE
+
+Tells the split operator to split the target string on whitespace
+without invoking the regex engine. The definition of whitespace varies
+depending on whether the target string is a UTF-8 string and on
+whether RXf_PMf_LOCALE is set.
+
+Perl's engine sets this flag if the pattern is C<\s+>.
+
+=item RXf_NULL
+
+Tells the split operator to split the target string on
+characters. The definition of character varies depending on whether
+the target string is a UTF-8 string.
+
+Perl's engine sets this flag on empty patterns, this optimization
+makes C much faster than it would otherwise be. It's even
+faster than C.
+
+=back
=head2 exec
@@ -186,38 +239,45 @@ can release any resources pointed to by the C member of the
regexp structure. This is only responsible for freeing private data;
perl will handle releasing anything else contained in the regexp structure.
-=head2 numbered_buff_FETCH
+=head2 Numbered capture callbacks
- void numbered_buff_FETCH(pTHX_ REGEXP * const rx, const I32 paren,
- SV * const sv);
+Called to get/set the value of C<$`>, C<$'>, C<$&> and their named
+equivalents, ${^PREMATCH}, ${^POSTMATCH} and $^{MATCH}, as well as the
+numbered capture buffers (C<$1>, C<$2>, ...).
-Called to get the value of C<$`>, C<$'>, C<$&> (and their named
-equivalents, see L) and the numbered capture buffers (C<$1>,
-C<$2>, ...).
-
-The C paramater will be C<-2> for C<$`>, C<-1> for C<$'>, C<0>
+The C parameter will be C<-2> for C<$`>, C<-1> for C<$'>, C<0>
for C<$&>, C<1> for C<$1> and so forth.
-C should be set to the scalar to return, the scalar is passed as
-an argument rather than being returned from the function because when
-it's called perl already has a scalar to store the value, creating
-another one would be redundant. The scalar can be set with
-C, C and friends, see L.
+The names have been chosen by analogy with L methods
+names with an additional B callback for efficiency. However
+named capture variables are currently not tied internally but
+implemented via magic.
+
+=head3 numbered_buff_FETCH
+
+ void numbered_buff_FETCH(pTHX_ REGEXP * const rx, const I32 paren,
+ SV * const sv);
+
+Fetch a specified numbered capture. C should be set to the scalar
+to return, the scalar is passed as an argument rather than being
+returned from the function because when it's called perl already has a
+scalar to store the value, creating another one would be
+redundant. The scalar can be set with C, C and
+friends, see L.
This callback is where perl untaints its own capture variables under
-taint mode (see L). See the C
+taint mode (see L). See the C
function in F for how to untaint capture variables if
that's something you'd like your engine to do as well.
-=head2 numbered_buff_STORE
+=head3 numbered_buff_STORE
void (*numbered_buff_STORE) (pTHX_ REGEXP * const rx, const I32 paren,
SV const * const value);
-Called to set the value of a numbered capture variable. C is
-the paren number (see the L above) and
-C is the scalar that is to be used as the new value. It's up to
-the engine to make sure this is used as the new value (or reject it).
+Set the value of a numbered capture variable. C is the scalar
+that is to be used as the new value. It's up to the engine to make
+sure this is used as the new value (or reject it).
Example:
@@ -227,7 +287,7 @@ Example:
}
Perl's own engine will croak on any attempt to modify the capture
-variables, to do this in another engine use the following callack
+variables, to do this in another engine use the following callback
(copied from C):
void
@@ -242,7 +302,7 @@ variables, to do this in another engine use the following callack
Perl_croak(aTHX_ PL_no_modify);
}
-Actually perl 5.10 will not I croak in a statement that looks
+Actually perl will not I croak in a statement that looks
like it would modify a numbered capture variable. This is because the
STORE callback will not be called if perl can determine that it
doesn't have to modify the value. This is exactly how tied variables
@@ -262,19 +322,19 @@ behave in the same situation:
Because C<$sv> is C when the C operator is applied to it
the transliteration won't actually execute and the program won't
-C. This is different to how 5.8 behaved since the capture
-variables were READONLY variables then, now they'll just die on
-assignment in the default engine.
+C. This is different to how 5.8 and earlier versions behaved
+since the capture variables were READONLY variables then, now they'll
+just die when assigned to in the default engine.
-=head2 numbered_buff_LENGTH
+=head3 numbered_buff_LENGTH
I32 numbered_buff_LENGTH (pTHX_ REGEXP * const rx, const SV * const sv,
const I32 paren);
Get the C of a capture variable. There's a special callback
for this so that perl doesn't have to do a FETCH and run C on
-the result, since the length is (in perl's case) known from a memory
-offset this is much more efficient:
+the result, since the length is (in perl's case) known from an offset
+stored in C<< rx->offs >> this is much more efficient:
I32 s1 = rx->offs[paren].start;
I32 s2 = rx->offs[paren].end;
@@ -284,14 +344,61 @@ This is a little bit more complex in the case of UTF-8, see what
C does with
L.
-=head2 named_buff_FETCH
+=head2 Named capture callbacks
+
+Called to get/set the value of C<%+> and C<%-> as well as by some
+utility functions in L.
+
+There are two callbacks, C is called in all the cases the
+FETCH, STORE, DELETE, CLEAR, EXISTS and SCALAR L callbacks
+would be on changes to C<%+> and C<%-> and C in the
+same cases as FIRSTKEY and NEXTKEY.
+
+The C parameter can be used to determine which of these
+operations the callbacks should respond to, the following flags are
+currently defined:
+
+Which L operation is being performed from the Perl level on
+C<%+> or C<%+>, if any:
+
+ RXapif_FETCH
+ RXapif_STORE
+ RXapif_DELETE
+ RXapif_CLEAR
+ RXapif_EXISTS
+ RXapif_SCALAR
+ RXapif_FIRSTKEY
+ RXapif_NEXTKEY
+
+Whether C<%+> or C<%-> is being operated on, if any.
+
+ RXapif_ONE /* %+ */
+ RXapif_ALL /* %- */
+
+Whether this is being called as C, C or
+C, if any. The first two will be combined with
+C or C.
- SV* named_buff_FETCH(pTHX_ REGEXP * const rx, SV * const key,
- const U32 flags);
+ RXapif_REGNAME
+ RXapif_REGNAMES
+ RXapif_REGNAMES_COUNT
-Called to get the value of key in the C<%+> and C<%-> hashes, C
-is the hash key being requested and if C is true C<%-> is
-being requested (and C<%+> if it's not).
+Internally C<%+> and C<%-> are implemented with a real tied interface
+via L. The methods in that package will call
+back into these functions. However the usage of
+L for this purpose might change in future
+releases. For instance this might be implemented by magic instead
+(would need an extension to mgvtbl).
+
+=head3 named_buff
+
+ SV* (*named_buff) (pTHX_ REGEXP * const rx, SV * const key,
+ SV * const value, U32 flags);
+
+=head3 named_buff_iter
+
+ SV* (*named_buff_iter) (pTHX_ REGEXP * const rx, const SV * const lastkey,
+ const U32 flags);
=head2 qr_package
@@ -302,10 +409,14 @@ qr//>). It is recommended that engines change this to their package
name for identification regardless of whether they implement methods
on the object.
-A callback implementation might be:
+The package this method returns should also have the internal
+C package in its C<@ISA>. Cisa("Regexp")> should always
+be true regardless of what engine is being used.
+
+Example implementation might be:
SV*
- Example_reg_qr_package(pTHX_ REGEXP * const rx)
+ Example_qr_package(pTHX_ REGEXP * const rx)
{
PERL_UNUSED_ARG(rx);
return newSVpvs("re::engine::Example");
@@ -318,40 +429,24 @@ package as a normal object.
my $re = qr//;
$re->meth; # dispatched to re::engine::Example::meth()
-To retrieve the C object from the scalar in an XS function use the
-following snippet:
-
- void meth(SV * rv)
- PPCODE:
- MAGIC * mg;
- REGEXP * re;
-
- if (SvMAGICAL(sv))
- mg_get(sv);
- if (SvROK(sv) &&
- (sv = (SV*)SvRV(sv)) && /* assignment deliberate */
- SvTYPE(sv) == SVt_PVMG &&
- (mg = mg_find(sv, PERL_MAGIC_qr))) /* assignment deliberate */
- {
- re = (REGEXP *)mg->mg_obj;
- }
-
-Or use the (CURRENTLY UNDOCUMENETED!) C function:
+To retrieve the C object from the scalar in an XS function use
+the C macro, see L<"REGEXP Functions" in perlapi|perlapi/REGEXP
+Functions>.
void meth(SV * rv)
PPCODE:
- const REGEXP * const re = (REGEXP *)Perl_get_re_arg( aTHX_ rv, 0, NULL );
+ REGEXP * re = SvRX(sv);
=head2 dupe
void* dupe(pTHX_ REGEXP * const rx, CLONE_PARAMS *param);
On threaded builds a regexp may need to be duplicated so that the pattern
-can be used by mutiple threads. This routine is expected to handle the
+can be used by multiple threads. This routine is expected to handle the
duplication of any private data pointed to by the C member of
the regexp structure. It will be called with the preconstructed new
regexp structure as an argument, the C member will point at
-the B private structue, and it is this routine's responsibility to
+the B private structure, and it is this routine's responsibility to
construct a copy and return a pointer to it (which perl will then use to
overwrite the field as passed to this routine.)
@@ -418,7 +513,6 @@ values.
I32 prelen; /* length of precomp */
const char *precomp; /* pre-compilation regular expression */
- /* wrapped can't be const char*, as it is returned by sv_2pv_flags */
char *wrapped; /* wrapped version of the pattern */
I32 wraplen; /* length of wrapped */
@@ -448,8 +542,10 @@ TODO, see L
=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 on L.
+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
@@ -470,7 +566,7 @@ following pattern:
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 whether it can do in-place substition which can result in
+C to tell whether it can do in-place substitution which can result in
considerable speedup.
=head2 C
@@ -479,7 +575,9 @@ Left offset from pos() to start match at.
=head2 C
-TODO: document
+Substring data about strings that must appear in the final match. This
+is currently only used internally by perl's engine for but might be
+used in the future for all engines for optimisations.
=head2 C, C, and C
@@ -490,7 +588,7 @@ the last close paren to be entered.
=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
+this is the same as C unless the engine chose to modify one of them.
=head2 C
@@ -500,7 +598,7 @@ engine should use something else.
=head2 C
-TODO: document
+Unused. Left in for compatibility with perl 5.10.0.
=head2 C
@@ -520,8 +618,18 @@ C<$paren >= 1>.
=head2 C C
-Used for debugging purposes. C holds a copy of the pattern
-that was compiled and C its length.
+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 whether 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.
+
+The relevant snippet from C:
+
+ if (!re || !re->precomp || re->prelen != (I32)len ||
+ memNE(re->precomp, t, len))
+ /* Compile a new pattern */
=head2 C
@@ -532,7 +640,7 @@ 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
+=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
@@ -542,34 +650,24 @@ the regex engine at all, and if so where in the string to search.
=head2 C C C
- #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL)
- if (RX_MATCH_COPIED(ret))
- ret->subbeg = SAVEPVN(ret->subbeg, ret->sublen);
- else
- ret->subbeg = NULL;
-
-Cextflags & RXf_PMf_KEEPCOPY>
-
-These are used during execution phase for managing search and replace
-patterns.
+Used during execution phase for managing search and replace patterns.
=head2 C C
-Stores the string C stringifies to, for example C<(?-xism:eek)>
-in the case of C.
+Stores the string C stringifies to. The perl engine for example
+stores C<(?-xism:eek)> in the case of C.
-When using a custom engine that doesn't support the C<(?:)> construct for
-inline modifiers it's best to have C stringify to the supplied pattern,
-note that this will create invalid patterns in cases such as:
+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/; # "c"
+ my $y = qr/c/i; # "c"
my $z = qr/$x$y/; # "a|bc"
-There's no solution for such problems other than making the custom engine
-understand some for of inline modifiers.
-
-The C in F does the stringification work.
+There's no solution for this problem other than making the custom
+engine understand a construct like C<(?:)>.
=head2 C