10

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.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
  • What do you mean for *perl from XS* ? I don't understand your question, please explain. – F. Hauri - Give Up GitHub Mar 06 '17 at 16:07
  • @F.Hauri in perl you may write C code. To simplify C<->Perl interaction there is [`XS`](http://perldoc.perl.org/perlxstut.html) mechanism exists. I ask how to access token `__SUB__` from `XS` – Eugen Konkov Mar 06 '17 at 16:18
  • In the book [Perl Developer's Dictionary](http://www.informit.com/store/perl-developers-dictionary-9780672320675) page 267 it says that the tokens `__LINE__` and `__FILE__` are replaced at compile time. See more [here](https://books.google.no/books?id=GvGCdRh1ryMC&pg=PA267&lpg=PA267&dq=perl+__FILE__+__LINE__+compile-time&source=bl&ots=Zvr370V1cn&sig=E7sqCpZG6gnDDhgKL7Kulk7yr3U&hl=en&sa=X&ved=0ahUKEwjGw6aLpMLSAhWCKywKHQH1BycQ6AEIKDAC#v=onepage&q=perl%20__FILE__%20__LINE__%20compile-time&f=false') – Håkon Hægland Mar 06 '17 at 16:25
  • Rather `PL_curstname` for the package name, no? (I'm not sure about it, but from what I read in [`intrpvar.h`](https://github.com/Perl/perl5/blob/blead/intrpvar.h), it seems more like it. Have a look at this file, and at [`embedvar.h`](https://github.com/Perl/perl5/blob/blead/embedvar.h) as well, it could help you find what you are looking for. – Dada Mar 06 '17 at 16:43
  • As @HåkonHægland said, `__LINE__` and `__FILE__` are replaced at compile time. However, you should still be able to get informations about the line or file, since when a `die` happens for instance, Perl tells you what line/file it happen at. I'd suggest you have a look in Perl sources, especially what happens when there is a `die` (for instance because of a division by 0). Not sure it will show you what you're looking for, but that's how I'd do it. – Dada Mar 06 '17 at 16:44
  • @Dada I believe that `__FILE__`, `__LINE__` is stored at COP (`PL_curcop`/`PL_op`). `__SUB__` is accessable as OP->cv->name... The question is not about how to access this data, but about accessing this data through some *special interface* – Eugen Konkov Mar 06 '17 at 16:53
  • So I assume `CopFILE` and `CopLINE` macros are not what you are looking for? (sorry if I don't quite understand your question; I don't write XS code, I just play with Perl sources now and then) – Dada Mar 06 '17 at 17:16
  • 4
    Those are Perl operators, so you can't call them except from Perl code. You can surely find the file and line number of the statement that called your XS sub, though, since `caller` does it. – ikegami Mar 06 '17 at 17:46

3 Answers3

3

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.

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
rurban
  • 4,025
  • 24
  • 27
2

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.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
2

The C equivalent to __FILE__ is __FILE__.

The C equivalent to __LINE__ is __LINE__.

The C99 equivalent to __SUB__ is __func__. There wasn't anything standard before.

There's no C equivalent to __PACKAGE__ because C doesn't have namespaces.

That said, I don't think you want information about the current line of execution; I think you want information about the XS sub's caller. That means you're actually asking for the XS equivalent of caller.

The XS equivalent of caller is caller_cx. Looking at Perl_cx_dump in scope.c should give an idea how to use the returned PERL_CONTEXT structure.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Why are you posting a copy-and-paste answer, one directly copied from your [previous answer](https://stackoverflow.com/a/45537463/522444)? – Hovercraft Full Of Eels Aug 06 '17 at 23:21
  • Then it would probably be better for you to figure out which one should be closed, and vote on that, no? Or at least post a comment linking your prior answer, since it's not felt to be correct to copy and paste answers -- at least each time I've seen moderators handle this (and they have software to identify this), they usually delete one or both answers. – Hovercraft Full Of Eels Aug 06 '17 at 23:40
  • Because I'm not a PERL expert. Regardless, we'll let the moderators decide. – Hovercraft Full Of Eels Aug 06 '17 at 23:46
  • @Hovercraft Full Of Eels, I didn't know about this question when I answered the other. – ikegami Aug 06 '17 at 23:56
  • Again, it's no never mind to me. We have a plagiarism detector running (which is how I was flagged to this answer and its dup), and so the moderators will find the duplicates and handle them, if you don't handle them yourself. – Hovercraft Full Of Eels Aug 06 '17 at 23:57
  • @Hovercraft Full Of Eels, The other question will get closed as a dup, but I'm not going to hurry that close a question as a duplicate of one that didn't have an answer. – ikegami Aug 07 '17 at 00:03
  • More problematic is that 5 of 6 paragraphs of this are wrong. – rurban Aug 17 '17 at 13:17
  • @rurban, Really??? That's a very harsh assessment. Fixed to use more precise wording. – ikegami Aug 17 '17 at 14:05