develooper Front page | perl.perl5.porters | Postings from July 2017

Re: eval_pv package

Thread Previous
From:
Father Chrysostomos
Date:
July 6, 2017 20:01
Subject:
Re: eval_pv package
Message ID:
20170706200100.13147.qmail@lists-nntp.develooper.com
David Mertens wrote:
> perlapi mentions that eval_pv can evaluate a block of Perl code. It does
> not indicate the package in which the evaluation occurs. In C::Blocks, I
> use this to allow for generation of C code with a Perl block. It appears
> that eval_pv is run in the "main" package. This is certainly the case when
> used during the operation of a hooked keyword.

I believe, based on reading the code, that it is actually the value
of CopSTASH(PL_curcop), which just happens to be PL_defstash at com-
pile time.

> 
> I would really like for the package to be that in which the keyword is
> actively parsing. It's easy enough to achieve that by prepending the eval'd
> C string with a package statement (and a #line statement, while I'm at it),
> but I wanted to know if there was any more direct way of telling eval_pv
> about the package in which it is being evaluated.

At compile time, PL_curcop points to PL_compiling, a COP-shaped field
in the interpreter struct.  As far as I know, CopSTASH(&PL_compiling)
is not actually used (probably because it is non-trivial to store that
value), but the parser uses PL_curstash instead.

If you are calling eval_pv at compile time (PL_curcop ==
&PL_compiling), then CopSTASH(&PL_compiling) is what you are seeing,
and that is set to PL_defstash at startup (perl.c:init_main_stash) and
never changes, as far as I know.

If you want eval_pv (which assumes run time) to use a different pack-
age, either set CopSTASH(&PL_compiling) yourself via CopSTASH_set
(and set it back), or set PL_curcop to point to your own COP (prob-
ably a static/auto one).  You can see an example of the latter in
op.c:fold_constants in the perl source.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About