Cool/useful short examples of Perl?

Joel Bernstein joel at fysh.org
Thu Jun 9 15:49:22 BST 2011


On 9 June 2011 16:38, Colin Campbell <colin.campbell at ptfs-europe.com> wrote:
> On 09/06/11 11:52, David Cantrell wrote:
>> On Wed, Jun 08, 2011 at 07:46:52AM +0200, Richard Foley wrote:
>
>>> Even worse is:
>>>      unless ( !something ) { ...
>>>
>>> The brain just into tailspin goes.
>>
>> It's not just Germans' brains that melt.  Double negatives are just
>> confusing.  Normal people hardly ever use them.
>>
> Judging from some of the code I've seen some folks (possibly with a bit
> less programming experience) think its great to pile on the
> unless ( !$something && !$something_else || $yet_something_else )

Sometimes you really do need complex conditions like this.
I favour at minimum naming and commenting them properly, something like:

sub should_permit_frobnication {
  my $self = shift;
  return 1 if ....; # we permit this case
  return 1 if ... and not ....; # special case
  return 0; # not permitted by default
}

(later)
unless ($self->should_permit_frobnication) {
 ... # the condition above should make sense in the semantic of the domain
}

> Simplicity is hard to learn...
...from other people's mistakes...

/joel



More information about the london.pm mailing list