Cool/useful short examples of Perl?
Matt Lawrence
matt.lawrence at virgin.net
Wed Jun 8 14:37:10 BST 2011
On 08/06/11 13:41, Paul Makepeace wrote:
> On Wed, Jun 8, 2011 at 13:17, Tom Hukins<tom at eborcom.com> wrote:
>> On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:
>>> I'd rather go for sacking people that don't know the difference
>>> between
>>>
>>> if (something) { ... }
>>>
>>> and
>>>
>>> unless (!something) { ... }
>> It's sunny outside and pubs are open: I can think of worse times to
>> lose my job.
>>
>>> Or does everyone think they are always equivalent?
>> I'm not everyone, and with a language as flexible as Perl I hesitate
>> to make strong statements involving words like "always", but I don't
>> recall encountering a situation where they differ.
> $ perl -le 'print "$_ == !!$_ ? ", $_ == !!$_ ? "yes" : "no" for (-1,
> 0, 1, 2, undef)'
> -1 == !!-1 ? no
> 0 == !!0 ? yes
> 1 == !!1 ? yes
> 2 == !!2 ? no
> == !! ? yes
>
But:
$ perl -le 'print 0 eq !!0 ? "yes" : "no"'
no
This still wouldn't affect the behaviour of the original example. In the
context of an if or unless statement, the only thing that matters is the
boolean value of the expression, not exactly which true or false value
it is. Perl's canonical true and false are 1 and '' respectively, but
everything has a boolean value.
Matt
More information about the london.pm
mailing list