develooper Front page | perl.perl5.porters | Postings from August 2023

Calling eval_sv() at compiletime with the right context

Thread Next
Paul "LeoNerd" Evans
August 21, 2023 20:03
Calling eval_sv() at compiletime with the right context
Message ID:
I'm running some C code that's somewhere within a big nest of C
function calls, ultimately invoked from Perl's PL_keyword_plugin. I.e.
happening at parse-time. The code is trying to eval_sv() a string
parsed out of the source code. Here's the call currently:

I find that when that happens, the eval'ed code runs in package "main",
has no strict, no warnings, no features from the prevailing `use
VERSION`, etc... So I suspect I am doing something wrong. But I have
no idea what.

Evidentally I'm not the first to encounter this problem. mauke said it
sounded familiar, and offered this workaround:

I took that inspiration and adjusted it a bit; now I'm doing this:

    // We have to fool eval_sv() into seeing the right package
    COP *fakecop = (COP *)newSTATEOP(0, NULL, NULL);
    CopSTASH_set(fakecop, PL_curstash);
    PL_curcop = fakecop;

    eval_sv_rethrow(value, G_SCALAR);

This now fixes a littlebit. The invoked code now sees the correct
package, e.g. if I inject the code `print STDERR __PACKAGE__, "\n"`

Problem is everything else is still wrong. I still have no strict and
no feature flags. Though, I do get warnings. I seem to have this
bizarre mixture of some state but not the rest.

I suspect I'm somewhat on the right lines here in that I need to create
a fake COP to put into PL_curcop, but apparently it doesn't have the
right settings for hints or feature flags.

Can anyone suggest some further guidance?

Paul "LeoNerd" Evans      |  |

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About