Cool/useful short examples of Perl?

Paul Makepeace paulm at paulm.com
Wed Jun 8 14:33:07 BST 2011


On Wed, Jun 8, 2011 at 14:15, Kaoru <kaoru at slackwise.net> wrote:
> 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?

Yes, you're right - perl doesn't use == semantics for that.


$ perl -e 'for ("a", "", -1, 0, 1, 2, undef) {
print "$_: ";
if ($_) {
  unless (!$_) { print "ok" } else { print "not ok" }
} else {
  unless (!$_) { print "not ok" } else { print "ok" }
}
print "\n" }'
a: ok
: ok
-1: ok
0: ok
1: ok
2: ok
: ok

A legitimate use of unless + else! :-)

P



More information about the london.pm mailing list