Skip to content

eval_sv() doesn't preserve caller hints/features #21415

Closed
@leonerd

Description

@leonerd

The Perl API function eval_sv() is conceptually the XS version of the Perl eval() function. But it doesn't really match very well, because it doesn't preserve the hints bits or enabled features of its caller; whereas the Perl function does.

eval_sv() isn't normally accessible from Perl but we can abuse XS::APItest in a built source checkout to find a callable copy of it. See the following examples.

The hints bits include the strict flags; these are not preserved:

$ ./perl -Ilib -e 'use strict; eval("\$x + \$y") or die $@'
Global symbol "$x" requires explicit package name (did you forget to declare "my $x"?) at (eval 1) line 1.
Global symbol "$y" requires explicit package name (did you forget to declare "my $y"?) at (eval 1) line 1.

$ ./perl -Ilib -MXS::APItest -e 'use strict; eval_pv("print \$x + \$y, \"DONE\"", 0)'
0DONE

The features bits include the enabled keywords; these are not preserved either:

$ ./perl -Ilib -e 'use feature "say"; eval("say q(HELLO)") or die $@'
HELLO

$ ./perl -Ilib -MXS::APItest -e 'use strict; eval_pv("say q(HELLO)", 0) or die $@'
syntax error at (eval 6) line 1, near "say q(HELLO)"
Execution of (eval 6) aborted due to compilation errors.

By comparison, the warnings bits are preserved, suggesting that at least this general testing method is valid:

$ ./perl -Ilib -MXS::APItest -e 'use warnings; eval_pv("print 123 + undef", 0) or die $@'
Use of uninitialized value in addition (+) at (eval 6) line 1.
123

$ ./perl -Ilib -MXS::APItest -e 'eval_pv("print 123 + undef", 0) or die $@'
123

Ultimately, I don't think this behaviour could be changed by default without breaking a huge amount of existing code. However, I think it would be useful to provide a new flag of some name I don't have a good suggestion for, to ask it to do this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions