Cool/useful short examples of Perl?

Graeme Hewson ghewson at wormhole.me.uk
Thu Jun 9 18:13:48 BST 2011


On Thursday 09 June 2011 15:49:22 Joel Bernstein wrote:

> 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
> }

With more than two or three decisions (theoretically four or eight outcomes) I 
would write a decision table into comments around the code. This allows you 
to reason about (and show you've reasoned about) the "impossible" conditions. 
So it turns into a ternary decision: true, false or die.


More information about the london.pm mailing list