Perl Eval Warnings

I need to hide warnings inside eval, but the rest of the code should continue to give warning messages. Here is what I have -

eval "\$value = $hash->{key}"; 

now the value of $ hash → {key} can be a function call, for example:

 $hash->{key} = "function(0.01*$another_var)"; 

The problem occurs when $another_var is undef (or "" ). The script just fights with the following message -

The argument "" is not numeric by multiplying (*) by (eval 1381) line 1.

Any suggestions how can I avoid this? One of the options that I was thinking about was to parse the value inside the brackets and evaluate it first, but its quite difficult with the data I'm dealing with.

+4
source share
3 answers

Wrap your code in a no warnings block.

 ... { no warnings; eval "\$value = $hash->{key}"; } ... 

You can also disable certain alert classes. See perllexwarn for a hierarchy of alert categories and perldiag for the category to which a particular alert belongs.

 { no warnings qw(uninitialized numeric); eval "\$value = $hash->{key}"; } 

(blah blah blah standard disclaimer that anyone who turns off alerts cannot get 25 feet from the blah blah add-on machine)

+9
source

Are you sure you don't want to do something like:

 my $href; my $somevar = 8; $href->{foo} = sub { $somevar * 4 }; my $var = $href->{foo}->(); 

If you are not sure whether $ href → {foo} is a scalar, ref code, etc., you can check it using the ref () function, or better, using Scalar :: Util :: reftype ().

+3
source

Change the hash key "function( 0.01 * ($another_var // 0) )"

$another_var // 0 equivalently defined($another_var) ? $another_var : 0 defined($another_var) ? $another_var : 0 .

0
source

Source: https://habr.com/ru/post/1337052/


All Articles