Fellow monks,
From time to time, all of us, whatever our level, have something to confess. As a humble Initiate, I must therefore confess that the code I place before you now is not just my first Perl program. It is my first program in any language. Thus I ask for your kind guidance and suggestions as to how it could be improved, and beg your forgiveness for whatever egregious Perl sins may be contained therein. Thank you, brothers.
#!/usr/bin/perl
use diagnostics;
use warnings;
use strict;
use CGI;
my %all_quotas;
my @members = ('Algeria', 'Indonesia', 'Iran', 'Iraq', 'Kuwait', 'Liby
+a', 'Nigeria', Qatar', 'Saudi Arabia', 'UAE', 'Venezuela', 'total OPE
+C' );
# Sorry about the ghastly ASCIIbetical hack. Suggestions welcome!
my $query = CGI->new();
print $query->header("text/html"),
$query->start_html( -title => "QuotaBase: a database of OPEC o
+il production quotas",
-bgcolor => "#cbcbcb" ),
$query->h1("Welcome to QuotaBase!"),
$query->start_table(),
$query->Tr(),
$query->td({-width => "600"}),
$query->p("QuotaBase is an interactive database of OPEC oil prod
+uction quotas. By default, the <b>current quotas</b> are displayed. T
+o view <b>historical quota information</b>, select the period you wan
+t from the drop-down list and click the 'Show quotas' button. A table
+ of all the quotas for that period will be displayed."),
$query->end_td(),
$query->end_Tr(),
$query->end_table(),
$query->start_form(),
"Choose a period: ",
" ",
$query->popup_menu( -name=>'period',
-values=>['Jan 02 - Dec 02', 'Sep 01 - Dec 0
+1', 'Apr 01 - Aug 01', 'Feb 01 - Mar 01', '31 Oct 00 - Jan 01', '1 Oc
+t 00 - 30 Oct 00'],
-default=>'Jan 02 - Dec 02'),
" ",
$query->submit(-name=>'period', -value=>'Show quotas'),
" ",
$query->defaults('Reset current quotas'),
$query->endform;
$query->end_p(),
build_database();
my @$period = $query->param('period');
process_query(@$period);
print $query->end_html;
##### subs #####
sub build_database { # thanks to Zaxo
while (<DATA>) {
( my $period, $_ ) = split ':';
@{$all_quotas{$period}}{@members} = split ' ';
}
}
sub process_query {
($period) = @_;
if ($period) {
print_table();
} else {
$period = 'Jan 02 - Dec 02';
print_table();
}
}
sub print_table {
print "<table border=1>\n";
print "<tr><td colspan=2 align=center><b>$period</b></td></tr>
+\n";
foreach my $member ( sort keys %{ $all_quotas{$period} } ) {
print "<tr><td width=130 align=left>$member</td>
<td width=130 align=right>$all_quotas{$period}{$member}</t
+d></tr>\n";
}
print "</table>";
}
__DATA__
Jan 02 - Dec 02: 693 1125 3186 0 1741 1162 1787 562 7053 1894 2
+497 21700
Sep 01 - Dec 01: 741 1203 3406 0 1861 1242 1911 601 7541 2025 2
+670 23201
Apr 01 - Aug 01: 773 1255 3552 0 1941 1296 1993 627 7865 2113 2
+786 24201
Feb 01 - Mar 01: 805 1307 3698 0 2021 1350 2075 653 8189 2201 2
+902 25201
31 Oct 00 - Jan 01: 853 1385 3917 0 2141 1431 2198 692 8674 2333 3
+077 26700
1 Oct 00 - 30 Oct 00: 837 1359 3844 0 2101 1404 2157 679 8512 2289 3
+019 26200