In perl special tokens like __PACKAGE__
, __SUB__
, __FILE__
, __LINE__
exists and available from script.
I may get value of __PACKAGE__
from XS
as HvNAME( PL_currstash )
, I suppose.
But how to access others?
Is there special interface to access all of them from XS
? Like: CTX->package
, CTX->sub
etc.
You can look them up one by one in toke.c
for the compile-time values:
__PACKAGE__
=> HvNAME(PL_curstash)
or PL_curstname
__FILE__
=> CopFILE(PL_curcop)
(at compile-time)__LINE__
=> CopLINE(PL_curcop)
(at compile-time)__SUB__
=> PL_compcv
If you need them at run-time look at the various data fields available in the context caller_cx
and current sub (cv
). There's no context struct as in parrot or perl6 passed around, rather a stack of active context blocks.
Perl subroutines are represented in C with type CV
. The CV
for the XSUB is passed in the cv
argument:
#define XSPROTO(name) void name(pTHX_ CV* cv)
You can get the name of the XSUB with GvNAME(CvGV(cv))
. This is especially useful if you register an XSUB under multiple names, for example with the ALIAS
or INTERFACE
keywords, or in typemaps.
To get the current stash (__PACKAGE__
equivalent), I'd suggest to use CvSTASH(cv)
.
__FILE__
and __LINE__
are provided by the C compiler as macro.
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