To get current context I find caller_cx
function in perlapi
. But there is no description for structure. In perl source code perl.h
I can find only this typedef
:
typedef struct context PERL_CONTEXT;
Is there examples how to use this struct returned by caller_cx
to find current package from XS?
XS is a Perl foreign function interface through which a program can call a C or C++ subroutine. XS or xsub is an abbreviation of "eXternal Subroutine", where external refers to programming languages external to Perl.
A module can be loaded by calling the use function. #!/usr/bin/perl use Foo; bar( "a" ); blat( "b" ); Notice that we didn't have to fully qualify the package's function names. The use function will export a list of symbols from a module given a few added statements inside a module.
The context
struct is defined cop.h
as mentioned by @Dada in the comments:
struct context {
union {
struct block cx_blk;
struct subst cx_subst;
} cx_u;
};
also the block
structures are defined in cop.h
.
By inspecting the C implementation of the Perl caller
function in pp_ctl.c
(line 1850), I think you can get the package name using the following code:
const PERL_CONTEXT *cx = caller_cx(0, NULL);
char *pack_name = HvNAME((HV*)CopSTASH(cx->blk_oldcop));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With