Mailing List Archive

some proposed XS pod changes
As part of my intention to completely revamp the perlxs.pod and
perlxstut.pod documentation, here are a few items for discussion.

--------

1) Whether to make XSUB signatures ANSI by default in perlxs, perlxstut.

Most existing XS code, and all the docs, use the older "K&R" style of XSUB
declaration, which mirrors the old K&R way of declaring C functions:

int
add(x, y)
int x
int y
CODE:
...

For the last 24 years, XS has also supported (via analogy with ANSI C)
"ANSI" style XS declarations:

int
add(int x, int y)
CODE:
...

But no-one uses this newer style because it's not used much in the current
doc examples.

I propose making the emphasis of the docs, and most of the examples,
switch to ANSI.

AFAIK, the new syntax allows you to do just about everything you can with
the old syntax, except include on-the-fly typemapping, which overrides the
default typemap, e.g.:

int
add(x, y)
int x
int y = do_weird_int_munging(SvIV(ST[1]))
CODE:

But since typemaps can now be defined inline within the .xs file via the
TYPEMAP:: <<EOF
mechanism, that seems less of an issue.

The only slightly messy thing about the new syntax is that you have
to have the whole signature on one line, or use \ to split the lines up:


int add(int x, \
int y)

The perlxs.pod text will still describe the old syntax, while
perlxstut.pod won't, apart from a brief mention that it exists (and refer
therm to perlxs.pod), to help people understand older XS code in the wild.
And of course the parser will continue to support the old syntax in the
decades to come.

-------

2) What should the earliest perl and xsubpp version be that the docs
should mention? E.g. remove any mentions of workarounds for 5.005 issues,
or "introduced in 5.003alpha7" type text?

This affects people who are writing new XS code with the intention that is
still capable of being installed on old perl installations. here, the
(modern) docs are telling them "don't use feature X if you want your XS
code to to run on 5.005 platforms" (or whatever).

And relatedly, what should be the earliest format version of XS file that
the current xsubpp should support? Currently the latest Extutils::ParseXS
and xsubpp script can (in principle) be installed on 5.6.0 perl. In
addtion, the current parser is capable of recognising older XS code and
injkect workarounds for things that have changed in the meantime.

Should we continue including various hacks and workarounds in the parser
to handle ancient .xs files? Assuming there are any such hacks - I'm not
too sure. But if we formally declare that 5.6.0 (for example) is the
minimum, then I can remove any such hack I might come across. Similalry,
what is the oldest perl we should support the current Extutils::ParseXS
running on?

For all of these, I'm leaning towards 5.8.0.

-------

3) Whether to replace the rpcb_gettime() example used a million times in
perlxs.pod with some other example library function, since this function
is no longer common, and won't work out of the box on my linux system (I'd
have to install non-default libraries and header functions).

I.e. unless someone has any strong attachment to it, I'll use something
else, or maybe use an hypothetical function rather than real one.

-------

4) Whether to make h2xs inject "PROTOTYPES: FALSE" into the .xs file when
creating a new skeleton distribution.

By default, xsubpp doesn't generate perl prototypes for the XSUBs that get
inserted into the perl namespace. (It will if the .xs file includes
"PROTOTYPES: TRUE" near the top.)

But the XS parser, xsubpp, will generate a warning if PROTOTYPES isn't
specified one way or another.

These days we generally regard adding a function prototype to be a
mistake, unless someone is explicitly trying to emulate a perl-builtin
(e.g. using '&' as part of a prototype for a mymap() function).

So I propose that h2xs be made to add "PROTOTYPES: FALSE" into the
skeleton .xs file that it generates, since this will be the useful default
99% of the time. (That keyword has been available since 1995).

-------

5) Whether to make h2xs inject '#define PERL_REENTRANT' into the .xs file
when creating a new skeleton distribution. (Possibly only if the version
arg passed to h2xs is >= 5.16)?

This macro has been available since 5.16.0, and I assume it is a NOOP on
earlier perls. It sounds like setting it by default is probably a Good
Thing, rather than having someone write their XS code, *then* find out
later it has a race condition under threads, *then* find the section in
perlxs which recommends setting it. I don't know whether there are any
downsides to setting by default.


--
Never do today what you can put off till tomorrow.
Re: some proposed XS pod changes [ In reply to ]
On Mon, Mar 4, 2024 at 3:41?PM Dave Mitchell <davem@iabyn.com> wrote:

