How to find current package name from perl XS?

238 views Asked by At

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?

1

There are 1 answers

12
Håkon Hægland On BEST ANSWER

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));