@@ -651,7 +651,7 @@ understand what's happening behind the scenes.
651651
652652(This next paragraph is definitely only for background on debugging.)
653653
654- An C<OP> is is a data structure within the perl interpreter. It is used to
654+ An C<OP> is a data structure within the perl interpreter. It is used to
655655hold the nodes within a tree structure created when the perl source is
656656compiled. It usually represents a single operation within the perl source,
657657such as an add, or a function call. The structure has various flags and
@@ -686,15 +686,15 @@ Consider this subroutine call:
686686The various OPs executed by the Perl interpreter up until the function is
687687called will: push a mark indicating the start of a new argument stack
688688frame; push an SV containing the integer value 1; push the SV currently
689- bound to the variable C<$x>; and push the C<*foo> typeglob. Then the
690- PP function C<pp_entersub()> associated with the C<OP_ENTERSUB> will pop
691- that typeglob, extract the C<&foo> CV from it, and see whether it is a
689+ associated with the variable C<$x>; and push the C<*foo> typeglob. Then
690+ the PP function C<pp_entersub()> associated with the C<OP_ENTERSUB> will
691+ pop that typeglob, extract the C<&foo> CV from it, and see whether it is a
692692normal CV or an XSUB CV.
693693
694694For a normal Perl subroutine call, C<pp_entersub()> will then: pop the
695695topmost mark off the mark stack; pop the SV pointers between that mark and
696696the top of the stack and store them in C<@_>; then set C<PL_op> to the
697- first OP pointed to by the C<&foo> CV. Those OPs will then be run by the
697+ first OP pointed to by the C<&foo> CV. Those OPs will then be run by the
698698main loop, until the OPs associated with the last statement of the
699699function (or an explicit return) will leave any return values as SV
700700pointers on the stack.
@@ -822,7 +822,7 @@ debugging.)
822822
823823As mentioned above, almost all runtime data within the perl interpreter is
824824stored in an SV (scalar value) structure. The head of an SV structure
825- consists of three or four words : a reference count; a type and flags; a
825+ consists of three or four fields : a reference count; a type and flags; a
826826pointer to a body; and since perl 5.10.0, a general payload field. There
827827are around 17 types, and the type indicates what body (if any) is pointed
828828to from the SV's head. The body type only indicates what I<sorts> of data
@@ -943,14 +943,16 @@ as C<SvPVutf8>):
943943 STRLEN len;
944944 char *pv = SvPV(sv, len);
945945
946- which both retrieves a string pointer and sets C<len> to its length. Note
947- that there is no guarantee that after this call C<SvPOK(sv)> is true, nor
948- that C<pv == SvPVX(sv)>. For example, C<sv> may be a reference to a
949- blessed object with an overloaded stringify (C<"">) method. In which
950- case, behind the scenes there may be a temporary SV containing the result
951- of the call to the method, with C<pv> pointing to I<that> SV's string
952- buffer; C<sv> remains a reference. Similarly, a non-overloaded reference
953- to an array may return a temporary string like C<"ARRAY(0x12345678)">.
946+ which both retrieves a string pointer and sets C<len> to its length.
947+ (C<SvPV> is a macro, which is how it can update C<len> without needing an
948+ explicit C<&len>.) Note that there is no guarantee that after this call
949+ C<SvPOK(sv)> is true, nor that C<pv == SvPVX(sv)>. For example, C<sv> may
950+ be a reference to a blessed object with an overloaded stringify (C<"">)
951+ method. In which case, behind the scenes there may be a temporary SV
952+ containing the result of the call to the method, with C<pv> pointing to
953+ I<that> SV's string buffer; C<sv> remains a reference. Similarly, a
954+ non-overloaded reference to an array may return a temporary string like
955+ C<"ARRAY(0x12345678)">.
954956
955957If you need to coerce an SV to a string (e.g. before directly modifying
956958its string buffer) then use C<SvPV_force()> or one of its variants. For
@@ -1357,7 +1359,7 @@ example where the SV pre-exists in an array:
13571359 OUTPUT:
13581360 RETVAL
13591361
1360- Finally, note that some very old (pre-1996) XS documentation suggested
1362+ Finally, note that some very old (pre-1996) XS documentation suggested
13611363that you could return your own SV using code like:
13621364
13631365 void
@@ -1813,7 +1815,7 @@ In general, XSUB prototypes (similarly to perl sub prototypes) are of very
18131815limited use and are typically only used to mimic the behaviour of Perl
18141816builtins. For example there is no way to implement a C<push @a, ...;>
18151817style function without a way of telling the Perl interpreter not to
1816- flatten C<@a>. Outside of these narrow uses, it is generally a mistake to
1818+ flatten C<@a>. Outside of these narrow uses, it is generally a mistake to
18171819use prototypes.
18181820
18191821In the early days of XS it was thought that using prototypes was probably
@@ -1922,7 +1924,7 @@ affect any subsequent XSUBs within the file, until further updates.
19221924
19231925Note however that, due to a quirk in parsing, it is possible for a
19241926C<TYPEMAP:> block which comes I<immediately after> an XSUB to affect any
1925- entries used by that XSUB, as if the block had appeared just before the
1927+ entries used by that XSUB, as if the block had appeared just before the
19261928XSUB. If all such typemap blocks are placed near the start of an XS file,
19271929then this won't be an issue. Indeed, it can only be a possible issue if
19281930you want typemap meanings to change during the course of an XS file (which
@@ -1986,7 +1988,7 @@ XSUB keyword or file-scoped directive.
19861988An XSUB definition consists of a declaration (typically two lines),
19871989followed by an optional body. The declaration specifies the XSUB's name,
19881990parameters and return type. The body consists of sections started by
1989- keywords, which may specify how its parameters and any any return value
1991+ keywords, which may specify how its parameters and any return value
19901992should be processed, and what the main C code body of the XSUB consists
19911993of. Other keywords can change the behaviour of the XSUB, or affect how it
19921994is registered with Perl, e.g. with extra named aliases. In the absence of
@@ -2105,9 +2107,8 @@ comments.
21052107
21062108In fact all it does is extract the text between the C<(...)> and split on
21072109commas, while having enough intelligence to ignore commas and a closing
2108- parenthesis within a double-quoted string. Once each parameter declaration
2109- is extracted, it is processed, as described below in
2110- L</"An XSUB Parameter">.
2110+ parenthesis within a quoted string. Once each parameter declaration is
2111+ extracted, it is processed, as described below in L</"An XSUB Parameter">.
21112112
21122113Each parameter declaration usually generates a C auto variable declaration
21132114of the same name, along with initialisation code which assigns the value
@@ -2233,7 +2234,7 @@ See L</T_PTROBJ and opaque handles> for an example of this using
22332234the common C<T_PTROBJ> typemap type.
22342235
22352236Note that any check on whether the passed Perl object is of the correct
2236- class is down to to the implementation in the particular typemap: for
2237+ class is down to the implementation in the particular typemap: for
22372238example, C<T_PTROBJ> will croak unless the passed SV argument is blessed
22382239into the C<Foo::Bar> or derived class.
22392240
@@ -2565,9 +2566,11 @@ ellipsis as the last parameter, similar to C function declarations. Its
25652566main effect is to disable the error check for too many parameters. Any
25662567declared parameters will still be processed as normal, but the programmer
25672568will have to access any extra arguments manually, making use of the
2568- C<ST(n)> macro to access the nth item on the stack (counting from 0), and
2569- the C<items> variable, which indicates the total number of passed
2570- arguments, including any fixed arguments.
2569+ C<ST(n)> macro to access the nth item on the stack, and the C<items>
2570+ variable, which indicates the total number of passed arguments, including
2571+ any fixed arguments. Note that C<ST(0)> is the first passed argument,
2572+ while the first ellipsis argument is C<ST(i)> where C<i> is the number of
2573+ fixed arguments preceding the ellipsis.
25712574
25722575Note that currently XS doesn't provide any mechanism to autocall
25732576variable-length C functions, so the ellipsis should only be used on XSUBs
@@ -2619,7 +2622,7 @@ example, this XSUB does the equivalent of the Perl C<map { $_*3 } ...>
26192622
26202623Note that the L<PPCODE|/The PPCODE: Keyword> keyword, in comparison to
26212624C<CODE>, resets the local copy of the argument stack pointer, and relies
2622- on the coder to place any return values on the stack. The example above
2625+ on the coder to place any return values on the stack. The example above
26232626reclaims the passed arguments by setting C<SP> back to the top of the
26242627stack, then replaces the items on the stack one by one.
26252628
@@ -3200,7 +3203,10 @@ on the stack; then one by one, each passed argument is retrieved, and then
32003203each stack slot is replaced with a new mortal value. When the loop is
32013204finished, the current stack frame contains a list of mortals, which is
32023205then returned to the caller, with C<SP> indicating how many items are
3203- returned.
3206+ returned. Note that in this example the C<SP += items> could have been
3207+ done at the end instead. However, if the code was doing a mixture of
3208+ updating the passed arguments I<and> pushing extra return values, then
3209+ setting it early (before the first push) would be important.
32043210
32053211Before pushing return values onto the stack (or storing values at C<ST(i)>
32063212locations higher than the number of passed arguments), it is necessary to
@@ -3760,7 +3766,7 @@ similar. Conversely, C<INTERFACE> is intended for use with autocall;
37603766information stored in the CV indicates which C library function should be
37613767autocalled.
37623768
3763- Finally, there is the C<CASE> keyword, which allows the whole body of an
3769+ Finally, there is the C<CASE> keyword, which allows the whole body of an
37643770XSUB (not just the C<CODE> part) to have alternate cases. It can be
37653771thought of as a C<switch()> analogue which works at the top-most XS level
37663772rather than at the C level. The value the C<CASE> acts on could be
@@ -3799,7 +3805,8 @@ same C function, C<XS_Foo__Bar__add()>.
37993805
38003806The alias name can be either a simple function name or can include a
38013807package name. The alias value to the right of the C<=> may be either a
3802- literal positive integer or a word (which is expected to be a CPP define).
3808+ literal positive integer or a word (which is expected to be a CPP define
3809+ or enum constant).
38033810
38043811The rest of the line following the C<ALIAS> keyword, plus any further
38053812lines until the next keyword, are assumed to contain zero or more alias
@@ -4633,11 +4640,12 @@ This class might be used like this:
46334640
46344641=head2 Safely Storing Static Data in XS
46354642
4636- You should generally avoid declaring static variables and data within an
4637- XS file. The Perl interpreter binary is commonly configured to allow
4638- multiple interpreter structures, with a complete set of interpreter state
4639- per interpreter struct. In this case, you usually need your "static" data
4640- to be per-interpreter rather than a single shared per-process value.
4643+ You should generally avoid declaring static variables and similar
4644+ I<mutable> data within an XS file. The Perl interpreter binary is commonly
4645+ configured to allow multiple interpreter structures, with a complete set
4646+ of interpreter state per interpreter struct. In this case, you usually
4647+ need your "static" data to be per-interpreter rather than a single shared
4648+ per-process value.
46414649
46424650This becomes more important in the presence of multiple threads; either
46434651via C<use threads> or where the Perl interpreter is embedded within
0 commit comments