Wednesday, July 25, 2007

In the array and hash naming convention guideline in Perl Best Practices, they have this lovely example of when an array name should be singular (answer: when used as a random access table like a hash):

sub factorial {
my ($n) = @_;

croak "Can't compute factorial($n)"
if $n < 0 || $n > $MAX_FACT;

return $factorial[$n];
}
Factorials not pre-calculated for the cache are simply declared incomputable. Talk about lazy programming!

(Code snippet from:
Perl Best Practices, by Damian Conway, Copyright 2005 O'Reilly Media, Inc., ISBN 0-596-00173-8
Good book.)

No comments: