Skip to content

Commit e975899

Browse files
committed
perlxs.pod: add "Sharing XSUB bodies" section
Populate the introduction to this new section.
1 parent 813423d commit e975899

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
@@ -3605,7 +3605,32 @@ or C<INTERFACE>; in this case, the attributes are just silently ignored.
36053605

36063606
=head2 Sharing XSUB bodies
36073607

3608-
XXX TBC
3608+
Sometimes you end up writing several XSUBs which are very similar: they
3609+
all have the same signature, have the same generated code to convert
3610+
arguments and return values between Perl and C, and may only differ in a
3611+
few lines in the main body or in which C library function they wrap. It is
3612+
in fact possible to share the same XSUB function among multiple Perl CVs.
3613+
For example, C<&Foo::Bar::add> and C<&Foo::Bar::subtract> could be two
3614+
separate CVs in the Perl namespace which both point to the same XSUB,
3615+
C<XS_Foo__Bar__add()> say. But each CV holds some sort of unique
3616+
identifier which can be accessed by the XSUB so that it can determine
3617+
whether it should behave as C<add> or C<subtract>.
3618+
3619+
Both the C<ALIAS> and C<INTERFACE> keywords (described below) allow
3620+
multiple CVs to share the same XSUB. The difference between them is that
3621+
C<ALIAS> is intended for when you supply the main body of the XSUB
3622+
yourself (e.g. using C<CODE>): it sets an integer variable, C<ix> (derived
3623+
from the passed CV), which you can use in a C<switch()> statement or
3624+
similar. Conversely, C<INTERFACE> is intended for use with autocall;
3625+
information stored in the CV indicates which C library function should be
3626+
autocalled.
3627+
3628+
Finally, there is the C<CASE> keyword, which allows the whole body of an
3629+
XSUB (not just the C<CODE> part) to have alternate cases. It can be
3630+
thought of as a C<switch()> analogue which works at the top-most XS level
3631+
rather than at the C level. The value the C<CASE> acts on could be
3632+
C<items> for example, or it could be used in conjunction with the C<ALIAS>
3633+
keyword and switch on the value of C<ix>.
36093634

36103635
=head3 The ALIAS: Keyword
36113636

0 commit comments

Comments
 (0)