There are two things at work here. First, the false value of perl is actually neither strictly '' nor 0, it's kinda both (link to the doc when I find it). As demonstrated by
perl -E "use strict; use warnings; say( '' + 1 ); say( !1 + 1 ); say !
+1; say '!1 is defined' if defined !1"
Argument "" isn't numeric in addition (+) at -e line 1.
1
1
!1 is defined
The warning about the non numeric argument happens only once, because !1 is treated as a number in arithmetic context. But printing !1 doesn't show anything because false is the empty string in string context.
~ however only works in two ways: bitwise not on a number, or bitwise not on a binary string. And the output will be of the same type. This means that ~~ is only equivalent to scalar when the operand is a number or a string, but it will force another type (probably string) on any other value (an object, a reference...).
Edit: one place where the special value of perl's false is mentioned: Relational Operators.
Also, ~~ is not equivalent to scalar for some big numbers and negative numbers:
DB<1> say ~~ (-1)
18446744073709551615
So it's better not to use it outside of obfuscated code. |