General term for cost-based operator OR

Just a quick question.

printf("%d", 99 || 44) prints "1" in C print 99 || 44 prints "99" in perl 

There are two different types of assessment. Does everyone have a name?

edit: I’m interested to know how this Perl score is usually called up in comparison to C. When you say: “For example X, and the perl example is not X, but Y,” which words would you use for X and Y. “short closure "is not what I'm looking for.

+6
c ++ c language-agnostic perl
source share
6 answers

Version C uses || as a logical OR between these two values. Both 44 and 99 evaluate true in C since they are not 0 , so the OR result between them returns 1 (AKA true in C)

In this particular fragment perl || it is a null coalescence operator , a binary code that evaluates the second argument if the first is null, otherwise it evaluates the first argument. Since 99 is the first argument, not null, it is returned and printed.

EDIT: Thanks to Evan for the trash: The || the perl operator is not a zero-coalescing operator; it returns RHS if the LHS evaluates to false, another wise returns LHS. // is the "correct" null-coalescing operator.

Here is a list of perl values ​​that evaluate to false ( from wikipedia)

 $false = 0; # the number zero $false = 0.0; # the number zero as a float $false = 0b0; # the number zero in binary $false = 0x0; # the number zero in hexadecimal $false = '0'; # the string zero $false = ""; # the empty string $false = undef; # the return value from undef $false = 2-3+1 # computes to 0 which is converted to "0" so it is false 
+6
source share

As you noticed, the words you are looking for are not “short”. Short circuit evaluation means that in the expression

 e1 || e2 

if the expression e1 is evaluated as something that represents the truth, then there is no need to evaluate e2 . Both C and Perl use a short circuit rating.

I know the difference you make in two different versions of a short circuit OR, but in twenty years of working in programming languages ​​I have never seen these things be called. The Perl version is quite popular in dynamic languages, for example, Icon, Lua, Scheme.

The Perl version is almost expressed in C:

 e1 ? e1 : e2 

Unfortunately, this version can evaluate e1 twice, depending on the optimizer - and if e1 has side effects or if the compiler cannot determine if there can be side effects, then the compiler must evaluate it twice. This defect can be fixed by associating the value of e1 with a new local variable, but this requires a GNU extension.

C behavior can be emulated in Perl with

 !!(e1 || e2) 
+9
source share

Read here .

Binary || performs a logical 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.

In Perl || and && operators differ from C in that instead of returning 0 or 1, they return the last evaluated value.


printf ("% d", 99 || 44) prints "1" in C

This is because 99||44 returns true (only 99 (which is nonzero) is evaluated due to the short circuit || ), the equivalent of which is 1 , so printf() outputs 1 .

print 99 || 44 prints "99" in perl

.. instead of returning 0 or 1, the last evaluated value is returned ( 99 here).

+8
source share

In Perl 99 || 44 99 || 44 returns 99 because || is a short circuit, and if its first argument is true in a boolean context, it simply returns it. print prints 99.

In C, the result || logical, which is passed to printf , either to 1 or to 0. It also closes, so 44 is not even evaluated.

+3
source share

Both C and Perl refer to their respective operators || as a logical OR (as opposed to a bitwise OR). There is no special name for Perl behavior that returns the last value, not 0 or 1.

+1
source share

In C, || is a boolean operation . In Perl, this is an integer type-agnostic operation that is used to process the first argument as a boolean. This is the only difference.

0
source share

All Articles