In Perl, like || operator work?

So here

$y = 0 | 2 | 4; # answer is 6 $x = 0 || 2 || 4; # answer is 2 

I know why $y is 6 because it uses the OR operator for each number and 2 | 4 = 6 2 | 4 = 6 , but for $x ...

Why is it 2 ?

+6
source share
4 answers

Because 2 is the first non-fake element and logical OR short circuits. It evaluates to zero, which is false, then 2, which is not false, so it executes and returns 2. Consider the output of the following example:

 $val = 1; sub a_proc { print "a_proc: ", $val++, "\n"; 1; } $another_val = &a_proc || &a_proc; 

This will exit a_proc: 1 . As soon as a_proc returns the true value, the interpreter may stop evaluating, since the logical OR from true and any value of true .

+13
source

Its short circuit preserving the value of logical or. Basically, he evaluates each of the things between || in turn, until he finds one that is not false, after which he returns this and evaluates the subsequent operands.

change

There are two important functions of perl logical ops

  • they are short in that they evaluate the left operand first, and if it is true (false for && ), DO NOT evaluate the right operand

  • they retain the value because they convert the operand to a boolean value of true or false for the conjunction (and to determine whether the right operand should be evaluated), but the result of the expression is the original value before converting to boolean

Both of these functions are very important, and they combine to make || especially useful in perl - much more than in C / C ++, where it simply shorts and stores the value of NOT.

+8
source

As long as the operators look the same, their purpose is actually different.

From perldoc perlop :

Binary "||" performs a short circuit logical operation OR. That is, if the left operand is right, the right operand is not even evaluated.

Compared with:

Binary "|" returns its operands ORed little by little.

Purpose || is to come up with an answer to "is x || y true or false?" while the goal is bitwise or - | - come up with a product (?) from operands or something like "what is the result of x | y ?"

Since the only interesting two results from || are true or false, the operator can (and is) short-term, thereby causing this effect.

In the first statement: (0 | 2) = 2, (2 | 4) = 6

In the second statement: (0 || 2) = 2, (2 || ...) = 2

Interestingly, bitwise or sets logical values ​​inside a binary number. Adding a true or false value to a position in the binary representation of a number.

 0000 | 0010 = 0010 0010 | 0100 = 0110 0110 | 0001 = 0111 0111 | 0001 = 0111 # no change 

This is very convenient for storing several logical values ​​in one number, which can be checked with & ( bitwise AND ).

 0101 & 0100 = 0100 (true) 0101 & 0010 = 0000 (false) 

There are 10 kinds of people: those who understand binary numbers, and those who do not.

+5
source
 EXPR_A || EXPR_B 

more or less equivalent

 do { my $rv = EXPR_A; if ($rv) { $rv } else { EXPR_B } } 

Or in English

  • First, it computes EXPR_A in a scalar context.
  • If the value is true, this value is returned.
  • If false, EXPR_B evaluates and returns.

Sometimes you will see them chained.

 EXPR_A || EXPR_B || EXPR_C || EXPR_D 

simply

 ( ( EXPR_A || EXPR_B ) || EXPR_C ) || EXPR_D 

so just apply the above recursively.

As a result, you will get the first expression that returns true, or the result of the last if none of them is true.

+2
source

Source: https://habr.com/ru/post/888488/


All Articles