> 2) What should the earliest perl and xsubpp version be that the docs
> should mention? E.g. remove any mentions of workarounds for 5.005 issues,
> or "introduced in 5.003alpha7" type text?
>
> This affects people who are writing new XS code with the intention that is
> still capable of being installed on old perl installations. here, the
> (modern) docs are telling them "don't use feature X if you want your XS
> code to to run on 5.005 platforms" (or whatever).


Obviously, as someone who doesn't work in this area, my opinions aren't
worth that much, but perhaps keep the old docs, but perhaps rename them so
people can easily refer to them if needed?

Best,
Ovid
Re: some proposed XS pod changes [ In reply to ]
On Mon, Mar 4, 2024, at 09:53, Ovid wrote:
> On Mon, Mar 4, 2024 at 3:41?PM Dave Mitchell <davem@iabyn.com> wrote:
>> This affects people who are writing new XS code with the intention that is
>> still capable of being installed on old perl installations. here, the
>> (modern) docs are telling them "don't use feature X if you want your XS
>> code to to run on 5.005 platforms" (or whatever).
>
> Obviously, as someone who doesn't work in this area, my opinions aren't worth that much, but perhaps keep the old docs, but perhaps rename them so people can easily refer to them if needed?

They'll be in git. If somebody needs to support a 25 year old Perl with new XS, consulting git for docs feels like a reasonable cost. The benefit is that people writing code today for today's perl don't get confused by search results, or have to think about which support window they want. "I wanted to support even older perls, so I started from perlxsold, because some people still run a 2018 perl!" is an outcome we don't want.

--
rjbs
Re: some proposed XS pod changes [ In reply to ]
"Ricardo Signes" <perl.p5p@rjbs.manxome.org> writes:

> On Mon, Mar 4, 2024, at 09:53, Ovid wrote:
>> On Mon, Mar 4, 2024 at 3:41?PM Dave Mitchell <davem@iabyn.com> wrote:
>>> This affects people who are writing new XS code with the intention that is
>>> still capable of being installed on old perl installations. here, the
>>> (modern) docs are telling them "don't use feature X if you want your XS
>>> code to to run on 5.005 platforms" (or whatever).
>>
>> Obviously, as someone who doesn't work in this area, my opinions
>> aren't worth that much, but perhaps keep the old docs, but perhaps
>> rename them so people can easily refer to them if needed?
>
> They'll be in git. If somebody needs to support a 25 year old Perl with
> new XS, consulting git for docs feels like a reasonable cost.

They would want to have the perl version they're targeting installed
anyway, so can just run `perldoc perlxs` from the right version.

Or they can use the version dropdown on https://perldoc.perl.org/perlxs.

- ilmari
Re: some proposed XS pod changes [ In reply to ]
On Mon, Mar 4, 2024 at 3:41?PM Dave Mitchell <davem@iabyn.com> wrote:

> 1) Whether to make XSUB signatures ANSI by default in perlxs, perlxstut.
>
> Most existing XS code, and all the docs, use the older "K&R" style of XSUB
> declaration, which mirrors the old K&R way of declaring C functions:
>
> int
> add(x, y)
> int x
> int y
> CODE:
> ...
>
> For the last 24 years, XS has also supported (via analogy with ANSI C)
> "ANSI" style XS declarations:
>
> int
> add(int x, int y)
> CODE:
> ...
>
> But no-one uses this newer style because it's not used much in the current
> doc examples.
>
> I propose making the emphasis of the docs, and most of the examples,
> switch to ANSI.
>
> AFAIK, the new syntax allows you to do just about everything you can with
> the old syntax, except include on-the-fly typemapping, which overrides the
> default typemap, e.g.:
>
> int
> add(x, y)
> int x
> int y = do_weird_int_munging(SvIV(ST[1]))
> CODE:
>
> But since typemaps can now be defined inline within the .xs file via the
> TYPEMAP:: <<EOF
> mechanism, that seems less of an issue.
>
> The only slightly messy thing about the new syntax is that you have
> to have the whole signature on one line, or use \ to split the lines up:
>
>
> int add(int x, \
> int y)
>
> The perlxs.pod text will still describe the old syntax, while
> perlxstut.pod won't, apart from a brief mention that it exists (and refer
> therm to perlxs.pod), to help people understand older XS code in the wild.
> And of course the parser will continue to support the old syntax in the
> decades to come.
>

