Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing special tokens in XS [perl] [duplicate]

Tags:

perl

xs

perlapi

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.

like image 555
Eugen Konkov Avatar asked Nov 18 '22 13:11

Eugen Konkov


2 Answers

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.

like image 136
rurban Avatar answered Nov 24 '22 00:11

rurban


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.

like image 33
nwellnhof Avatar answered Nov 24 '22 00:11

nwellnhof