Page 84      All Pages  All Books
9.4 An Example
Rather than discuss style in the abstract, I’d like to demonstrate my opinion of good style through a comparative example. Here’s an excerpt from the popular Test package on CPAN: 4
package Test; use strict;
sub plan {
croak "Test::plan(%args): odd number of arguments" if @_ & 1;
croak "Test::plan(): should not be called more than once" if $planned;
local($\, $,); # guard against -l and other things that screw with # print
_reset_globals();
_read_program( (caller)[1] );
my $max=0;
for (my $x=0; $x < @_; $x+=2) {
my ($k,$v) = @_[$x,$x+1];
if ($k =~ /^test(s)?$/) { $max = $v; }
elsif ($k eq ’todo’ or
$k eq ’failok’) { for (@$v) { $todo{$_}=1; }; }
elsif ($k eq ’onfail’) {
ref $v eq ’CODE’ or croak "Test::plan(onfail => $v): must be CODE"; $ONFAIL = $v;
}
else { carp "Test::plan(): skipping unrecognized directive ’$k’" } }
my @todo = sort { $a <=> $b } keys %todo; if (@todo) {
print $TESTOUT "1..$max todo ".join(’ ’, @todo).";\n"; } else {
print $TESTOUT "1..$max\n"; }
http://search.cpan.org/src/SBURKE/Test-l.22/lib/Test.pm
Copyright © 2004 Robert Nagler                                                             70
All rights reserved nagler@extremeperl.org

Page 84      All Pages  All Books