On 17/09/2014 01:37, SSC_perl wrote:

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

As you have presented them, those code fragments are identical in
meaning. The code I used to prove it is below, and it give the output

    { optionprice => "", unitprice => 12.16 }

which is unchanged from the initial settings.

The probable error I can see is that, even if `$item->{optionprice}` is
true, it need not be numeric, and so would raise the `not numeric` warning.

Rob



use strict;
use warnings;
use 5.010;

my $item;

$item->{unitprice}   = '12.16';
$item->{optionprice} = '';

if ($item->{optionprice}) {
  $item->{unitprice} += $item->{optionprice};
}

$item->{unitprice} += $item->{optionprice} if ($item->{optionprice});

use Data::Dump;
dd $item;



---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to