Skip to content

Commit 45673ef

Browse files
committed
perlxs: more tweaks following code review
The many commits in this branch have completely rewritten perlxs.pod. This commit applies a second set of minor tweaks suggested by reviewers.
1 parent 6abfedb commit 45673ef

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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
655655
hold the nodes within a tree structure created when the perl source is
656656
compiled. It usually represents a single operation within the perl source,
657657
such as an add, or a function call. The structure has various flags and
@@ -686,15 +686,15 @@ Consider this subroutine call:
686686
The various OPs executed by the Perl interpreter up until the function is
687687
called will: push a mark indicating the start of a new argument stack
688688
frame; 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
692692
normal CV or an XSUB CV.
693693

694694
For a normal Perl subroutine call, C<pp_entersub()> will then: pop the
695695
topmost mark off the mark stack; pop the SV pointers between that mark and
696696
the 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
698698
main loop, until the OPs associated with the last statement of the
699699
function (or an explicit return) will leave any return values as SV
700700
pointers on the stack.
@@ -822,7 +822,7 @@ debugging.)
822822

823823
As mentioned above, almost all runtime data within the perl interpreter is
824824
stored 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
826826
pointer to a body; and since perl 5.10.0, a general payload field. There
827827
are around 17 types, and the type indicates what body (if any) is pointed
828828
to 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

955957
If you need to coerce an SV to a string (e.g. before directly modifying
956958
its 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
13611363
that 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
18131815
limited use and are typically only used to mimic the behaviour of Perl
18141816
builtins. For example there is no way to implement a C<push @a, ...;>
18151817
style 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
18171819
use prototypes.
18181820

18191821
In 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

19231925
Note however that, due to a quirk in parsing, it is possible for a
19241926
C<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
19261928
XSUB. If all such typemap blocks are placed near the start of an XS file,
19271929
then this won't be an issue. Indeed, it can only be a possible issue if
19281930
you want typemap meanings to change during the course of an XS file (which
@@ -1986,7 +1988,7 @@ XSUB keyword or file-scoped directive.
19861988
An XSUB definition consists of a declaration (typically two lines),
19871989
followed by an optional body. The declaration specifies the XSUB's name,
19881990
parameters 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
19901992
should be processed, and what the main C code body of the XSUB consists
19911993
of. Other keywords can change the behaviour of the XSUB, or affect how it
19921994
is registered with Perl, e.g. with extra named aliases. In the absence of
@@ -2105,9 +2107,8 @@ comments.
21052107

21062108
In fact all it does is extract the text between the C<(...)> and split on
21072109
commas, 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

21122113
Each parameter declaration usually generates a C auto variable declaration
21132114
of 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
22332234
the common C<T_PTROBJ> typemap type.
22342235

22352236
Note 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
22372238
example, C<T_PTROBJ> will croak unless the passed SV argument is blessed
22382239
into the C<Foo::Bar> or derived class.
22392240

@@ -2565,9 +2566,11 @@ ellipsis as the last parameter, similar to C function declarations. Its
25652566
main effect is to disable the error check for too many parameters. Any
25662567
declared parameters will still be processed as normal, but the programmer
25672568
will 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

25722575
Note that currently XS doesn't provide any mechanism to autocall
25732576
variable-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

26202623
Note that the L<PPCODE|/The PPCODE: Keyword> keyword, in comparison to
26212624
C<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
26232626
reclaims the passed arguments by setting C<SP> back to the top of the
26242627
stack, 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
32003203
each stack slot is replaced with a new mortal value. When the loop is
32013204
finished, the current stack frame contains a list of mortals, which is
32023205
then 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

32053211
Before pushing return values onto the stack (or storing values at C<ST(i)>
32063212
locations 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;
37603766
information stored in the CV indicates which C library function should be
37613767
autocalled.
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
37643770
XSUB (not just the C<CODE> part) to have alternate cases. It can be
37653771
thought of as a C<switch()> analogue which works at the top-most XS level
37663772
rather 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

38003806
The alias name can be either a simple function name or can include a
38013807
package 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

38043811
The rest of the line following the C<ALIAS> keyword, plus any further
38053812
lines 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

46424650
This becomes more important in the presence of multiple threads; either
46434651
via C<use threads> or where the Perl interpreter is embedded within

0 commit comments

Comments
 (0)