develooper Front page | perl.xs | Postings from March 2008

probing for perlapi.h symbols: PL_unitcheckav vs KEY_UNITCHECK

From:
Adriano Ferreira
Date:
March 3, 2008 09:17
Subject:
probing for perlapi.h symbols: PL_unitcheckav vs KEY_UNITCHECK
Message ID:
73ddeb6c0803030909s60f08635v6893e45ceb95480c@mail.gmail.com
Recently I tried my first XS code for Devel-Hook dist
(http://search.cpan.org/dist/Devel-Hook).

There I tried to include conditional compilation for dealing with
UNITCHECK blocks (introduced at perl 5.9.4 IIRC) by testing if
PL_unitcheckav (the internal array of UNITCHECK blocks that the
interpreter maintains) was defined or not:

The code is at:

http://search.cpan.org/src/FERREIRA/Devel-Hook-0.004/Hook.xs

And the relevant part is:

AV *_get_unitcheck_array() {
#ifdef PL_unitcheckav
    if ( !PL_unitcheckav ) {
        PL_unitcheckav = newAV();
    }
    return PL_unitcheckav;
#else
    croak( "UNITCHECK not implemented in this release of perl" );
#endif
}

But I found that this just doesn't work in certain installations of
Perl. Even with Perl 5.10.0, the above function was expanded to:

AV *_get_unitcheck_array() {

    croak( "UNITCHECK not implemented in this release of perl" );
}

What suggested me this approach was that PL_unitcheckav was defined as
a #define at "perlapi.h". But when I looked closer at the result of
the preprocessor, I saw that there was an actual symbol
"PL_unitcheckav" as well. Maybe that's the reason it did not work out.
(I am not sure as I am rusty with C programming and understand very
little of Perl internals programming).

A working solution that I found was including "keywords.h" and testing
for "KEY_UNITCHECK"

(as done here: http://search.cpan.org/src/FERREIRA/Devel-Hook-0.005/Hook.xs)

#include "keywords.h"

[ ... ]

AV *_get_unitcheck_array() {
#ifdef KEY_UNITCHECK
    if ( !PL_unitcheckav ) {
        PL_unitcheckav = newAV();
    }
    return PL_unitcheckav;
#else
    croak( "UNITCHECK not implemented in this release of perl" );
#endif
}

That works fine as intented. But it seems weird to me.

So my doubt is: what is the right way to do it? Where can I find more
about doing proper XS coding and programming with the Perl API? I
could not see any of this in "perlxs", "perlxstut", "perlapi",
"perlguts", etc.

Regards,
Adriano Ferreira

P.S. Another perplexing behavior was that at CPAN Testers reports:

http://cpantesters.perl.org/show/Devel-Hook.html#Devel-Hook-0.004

there were two PASS tests (as I would have expected since the
conditional compiling was defaulting to say that UNITCHECK was not
supported) but also three FAIL tests (which could only be triggered
because the conditional compilation actually worked in these
installations and hitted a test bug - only reachable if UNITCHECK
support was detected).



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About