"no warnings"; in the safe compartment

I am using reval from Perl Safe , and I want it to not generate warnings if the string that eval'ed cannot be parsed (in fact, I want it to not generate 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";

leads to:

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 "no warnings"; in a new area, but it does not affect the warnings generated during the revaluation (although, as @DavidO points out, he ignores the warning "uninitialized value"):

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";
}

, " " Safe, " " ; :

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";
}

, reval , 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:

, , , .

+5
2

$@, , $cft->reval( 'no warnings;' . ') 1' ); . 'require' trapped by operation mask at (eval 5) line 1.. , Safe .

$cft->reval( 'BEGIN { warnings->unimport; } ) 1' ); , , . , . eval, reval, , . amon STDERR.

+4

no warnings , pragma use warnings. , strict. .

- , , , - STDERR, :

{
  # I know what I'm doing!
  local $SIG{__WARN__} = sub {}; # locally ignore any warnings
  eval $code; # catches all "die"
}

STDERR /dev/null:

{
  # I know what I'm doing!
  open my $oldSTDERR, '>&' \*STDERR or die;
  close STDERR or die;
  open STDERR, '>', '/dev/null' or die;

  eval $code;

  close STDERR or die;
  open STDERR, '>&', $oldSTDERR or die;
  close $oldSTDERR;
}
+4

All Articles