Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to avoid "isn't numeric in numeric eq (==)"-warning

#!/usr/bin/env perl
use warnings;
use 5.12.2;

my $c = 'f'; # could be a number too

if ( $c eq 'd' || $c == 9 ) {
    say "Hello, world!";
} 

What is the best way, to avoid the 'Argument "f" isn't numeric in numeric eq (==) at ./perl.pl line 7.'-warning?
I suppose in this case I could use "eq" two times, but that doesn't look good.

like image 941
sid_com Avatar asked Nov 09 '10 08:11

sid_com


1 Answers

use Scalar::Util 'looks_like_number';    

if ( $c eq 'd' || ( looks_like_number($c) && $c == 9 ) ) {
    say "Hello, world!";
} 

You could also disable this category of warnings temporarily:

{
    no warnings 'numeric';
    # your code
}
like image 61
Eugene Yarmash Avatar answered Sep 23 '22 03:09

Eugene Yarmash