Skip to content

Commit f66fd49

Browse files
committed
perlxs.pod: add "Sharing XSUB bodies" section
Populate the introduction to this new section.
1 parent 4d48858 commit f66fd49

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3646,7 +3646,32 @@ or C<INTERFACE>; in this case, the attributes are just silently ignored.
36463646

36473647
=head2 Sharing XSUB bodies
36483648

3649-
XXX TBC
3649+
Sometimes you want to write several XSUBs which are very similar: they
3650+
all have the same signature, have the same generated code to convert
3651+
arguments and return values between Perl and C, and may only differ in a
3652+
few lines in the main body or in which C library function they wrap. It is
3653+
in fact possible to share the same XSUB function among multiple Perl CVs.
3654+
For example, C<&Foo::Bar::add> and C<&Foo::Bar::subtract> could be two
3655+
separate CVs in the Perl namespace which both point to the same XSUB,
3656+
C<XS_Foo__Bar__add()> say. But each CV holds some sort of unique
3657+
identifier which can be accessed by the XSUB so that it can determine
3658+
whether it should behave as C<add> or C<subtract>.
3659+
3660+
Both the C<ALIAS> and C<INTERFACE> keywords (described below) allow
3661+
multiple CVs to share the same XSUB. The difference between them is that
3662+
C<ALIAS> is intended for when you supply the main body of the XSUB
3663+
yourself (e.g. using C<CODE>): it sets an integer variable, C<ix> (derived
3664+
from the passed CV), which you can use in a C<switch()> statement or
3665+
similar. Conversely, C<INTERFACE> is intended for use with autocall;
3666+
information stored in the CV indicates which C library function should be
3667+
autocalled.
3668+
3669+
Finally, there is the C<CASE> keyword, which allows the whole body of an
3670+
XSUB (not just the C<CODE> part) to have alternate cases. It can be
3671+
thought of as a C<switch()> analogue which works at the top-most XS level
3672+
rather than at the C level. The value the C<CASE> acts on could be
3673+
C<items> for example, or it could be used in conjunction with the C<ALIAS>
3674+
keyword and switch on the value of C<ix>.
36503675

36513676
=head3 The ALIAS: Keyword
36523677

0 commit comments

Comments
 (0)