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/


Reply via email to