develooper Front page | perl.fwp | Postings from December 2001

Re: tri-state flags

Thread Previous | Thread Next
From:
Bart Lateur
Date:
December 6, 2001 04:46
Subject:
Re: tri-state flags
Message ID:
cmpu0u0j36adi22r4tuit71li37lu9vst3@4ax.com
On 5 Dec 2001 16:25:16 -0000, Lars Henrik Mathiesen wrote:

>bernie@fantasyfarm.com ("Bernie Cosell") writes:
>>On 5 Dec 2001, at 11:34, abigail@foad.org 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.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About