-----Original Message-----
From: SSC_perl
Sent: Wednesday, September 17, 2014 10:37 AM
To: Perl Beginners
Subject: Argument isn't numeric warning in if statement
I just ran across something puzzling. Why are these two statements not
equivalent when it comes to warnings?
if ($item->{'optionprice'}) {
$item->{'unitprice'} += $item->{'optionprice'};
}
and
$item->{'unitprice'} += $item->{'optionprice'} if
($item->{'optionprice'});
Given the following values:
$item->{'unitprice'} = '12.16';
$item->{'optionprice'} = '';
the 2nd statement returns an "Argument '' isn't numeric in addition (+)"
warning, while the 1st one doesn't. I thought I read where Peal reads a
statement like the 2nd one from right to left. It looks like it doesn't,
since $item->{'optionprice'} is evaluated in spite of the 'if'. Am I
mistaken?
Perl 5.10.1
Can't reproduce the anomaly here on 5.10.0 and 5.12.0 - I don't have 5.10.1.
##############################
C:\_32\pscrpt>type try.pl
use warnings;
$item->{'unitprice'} = '12.16';
$item->{'optionprice'} = '';
$item->{'unitprice'} += $item->{'optionprice'} if ($item->{'optionprice'});
C:\_32\pscrpt>perl try.pl
C:\_32\pscrpt>
##############################
Are you sure you've quoted the code (that's producing the warning) correctly
?
Cheers,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/