Skip to content

Commit 815fe4e

Browse files
committed
perlxs.pod: update: ALIAS
Rewrite this section: =head3 The ALIAS: Keyword
1 parent f66fd49 commit 815fe4e

File tree

1 file changed

+60
-51
lines changed

1 file changed

+60
-51
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,69 +3675,78 @@ keyword and switch on the value of C<ix>.
36753675

36763676
=head3 The ALIAS: Keyword
36773677

3678-
XXX this keyword can appear anywhere within the body of an XSUB.
3678+
int add(int x, int y)
3679+
ALIAS:
3680+
# implicit: add = 0
3681+
subtract = 1
3682+
multiply = 2 divide = 3
3683+
CODE:
3684+
switch (ix) { ... }
36793685

3680-
The ALIAS: keyword allows an XSUB to have two or more unique Perl names
3681-
and to know which of those names was used when it was invoked. The Perl
3682-
names may be fully-qualified with package names. Each alias is given an
3683-
index. The compiler will setup a variable called C<ix> which contain the
3684-
index of the alias which was used. When the XSUB is called with its
3685-
declared name C<ix> will be 0.
3686+
Note that this keyword can appear anywhere within the body of an XSUB.
36863687

3687-
The following example will create aliases C<FOO::gettime()> and
3688-
C<BAR::getit()> for this function.
3688+
The C<ALIAS> keyword allows a single XSUB to have two or more Perl names
3689+
and to know which of those names was used when it was invoked. Each alias
3690+
is given an integer index value, with the main name of the XSUB being
3691+
index 0. This index is accessible via the variable C<ix> which is
3692+
initialised based on which CV (i.e. which Perl subroutine) was called.
36893693

3690-
bool_t
3691-
rpcb_gettime(host, timep)
3692-
char *host
3693-
time_t &timep
3694-
ALIAS:
3695-
FOO::gettime = 1
3696-
BAR::getit = 2
3697-
INIT:
3698-
printf("# ix = %d\n", ix);
3699-
OUTPUT:
3700-
timep
3694+
Note that an XSUB may be shared by multiple CVs, and each CV may have
3695+
multiple names. Given the C<add> XSUB definition above, and given this
3696+
Perl code:
3697+
3698+
use Foo::Bar;
3699+
BEGIN { *addition = *add }
3700+
3701+
Then in the C<Foo::Bar> namespace, the entries C<add> and C<addition>
3702+
point to the same CV, which has index 0 stored in it; while C<subtract>
3703+
points to a second CV with index 1, and so on. All four CVs point to the
3704+
same C function, C<XS_Foo__Bar__add()>.
3705+
3706+
The alias name can be either a simple function name or can include a
3707+
package name. The alias value to the right of the C<=> may be either a
3708+
literal positive integer or a word (which is expected to be a CPP define).
37013709

3702-
A warning will be produced when you create more than one alias to the same
3703-
value. This may be worked around in a backwards compatible way by creating
3704-
multiple defines which resolve to the same value, or with a modern version
3705-
of ExtUtils::ParseXS you can use a symbolic alias, which are denoted with
3706-
a C<< => >> instead of a C<< = >>. For instance you could change the above
3707-
so that the alias section looked like this:
3710+
The rest of the line following the C<ALIAS> keyword, plus any further
3711+
lines until the next keyword, are assumed to contain zero or more alias
3712+
name and value pairs.
3713+
3714+
A warning will be produced if you create more than one alias to the same
3715+
index value. If you want multiple aliases with the same value, then a
3716+
backwards-compatible way of achieving this is via separate CPP defines to
3717+
the same value, e.g.
3718+
3719+
#define DIVIDE 3
3720+
#define DIVISION 3
37083721

37093722
ALIAS:
3710-
FOO::gettime = 1
3711-
BAR::getit = 2
3712-
BAZ::gettime => FOO::gettime
3723+
divide = DIVIDE
3724+
division = DIVISION
37133725

3714-
this would have the same effect as this:
3726+
Since Perl 5.38.0 or C<ExtUtils::ParseXS> 3.51, alias values may refer to
3727+
other alias names (or to the main function name) by using C<< => >> rather
3728+
than the C<=> symbol:
37153729

37163730
ALIAS:
3717-
FOO::gettime = 1
3718-
BAR::getit = 2
3719-
BAZ::gettime = 1
3731+
divide = 3
3732+
division => divide
37203733

3721-
except that the latter will produce warnings during the build process. A
3722-
mechanism that would work in a backwards compatible way with older
3723-
versions of our tool chain would be to do this:
3734+
Both alias names and C<< => >> values may be fully-qualified:
37243735

3725-
#define FOO_GETTIME 1
3726-
#define BAR_GETIT 2
3727-
#define BAZ_GETTIME 1
3736+
ALIAS:
3737+
red = 1
3738+
COLOR::red => red
3739+
COLOUR::red => COLOR::red
37283740

3729-
bool_t
3730-
rpcb_gettime(host, timep)
3731-
char *host
3732-
time_t &timep
3733-
ALIAS:
3734-
FOO::gettime = FOO_GETTIME
3735-
BAR::getit = BAR_GETIT
3736-
BAZ::gettime = BAZ_GETTIME
3737-
INIT:
3738-
printf("# ix = %d\n", ix);
3739-
OUTPUT:
3740-
timep
3741+
Note that any L<PREFIX|/The MODULE Declaration> is applied to the main
3742+
name of the XSUB, but not to any aliases.
3743+
3744+
See L</T_PTROBJ and opaque handles> for a fully-worked example using
3745+
aliases.
3746+
3747+
See L<INTERFACE|/The INTERFACE: Keyword> below for an alternative to
3748+
C<ALIAS> which is more suited for autocall. Note that C<ALIAS> should not
3749+
be used together with either of C<INTERFACE> or C<ATTRS>.
37413750

37423751
=head3 The INTERFACE: Keyword
37433752

0 commit comments

Comments
 (0)