我在这个形式的片段:在Perl中,有没有优雅的方式将undef手动转换为0?
my $a = $some_href->{$code}{'A'}; # a number or undef
my $b = $some_href->{$code}{'B'}; # a number or undef
$a = 0 unless defined($a);
$b = 0 unless defined($b);
my $total = $a + $b;
现实更是凌乱的,因为两个以上的变量都在关注。
我真正想写的是:
my $total = $some_href->{$code}{'A'} + $some_href->{$code}{'B'};
,并民主基金正确地评价为0,但我几乎在每次运行得到这些警告:
Use of uninitialized value in addition (+) at Stats.pm line 192.
什么是最好的方法,使这些消息消失了?
注意:如果相关,我'严格使用'和'使用警告'。
它是相关的。在这种情况下,您启用了您不关心的警告。 – jrockway 2009-08-29 15:14:28