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

Re: tri-state flags

Thread Previous | Thread Next
From:
Bart Lateur
Date:
December 6, 2001 17:32
Subject:
Re: tri-state flags
Message ID:
86601usvk3oqrqf02a9f34rpkalfbd4340@4ax.com
On Wed, 5 Dec 2001 04:32:49 -0500, Michael G Schwern wrote:

>At the risk of being prosaic:
>
>sub is_false ($) { defined $_[0] && !$_[0] }

Now there's a boring solution.

The other similar solutions offered (although that whole ~ idea is
nice), were equally uninspired I was hoping for something more creative,
something that I had been missing. They were:

	defined($temp = FLAG) && !$temp

Assuming $temp is a lexical, you can't limit it's scope by doing

	defined(my $temp = FLAG) && !$temp

because it's not the same variable. I hate that. Globals, with local, do
the proper thing:

	defined(local $_ = FLAG) && !$_

but it might clash with other uses of $_ in the same scope.

	do { defined(local $_ = FLAG) && !$_ }

doesn't have the disadvantages, but IMO it doesn't look elegant.

Well, I found a slightly unorthodox solution. Just writing a post like
these can be such a source of inspiration... Whether it is elegant or
not, is up to you to decide.

	grep { defined && !$_ } FLAG

in a scalar context... which will return 0 (failure of the test) or 1.
(Do think about the fact that, as is, FLAG will be evaluated in list
context.)

I think I like it.

-- 
	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