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

Re: tri-state flags

Thread Previous | Thread Next
From:
Sven Neuhaus
Date:
December 5, 2001 01:46
Subject:
Re: tri-state flags
Message ID:
20011205104555.B15635@neopoly.com
On Wed, Dec 05, 2001 at 10:43:14AM +0100, Sven Neuhaus wrote:
> On Wed, Dec 05, 2001 at 04:32:49AM -0500, Michael G Schwern wrote:
> > On Wed, Dec 05, 2001 at 03:30:07AM +0100, Bart Lateur wrote:
> > > I often use tri-state flags, with possible value true (1), false (0), or
> > > undetermined (undef).
> > > 
> > > The question is: how do you test for one of the flags, in particular for
> > > 0, without warnings?
> > > 
> > > For true, it's easy:
> > > 
> > > 	$flag
> > > 
> > > For undef, it's not hard:
> > > 
> > > 	!defined $flag
> > > 
> > > But for 0?
> > 
> > At the risk of being prosaic:
> > 
> > sub is_false ($) { defined $_[0] && !$_[0] }
> 
> Looks like "~" doesn't trigger a warning on undef.
> 
> So
> ~$a ? $a ? 3 : 2 : 1
> 
> will yield 3 for true, 2 for false, 1 for undef.

So, to answer your question,

~$a && !$a
will only be true for false.

-Sven
-- 
La-dee-dee, la-dee-dah.

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