Cool/useful short examples of Perl?
Kaoru
kaoru at slackwise.net
Wed Jun 8 14:15:06 BST 2011
On Wed, Jun 8, 2011 at 1:41 PM, Paul Makepeace <paulm at paulm.com> wrote:
> $ perl -le 'print "$_ == !!$_ ? ", $_ == !!$_ ? "yes" : "no" for (-1,
> 0, 1, 2, undef)'
> -1 == !!-1 ? no
> 0 == !!0 ? yes
> 1 == !!1 ? yes
> 2 == !!2 ? no
> == !! ? yes
Whether they are equal according to "==" shouldn't matter here, should
it? What matters between if(something) and unless (!something) is
whether something and !!something are the same boolean-wise.
I think this code shows they are all the same?
$ perl -le 'print "$_\tis the same booleanwise as\t!!$_\t", !($_ xor
!!$_) ? "yes" : "no" for (-1, 0, 1, 2, undef)'
-1 is the same booleanwise as !!-1 yes
0 is the same booleanwise as !!0 yes
1 is the same booleanwise as !!1 yes
2 is the same booleanwise as !!2 yes
is the same booleanwise as !! yes
-----
As has been pointed out "!" can be overridden etc, but I think the use
of unless is safer than your example suggested.
- Alex
More information about the london.pm
mailing list