Re: Argument isn't numeric warning in if statement
On 18/09/2014 00:34, SSC_perl wrote: On Sep 17, 2014, at 3:32 PM, Rob Dixon wrote: As you have presented them, those code fragments are identical in meaning. That was my understanding as well, but the inline 'if' gave an error while the block didn't. Running the code by itself in TextWrangler does not produce the warning. However, inside of my script, it does. Strange. This probably doesn't have anything to do with it, but I'm logging during tests with this code: BEGIN { use CGI::Carp qw(carpout); open(my $log, '>>', 'temp_logs/error.log') or warn("Unable to open error.log: $! \n"); carpout($log); } You're mistaken. There's something else at play here that you've overlooked. if ($item->{optionprice}) { $item->{unitprice} += $item->{optionprice}; } is *identical* to $item->{unitprice} += $item->{optionprice} if $item->{optionprice} and is also the same as $item->{optionprice} and $item->{unitprice} += $item->{optionprice} $item->{unitprice} += $item->{optionprice} if ($item->{optionprice}); Just so I'm clear on this, am I correct in thinking that Perl evaluates an inline 'if' from right to left - meaning that if $item->{optionprice} is NOT true, then the addition will not even be seen? Or does Perl look at the entire line before performing it? I'm not clear what you mean by an *inline* if statement. If you mean the post-fixed if, as in $item->{unitprice} += $item->{optionprice} if $item->{optionprice} then yes, the statement to the left of `if` won't be evaluated unless the expression after it is true. Rob --- 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/
Re: Argument isn't numeric warning in if statement
On Sep 17, 2014, at 3:32 PM, Rob Dixon wrote: > As you have presented them, those code fragments are identical in meaning. That was my understanding as well, but the inline 'if' gave an error while the block didn't. Running the code by itself in TextWrangler does not produce the warning. However, inside of my script, it does. Strange. This probably doesn't have anything to do with it, but I'm logging during tests with this code: BEGIN { use CGI::Carp qw(carpout); open(my $log, '>>', 'temp_logs/error.log') or warn("Unable to open error.log: $! \n"); carpout($log); } > $item->{unitprice} += $item->{optionprice} if ($item->{optionprice}); Just so I'm clear on this, am I correct in thinking that Perl evaluates an inline 'if' from right to left - meaning that if $item->{optionprice} is NOT true, then the addition will not even be seen? Or does Perl look at the entire line before performing it? Thanks, Frank SurfShop shopping cart is now open source... Follow us on GitHub! https://github.com/surfshopcart/surfshop -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Argument isn't numeric warning in if statement
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/
Re: Argument isn't numeric warning in if statement
On 09/17/2014 12:46 PM, SSC_perl wrote: > On Sep 16, 2014, at 6:58 PM, > wrote: >> Are you sure you've quoted the code (that's producing the warning) correctly? > > Yes, I did. I double-checked it just to be certain. However, I ran > the code by itself and it doesn't produce that warning, so it must be > something upstream that's causing it. > > Thanks, > Frank The code if (' ') { ... } will execute the block. The code if ('') { ...} will not Neither ' ' nor '' are numeric, so using them in an arithmetic expression will generate a warning. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Argument isn't numeric warning in if statement
On Sep 16, 2014, at 6:58 PM, wrote: > Are you sure you've quoted the code (that's producing the warning) correctly? Yes, I did. I double-checked it just to be certain. However, I ran the code by itself and it doesn't produce that warning, so it must be something upstream that's causing it. Thanks, Frank SurfShop shopping cart is now open source... Follow us on GitHub! https://github.com/surfshopcart/surfshop -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Argument isn't numeric warning in if statement
-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/
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 Thanks, Frank SurfShop shopping cart is now open source... http://www.surfshopcart.com/ Setting up shop has never been easier! Follow us on GitHub! https://github.com/surfshopcart/surfshop -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/