避免“数字 eq (==) 中不是数字”警告的最佳方法
#!/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!";
}
避免出现“Argument "f" isn't numeric in numeric eq (==) at ./perl.pl line 7.”警告的最佳方法是什么?
我想在这种情况下我可以使用“eq”两次,但这看起来不太好。
#!/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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用正则表达式来查看它是否是一个数字:
Using a regular expression to see if that is a number:
您还可以暂时禁用此类警告:
You could also disable this category of warnings temporarily:
不知道为什么你想避免警告。该警告告诉您程序中存在潜在问题。
如果您要将数字与包含未知数据的字符串进行比较,那么您要么必须使用“eq”进行比较,要么以某种方式清理数据,以便您知道它看起来像一个数字。
Not sure why you want to avoid the warning. The warning is telling you that there's a potential problem in your program.
If you're going to compare a number with a string that contains unknown data, then you're either going to have to use 'eq' for the comparison or clean up the data in some way so that you know it looks like a number.
避免出现有关将非数字与数字进行比较的警告的明显方法是不这样做!警告是为了您的利益而存在的 - 它们不应被忽视或解决。
要回答什么是最佳方式,您需要提供更多上下文 - 即
$c
代表什么,以及为什么有必要比较它'd'
或9
(为什么不使用$c eq '9'
)?The obvious way to avoid a warning about comparing a non-numeric to a numeric is not to do it! Warnings are there for your benefit - they should not be ignored, or worked around.
To answer what is the best way you need to provide more context - i.e. what does
$c
represent, and why is it necessary to compare it do'd'
or9
(and why not use$c eq '9'
)?