[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Another || quickie



The last version below doesn't completely satisfy his original logic...  Actually, maybe it does; because looking at it more closely, the original logic and the derived versions all have issues.  Using the shorthand variable $t->Value, it is being numerically compared to an empty string, which can be a problem.  An actual zero in $t->Value will fail to pass the original test because numerically the empty string is also zero; in fact all strings that do not start with a number will fail the 2nd if statement in the original version. Only strings that evaluate to a non-zero value will get past that 2nd if statement.

So, if $t->Value is supposed to have a string, we need to use the string comparison operations, eq and ne, for equal and not-equal respectively. Otherwise we should replace the "" with a zero.

Without further context I can't tell if this would actually cause problems in the logic of this snippet of code.

If we're attempting to test if $oWkS->{Cells}[$iR][0] is defined, which it looks like we are, then to make it easier to read later, we should be using a defined() test.  We shouldn't be relying on the fact that if it is a valid pointer, the memory address residing in $t is not going to be zero.

Another subtle issue that could come up is that if $oWkS->{Cells} didn't even exist before this snippet, it will as soon as the if statement is hit. This nice little "feechur" is called auto-vivification.  Read up on it if you haven't heard about it before.

-Scott

-----Original Message-----
From: omaha-pm-bounces@pm.org [mailto:omaha-pm-bounces@pm.org] On Behalf Of Jay Hannah
Sent: Monday, August 22, 2005 9:55 PM
To: Perl Mongers of Omaha, Nebraska USA
Subject: Re: [Omaha.pm] Another || quickie


On Aug 19, 2005, at 12:57 PM, Kenneth Thompson wrote:
> So if I understand this correctly:
>  
> This:
>  
>     if ($oWkS->{Cells}[$iR][0]) {
>       if ($oWkS->{Cells}[$iR][0]->Value != "") {
>         $myVar = ($oWkS->{Cells}[$iR][0]->Value)
>       }
>     }
>  
> Is the same as this:
>  
>     if (!$oWkS->{Cells}[$iR][0]) {}
>     elsif {$oWkS->{Cells}[$iR][0]->Value == "") {}
>     else {
>        $myVar = ($oWkS->{Cells}[$iR][0]->Value);
>     }

IMHO the 2nd is less readable.

> Which is shortcut(ed?) as this? :
>  
>     My $t = (!$oWkS->{Cells}[$iR][0];
>     $myVar = ((!$t) || ($t->Value != "") || $t->Value);

I think your unmatched ( and "My" will syntax error. Perhaps this?:

    my $t = $oWkS->{Cells}[$iR][0];
    $myVar = $t->Value if ($t && $t->Value);

?

j

_______________________________________________
Omaha-pm mailing list
Omaha-pm@pm.org
http://mail.pm.org/mailman/listinfo/omaha-pm