Skip to content

Commit 0e74c95

Browse files
committed
perlxs.pod: update: ALIAS
Rewrite this section: =head3 The ALIAS: Keyword
1 parent e975899 commit 0e74c95

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
@@ -3634,69 +3634,78 @@ keyword and switch on the value of C<ix>.
36343634

36353635
=head3 The ALIAS: Keyword
36363636

3637-
XXX this keyword can appear anywhere within the body of an XSUB.
3637+
int add(int x, int y)
3638+
ALIAS:
3639+
# implicit: add = 0
3640+
subtract = 1
3641+
multiply = 2 divide = 3
3642+
CODE:
3643+
switch (ix) { ... }
36383644

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

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

3649-
bool_t
3650-
rpcb_gettime(host, timep)
3651-
char *host
3652-
time_t &timep
3653-
ALIAS:
3654-
FOO::gettime = 1
3655-
BAR::getit = 2
3656-
INIT:
3657-
printf("# ix = %d\n", ix);
3658-
OUTPUT:
3659-
timep
3653+
Note that an XSUB may be shared by multiple CVs, and each CV may have
3654+
multiple names. Given the C<add> XSUB definition above, and given this
3655+
Perl code:
3656+
3657+
use Foo::Bar;
3658+
BEGIN { *add2 = *add }
3659+
3660+
Then in the C<Foo::Bar> namespace, the entries C<add> and C<add2> point to
3661+
the same CV, which has index 0 stored in it; while C<subtract> points to a
3662+
second CV with index 1, and so on. All four CVs point to the same C
3663+
function, C<XS_Foo__Bar__add()>.
3664+
3665+
The alias name can be either a simple function name or can include a
3666+
package name. The alias value to the right of the C<=> may be either a
3667+
literal positive integer or a word (which is expected to be a CPP define).
36603668

3661-
A warning will be produced when you create more than one alias to the same
3662-
value. This may be worked around in a backwards compatible way by creating
3663-
multiple defines which resolve to the same value, or with a modern version
3664-
of ExtUtils::ParseXS you can use a symbolic alias, which are denoted with
3665-
a C<< => >> instead of a C<< = >>. For instance you could change the above
3666-
so that the alias section looked like this:
3669+
The rest of the line following the C<ALIAS> keyword, plus any further
3670+
lines until the next keyword, are assumed to contain zero or more alias
3671+
name and value pairs.
3672+
3673+
A warning will be produced if you create more than one alias to the same
3674+
index value. If you want multiple aliases with the same value, then a
3675+
backwards-compatible way of achieving this is via separate CPP defines to
3676+
the same value, e.g.
3677+
3678+
#define DIVIDE 3
3679+
#define DIVISION 3
36673680

36683681
ALIAS:
3669-
FOO::gettime = 1
3670-
BAR::getit = 2
3671-
BAZ::gettime => FOO::gettime
3682+
divide = DIVIDE
3683+
division = DIVISION
36723684

3673-
this would have the same effect as this:
3685+
Since Perl 5.38.0 or ExtUtils::ParseXS 3.51, alias values may refer to
3686+
other alias names (or the main function name) by using C<< => >> rather
3687+
than the C<=> symbol:
36743688

36753689
ALIAS:
3676-
FOO::gettime = 1
3677-
BAR::getit = 2
3678-
BAZ::gettime = 1
3690+
divide = 3
3691+
division => divide
36793692

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

3684-
#define FOO_GETTIME 1
3685-
#define BAR_GETIT 2
3686-
#define BAZ_GETTIME 1
3695+
ALIAS:
3696+
red = 1
3697+
COLOR::red => red
3698+
COLOUR::red => COLOR::red
36873699

3688-
bool_t
3689-
rpcb_gettime(host, timep)
3690-
char *host
3691-
time_t &timep
3692-
ALIAS:
3693-
FOO::gettime = FOO_GETTIME
3694-
BAR::getit = BAR_GETIT
3695-
BAZ::gettime = BAZ_GETTIME
3696-
INIT:
3697-
printf("# ix = %d\n", ix);
3698-
OUTPUT:
3699-
timep
3700+
Note that any L<PREFIX|/The MODULE Declaration> is applied to the main
3701+
name of the XSUB, but not to any aliases.
3702+
3703+
See L</T_PTROBJ and opaque handles> for a fully-worked example using
3704+
aliases.
3705+
3706+
See L<INTERFACE|/The INTERFACE: Keyword> below for an alternative to
3707+
C<ALIAS> which is more suited for autocall. Note that C<ALIAS> should not
3708+
be used together with either of C<INTERFACE> or C<ATTR>.
37003709

37013710
=head3 The INTERFACE: Keyword
37023711

0 commit comments

Comments
 (0)