[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] CGI.pm is your friend




Another quick code refactor...


Before:

print "<select tabindex='1' name='month'>\n";
print " <option></option>\n";
for (my $x=1;$x<=12;$x++) {
   printf(" <option value='%02d'>$months[$x]</option>\n",$x);
}
print "</select>\n<select tabindex='2' name='day'>\n";
print " <option></option>\n";
for (my $x=1;$x<=31;$x++) {
   printf(" <option>%02d</option>\n",$x);
}
print "</select>\n<select tabindex='3' name='year'>\n";
print " <option></option>\n";
for (my $x=($tdn[0]-1);$x<=($tdn[0]+2);$x++) {
   printf(" <option>%04d</option>\n",$x);
}
print "</select>\n";


After:

use CGI;
my $q = new CGI;
print $q->popup_menu(-name=>'month', -values=>[undef, 1..12]);
print $q->popup_menu(-name=>'day',   -values=>[undef, 1..31]);
print $q->popup_menu(-name=>'year',  -values=>[undef, $year-1, $year, $year+1, $year+2 ]);


Or that last one using map:

print $q->popup_menu(-name=>'year',  -values=>[undef, map { $year + $_ } -1..2 ]);


Plus when you use CGI your form fields are automatically sticky, which can save your users lots of "Back button" frustration. :)

j
...gonna be the only poster in Oct?  -fingers crossed-  -laugh-