views:

279

answers:

6

Just a quick question

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

There are two different kinds of evaluation. Does each one have a name?

edit: i'm interested to know how this Perl evaluation is commonly called when compared to C. When you say "C example is X, and perl example is not X, but Y" which words would you use for X and Y. "short circuit" is not what i'm looking for.

+3  A: 

In Perl, 99 || 44 returns 99, because || is "short circuiting" and if its first argument is true in boolean context, it just returns it. print prints 99.

In C the result of || is logical, which passed to printf results either in 1 or 0. It's also short-circuiting, so 44 isn't even evaluated.

Eli Bendersky
C's || is short-circuiting as well. It just evaluates to 1 or zero.
Roger Lipscombe
@Roger: sure, thanks. edited it in to make more it clear
Eli Bendersky
+11  A: 

Read here.

Binary || performs a short-circuit logical OR operation. That is, if the left operand is true, the right operand is not even evaluated. Scalar or list context propagates down to the right operand if it is evaluated.

In Perl the || and && operators differ from C's in that, rather than returning 0 or 1, they return the last value evaluated.


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

That is because 99||44 returns true(only 99(which is non-zero) is evaluated due to the short-circuiting action of || ) whose equivalent is 1 hence printf() prints 1.

print 99 || 44 prints "99" in perl

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

Prasoon Saurav
thanks, but this doesn't answer my question. "short-circuit" applies to both C and Perl versions.
stereofrog
@stereofrog: Your question was _is there a **common term** for these two kinds of evaluation?_ .My answer is Short-Circuit evaluation. Doesn't it answer your question? BTW whoever downvoted, please explain the reason?
Prasoon Saurav
stereofrog: Look again, he answered, the difference is an implementation detail. C returns 1 for "true", while perl returns the last non-zero value it evaluates for "true".
John Knoeller
+6  A: 

The C version uses || as the logical OR between the two values. Both 44 and 99 evaluate to true in C as they are not 0, so the result of an OR between them returns 1 (AKA true in C)

In that particular perl snippet, || is the null-coalescing operator, an binary which evaluates to the second argument if the first is null, otherwise evaluating to the first argument. Since 99 is the first argument and not null, it gets returned and printed.

EDIT: Thanks Evan for the clafication: The || operator in perl is not the null-coalescing operator, it returns the RHS if the LHS evaluates to false, other wise returning the LHS. // is the "proper" null-coalescing operator.

Here's the list of values in perl 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
CrazyJugglerDrummer
re: *In that particular perl snippet, || is the null-coalescing operator, an binary which evaluates to the second argument if the first is null* -- It most certainly is **NOT** null-coalescing. Yes, `undef || die` will die, but so will `0 || die`. `||` evaluates the LHS and if it returns false it evaluates the RHS. undef just so happens to evaluate to false (thank god). Conversely, `//` is the null-coalescing operator introduced in 5.10 which *will* only trigger the RHS if the LHS is null, prior to that you could do `(defined $a) || die`
Evan Carroll
*The || operator in perl is not the null-coalescing operator, it returns the LHS if the first evaluates to false, other wise returning the first argument.* Actually, like all statements in Perl it returns its last evaluated expression which is always the last argument with `||`. Check out this `perl -e'print 0||"0"||undef'` to see.
Evan Carroll
+1  A: 

Both C and Perl refer to their respective || operators as a "logical OR" (as distinct from a bit-wise OR). There's no special name for Perl's behavior of returning the last value as opposed to 0 or 1.

Michael Carman
+9  A: 
Norman Ramsey
A: 

In C, || is a Boolean operation. In Perl it is an integer type-agnostic operation which happens to treat the first argument as a Boolean value. That is the only distinction.

Potatoswatter
Sure that it is an integer operation in Perl? What about `$s = "hello" || ""` ?
frunsi
@frunsi: Lol, I don't know Perl's type terminology.
Potatoswatter