Perl changing the current package?

164 views Asked by At

Without using a source-filter, is there a way to change the current running package? I'm trying to accomplish the same thing oose.pm does, and I'm wondering if I can drop my users in a non-main package.

1

There are 1 answers

2
rafl On

I think you'll be able to do that by changing PL_curstash and PL_curstname on the C level.

PL_curstash = gv_stashpvs("Some::Package", GV_ADD);
sv_setpvs(PL_curstname, "Some::Package");

PL_curstash is the stash of the current package during compilation, PL_curstname is its name.

Update:

I've found this problem kind of interesting and implemented the solution as Devel::ChangePackage. Turns out what I initially suggested just works. You can get it either from http://github.com/rafl/devel-changepackage, or from a CPAN mirror near you once they have updated.