On 11/02/2011 10:38, mailing lists wrote:
12 $sheet -> {MaxRow} ||= $sheet -> {MinRow};
Line 12 can be written as:
$sheet->{'MaxRow'} = $sheet->{'MaxRow'} || $sheet->{'MinRow'};
then that I don't understand is the program logic :-(
what's the purpose of lines 12 and 14??
Perl has no proper boolean values. Instead, the boolean operators treat
zero, undef, and the null string '' all as false. Anything else is true.
So
'' || 'xx'
evaluates to true, because although the left operand is false, the right
one is true and so the 'or' of the two is true.
The reason code like
$x = $x || $y
or the equivalent
$x ||= $y
are useful is because of the way Perl evaluates the expression. It is
called short-circuit evaluation, and is described here
<http://www.perlmonks.org/?node_id=301355>
and
<http://en.wikipedia.org/wiki/Short-circuit_evaluation>
Essentially,
$x ||= $y
is equivalent to
if ($x) {
$x = $x;
}
else {
$x = $y;
}
and, clearly, assigning $x to itself has no effect, so we have
if (not $x) {
$x = $y;
}
or
$x = $y if not $x;
So line 12 of your code can be replaced with
$sheet->{MaxRow} = $sheet->{MinRow} if not $sheet->{MaxRow};
which you may prefer, and I would find it hard not to agree with you.
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/