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

Re: tri-state flags

Thread Previous | Thread Next
Bart Lateur
December 6, 2001 04:46
Re: tri-state flags
Message ID:
On 5 Dec 2001 16:25:16 -0000, Lars Henrik Mathiesen wrote:

> ("Bernie Cosell") writes:
>>On 5 Dec 2001, at 11:34, 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

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;

Same result if you test with (1 == 2), for example.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About