Cool/useful short examples of Perl?

David Matthewman david at matthewman.org
Wed Jun 8 14:00:21 BST 2011


On 8 Jun 2011, at 13:17, Tom Hukins 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.

Their side effects are different. There may be a simpler way to demonstrate this, but for instance:

#! /usr/bin/env perl

print "unless: ";
print use_unless() . "\n";
print "if: ";
print use_if() . "\n";

sub use_unless {
  my $value = 1;
  unless ($value) {
    # nop  
  }
}

sub use_if {
  my $value = 1;
  if (!$value) {
    # nop
  }    
}

-- 
David Matthewman


More information about the london.pm mailing list