"no warnings;" in a Safe compartment
eval, perl, warnings
Solution
If you check `$@` you'll see that `$cft->reval( 'no warnings;' . ') 1' );` failed. `'require' trapped by operation mask at (eval 5) line 1.`. In other words, Safe is doing its job and preventing that code from trying to load a library.
`$cft->reval( 'BEGIN { warnings->unimport; } ) 1' );` would work, presuming warnings is already loaded outside the compartment. However, that won't quiet compile time errors. Unlike `eval`, `reval` seems to let them through. Use amon's technique of quieting STDERR.
Problem
I am using reval from Perl's Safe module and I want to prevent it from generating warnings if the string being eval'ed can't be parsed (actually, I want to prevent it from generating any warnings at all). For example, the following code: ``` use strict; use warnings; use Safe; use feature qw/say/; my $cft = Safe->new; my $x = $cft->reval(') 1' ); my $y = $cft->reval('2' ); say "x: $x"; say "y: $y"; ``` results in: ``` Number found where operator expected at (eval 5) line 1, near ") 1" (Missing operator before 1?) Use of uninitialized value $x in concatenation (.) or string at ./test line 12. x: y: 2 ``` What I'm trying to achieve is to have $x = undef and $y = 2, and no warnings. I tried to put a "no warnings;" inside a new scope, but it has no effect on the warnings produced from within the reval (although, as pointed out by @DavidO, it silences the 'uninitialized value' warning): ``` use strict; use warnings; use Safe; use feature qw/say/; my $cft = Safe->new; { no warnings; my $x = $cft->reval(') 1' ); my $y = $cft->reval('2' ); say "x: $x"; say "y: $y"; } ``` I guess that somehow the 'no warnings' has to be inside the Safe compartment, so I also tried to prepend "no warnings;" to the strings being eval'ed: ``` use strict; use warnings; use Safe; use feature qw/say/; my $cft = Safe->new; { my $x = $cft->reval( 'no warnings;' . ') 1' ); my $y = $cft->reval( 'no warnings;' . '2' ); say "x: $x"; say "y: $y"; } ``` This way reval does not issue any warnings, but both variables are undef: ``` Use of uninitialized value $x in concatenation (.) or string at ./test line 10. x: Use of uninitialized value $y in concatenation (.) or string at ./test line 11. y: ``` I don't know what else to try, and I hope that the problem description was clear enough.