These code guidelines will be added to as time goes on, or (more likely) forgotten and abandoned.
TO-DO: Fix the following stupid stylesheet issue
($script) = $ENV{'SCRIPT_FILENAME'}=~!m([^/.]*)(\.cgi)?$!; $atype, $ascr, $aact, $aid, $aser, $agrp, $reason map { } whenever possible--it helps raise the bar. After all, you don't want just anyone mucking around in your code. See if you can beat this one [please don't try to beat this map clause]: my @pseries = map {
my $reltime = $_->{reltime};
delete $_->{reltime};
my $dspec = PDB::MakeDSpec(%{$_});
my $v;
if ($nninterp eq 'slwin') {
$v = where($inputps{$dspec}->{v},
$inputps{$dspec}->{gmt} >= $gmtmin-$offsetsec+$reltime*3600 &
$inputps{$dspec}->{gmt} < $gmt1-$offsetsec+$reltime*3600 &
$inputps{$dspec}->{gmt} < $gmtnow+$reltime*3600);
}
else {
$v = where($inputps{$dspec}->{v},$inputps{$dspec}->{gmt} == $gmt0+$reltime*3600);
}
$v;
} @{$model->{inputs}};
</sarcasm>
use strict and use warnings if not present [and fix the code so it doesn't generate warnings]
use vars declarations (e.g., %PDB::ARGS instead of %ARGS).
use vars declarations with (a) $package::variable assignment(s).
use strict and use warnings. Then write your code so it doesn't generate any errors or warnings.
open() calls.
open() for more information.
Don't do this:
return map { "$_->[0]: <a href=$PDB::LOCATION/$_->[0]/$dates/$class>$_->[1]{'name'}</a>" }
sort { for (@sortbys) { my $c = $a->[1]{$_} cmp $b->[1]{$_}; return $c if $c} }
map {
my %stna = PDB::StnAttr($NOW,$_);
my ($lat,$lon) = split /[,\s]+/,$stna{loc};
$stna{loc} = join "",map { sprintf("%010.5f",$_) } (999.99999-$lat),$lon;
if ($stna{class} =~ /:tcoon:/) { $stna{class} = 'A'; }
elsif ($stna{class} =~ /:hydro:/) { $stna{class} = 'B'; }
elsif ($stna{class} =~ /:ccrtns:/) { $stna{class} = 'C'; }
else { $stna{class} = 'Z' }
[ $_, \%stna ];
} PDB::IdList($NOW,split(/[,\s]+/, $class));
If you must create something like the abomination above, at least have the courtesy to comment your code so the person who comes after you can understand your insanity genius and why you chose to nest 3 map() calls inside of a return.
die() that will display a webpage if it is part of an HTTP request.
PDB::Alert() and PDB::Note() should not be used in any Web/CGI stuff.