On 5 Dec 2001 16:25:16 -0000, Lars Henrik Mathiesen wrote:
>[EMAIL PROTECTED] ("Bernie Cosell") writes:
>>On 5 Dec 2001, at 11:34, [EMAIL PROTECTED] wrote:
>>> But Bart said he wanted to test for 0. The test above, and several of
>>> the other proposals don't distinguish between 0 and the empty string.
>
>Here's a trick: ~(~$flag||0) will map the false values undef and ''
>into 4294967295 (as well as the string "�"). It will pass both numeric
>0 and "0" through, however.
>
>>Well, what I thought he actually said was that the flag had one of
>>the values 1,0,undef.
>
>In that case, ! ~(~$flag||0) is what he needs.
I've played alittle with that ~ operator. It cerainly is an interesting
approach.
It relies on the different behaviour of bitwise operators for numerical
and string operands. It treats undef as "", as a string.
Also intersting to note is its behaviour WRT perl booleans. As you know,
(or should know ;-)), is that these have a dual nature: a boolean false
is 0 in numerical context, and "" in string context. Well, apparently ~
thinks of it as a number.
my $f = "" =~ /X/;
print ~$f;
-->
4294967295
Same result if you test with (1 == 2), for example.
--
Bart.