What is || = in perl for?

Looking at the documentation for Reading Excel , there is a statement that I really don't understand.

Can someone explain what $sheet -> {MaxRow} ||= $sheet -> {MinRow}; ?

I get this assignment operator. However, I do not understand how || must work?

+7
operators perl
source share
2 answers
 $sheet -> {MaxRow} ||= $sheet -> {MinRow}; 

equivalent to:

 $sheet -> {MaxRow} = ( $sheet -> {MaxRow} || $sheet -> {MinRow}); 

The local OR operator evaluates the second operand only if the first is false. So, MaxRow will get the MinRow value if MaxRow is 0 (possibly the first iteration).

+12
source share

The ||= operator is a logical-OR assignment. It is like += , which is add-assign. It calculates the logical OR of LHS and RHS, and then assigns the result to LHS, so it must be a valid l value.

In other words, just like

 $a += 3; 

equivalently

 $a = $a+3; 

we can say that

 $a ||= 1; 

equivalently

 $a = $a||1; 

Now, regarding the statement that you mentioned in your question, there is a bit more going on there than in my example above. In your application, LHS is not just a simple variable token, but a variable token that is processed as a hash reference ( $sheet ) and dereferenced to get the value that is entered using the 'MaxRow' string. RHS is also a hash dereferencing operation on $sheet , but whose key is 'MinRow' . But the behavior is the same; we can say that

 $sheet->{'MaxRow'} ||= $sheet->{'MinRow'}; 

equivalently

 $sheet->{'MaxRow'} = $sheet->{'MaxRow'}||$sheet->{'MinRow'}; 

(Note. I always like to explicitly specify the values โ€‹โ€‹of the hash keys as strings, because this is what they are, but not everyone goes to this degree of clarity.)

For more information on the logical OR operation, see http://en.wikipedia.org/wiki/Logical_disjunction , and for Perl-specific information, see http://perldoc.perl.org/perlop.html#C-style- Logical-Or (for || and // ) and http://perldoc.perl.org/perlop.html#Logical-or-and-Exclusive-Or (for or ). The most relevant quote from the Perl documentation on || :

Binary "||" Performs a logical OR short circuit operation. That is, if the left operand is true, the right operand is not even evaluated. A scalar or list context extends to the right operand, if evaluated.

This does not quite explain this; in case the LHS evaluates a plausible value (see below for a definition), then the return value of the operation || is the LHS value, otherwise it is the RHS value.

In Perl, boolean values โ€‹โ€‹are usually represented by 0 (or sometimes '' or undef ) for false and 1 for true. However, more specifically, any value that is not one of the three false values โ€‹โ€‹listed above is treated as true, and sometimes programmers refer to this difference using the unofficial terms of โ€œtruthfulnessโ€ and โ€œfalseโ€. IOW, 0 , '' and undef are false, and everything else is true. See http://www.perlmonks.org/?node=what%20is%20true%20and%20false%20in%20Perl%3F for details.

+6
source share

All Articles