Steve Hay <[EMAIL PROTECTED]> writes:
>How does an XSUB find out what package it is compiled into?

An XSUB isn't compiled into a package ;-)

Its CV is entered into a stash but its "body" (which is what __PACKAGE__
refrers to) doesn't really have any meaning. That is to say 
"current package" (i.e. "current context op" i.e. PL_curcop) does not 
change when you call an XSUB.

So CopSTASHPV(PL_curcop) gives you the name of the calling package
(if I have right macro name...)

This can be considered a feature ;-)
This is handy because it isn't easy to do caller() from XS ...

The XSUB "knows" what stash its CV in from the MODULE line.
For use in typemaps I think $Package will expands to current package
as declared on MODULE, but I don't think it is documented.
I seem to recall some "quoting issues" with using it.
But see lib/ExtUtils/typemap and T_STDIO et. al. for typical (mis)?use ;-)

Normally you only need to know for constructors to mimic 1 operand 
form of bless - but that is discouraged you should pass class name
so it is inheritable:

void
new(char *Class,...)
{
 ...
 sv_setsv(ST(0), sv_bless(newRV_noinc(inner_sv), gv_stashpv(Class,1)));
 ...
}

Note using 'Class' not 'class' to avoid keyword muddle if C++
gets its hands on things.



>
>It's easy in a pure Perl sub -- just use __PACKAGE__ -- but accessing 
>that from an XSUB doesn't work (or at least, not like this):
>
>#include "EXTERN.h"
>#include "perl.h"
>#include "XSUB.h"
>
>MODULE = Foo        PACKAGE = Foo       
>
>void
>foo()
>PPCODE:
>{
>    warn("I'm compiled into %s\n", SvPVX(eval_pv("__PACKAGE__", 0)));
>}
>
>The above outputs:
>
>I'm compiled into main

Actually it means "I was called from code compiled into main, so when I just 
compiled that litteral I used that as the package to put the code 
in and lexer expanded __PACKAGE__ to "main" for me.


>
>How do I get the "Foo" that I wanted?

  warn("I'm compiled into "%s\n", "Foo");   // Match MODULE line ;-)

  

>
>- Steve
>
>
>
>------------------------------------------------
>Radan Computational Ltd.
>
>The information contained in this message and any files transmitted with it are 
>confidential and intended for the addressee(s) only.  If you have received this 
>message in error or there are any problems, please notify the sender immediately.  
>The unauthorized use, disclosure, copying or alteration of this message is strictly 
>forbidden.  Note that any views or opinions presented in this email are solely those 
>of the author and do not necessarily represent those of Radan Computational Ltd.  The 
>recipient(s) of this message should check it and any attached files for viruses: 
>Radan Computational will accept no liability for any damage caused by any virus 
>transmitted by this email.

Reply via email to