I had been using XS for 10+ years before I discovered ANSI style XS. I've
been using it since and it has absolutely made my XS experience a lot more
pleasant.


> 2) What should the earliest perl and xsubpp version be that the docs
> should mention? E.g. remove any mentions of workarounds for 5.005 issues,
> or "introduced in 5.003alpha7" type text?
>
> This affects people who are writing new XS code with the intention that is
> still capable of being installed on old perl installations. here, the
> (modern) docs are telling them "don't use feature X if you want your XS
> code to to run on 5.005 platforms" (or whatever).
>
> And relatedly, what should be the earliest format version of XS file that
> the current xsubpp should support? Currently the latest Extutils::ParseXS
> and xsubpp script can (in principle) be installed on 5.6.0 perl. In
> addtion, the current parser is capable of recognising older XS code and
> injkect workarounds for things that have changed in the meantime.
>
> Should we continue including various hacks and workarounds in the parser
> to handle ancient .xs files? Assuming there are any such hacks - I'm not
> too sure. But if we formally declare that 5.6.0 (for example) is the
> minimum, then I can remove any such hack I might come across. Similalry,
> what is the oldest perl we should support the current Extutils::ParseXS
> running on?
>
> For all of these, I'm leaning towards 5.8.0.
>

That sounds like a reasonable baseline to me.


> 3) Whether to replace the rpcb_gettime() example used a million times in
> perlxs.pod with some other example library function, since this function
> is no longer common, and won't work out of the box on my linux system (I'd
> have to install non-default libraries and header functions).
>
> I.e. unless someone has any strong attachment to it, I'll use something
> else, or maybe use an hypothetical function rather than real one.
>

Sounds like a reasonable idea. I would nominate clock_gettime.


>
> 4) Whether to make h2xs inject "PROTOTYPES: FALSE" into the .xs file when
> creating a new skeleton distribution.
>
> By default, xsubpp doesn't generate perl prototypes for the XSUBs that get
> inserted into the perl namespace. (It will if the .xs file includes
> "PROTOTYPES: TRUE" near the top.)
>
> But the XS parser, xsubpp, will generate a warning if PROTOTYPES isn't
> specified one way or another.
>
> These days we generally regard adding a function prototype to be a
> mistake, unless someone is explicitly trying to emulate a perl-builtin
> (e.g. using '&' as part of a prototype for a mymap() function).
>
> So I propose that h2xs be made to add "PROTOTYPES: FALSE" into the
> skeleton .xs file that it generates, since this will be the useful default
> 99% of the time. (That keyword has been available since 1995).
>

Yes please. Module::Build has been doing prototype-less XS for like 20
years and it's always painful to remember that I have to explicitly add it
when working with MakeMaker.


> 5) Whether to make h2xs inject '#define PERL_REENTRANT' into the .xs file
> when creating a new skeleton distribution. (Possibly only if the version
> arg passed to h2xs is >= 5.16)?
>
> This macro has been available since 5.16.0, and I assume it is a NOOP on
> earlier perls. It sounds like setting it by default is probably a Good
> Thing, rather than having someone write their XS code, *then* find out
> later it has a race condition under threads, *then* find the section in
> perlxs which recommends setting it. I don't know whether there are any
> downsides to setting by default.
>

While usually a good idea, macro-defining a bunch of standard POSIX
functions can also break a lot of code. Not only because they will suddenly
require an interpreter pointer in places where they previously didn't, but
also because it can mess up C++ classes badly when they happen to have a
method with that same name (I've seen that happen before). I think better
documentation may be better than enabling it by default.

Leon
Re: some proposed XS pod changes [ In reply to ]
On Mon, Mar 04, 2024 at 03:53:42PM +0100, Ovid wrote:
> Obviously, as someone who doesn't work in this area, my opinions aren't
> worth that much, but perhaps keep the old docs, but perhaps rename them so
> people can easily refer to them if needed?

To clarify, I just want to delete a few sentences like this from the docs:

In versions of 5.002 prior to the gamma version, the test script in Example
1 will not function properly. You need to change the "use lib" line to
read:

I don't think there's going to be anyone writing fresh XS code who still
needs to know how to handle a workaround for perls released between
1994-Oct-17 and 1996-Feb-11.


--
I took leave and went to hear Mrs Turner's daughter play on the harpsicon,
but Lord, it was enough to make any man sick to hear her; yet I was forced
to commend her highly.
-- The Diary of Samuel Pepys, 1 May 1663