Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find current package name from perl XS?

Tags:

perl

perlapi

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?

like image 999
Eugen Konkov Avatar asked Mar 01 '17 12:03

Eugen Konkov


People also ask

What is XS file in Perl?

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.

How to call Perl module function?

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.


1 Answers

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));
like image 84
Håkon Hægland Avatar answered Oct 06 '22 09:10

Håkon Hægland