in reply to I present to you... Horrible code!
I read the first line of cchampion's reply but not the rest, so I thought I'd take a stab myself.
Like the OP, I presume the variable $dbh, the array @userdata and the subroutine error() to be predefined. However, it appears that the OP is using this subroutine to set a global variable called $sth from which results are later retrieved. I find this upsetting so I'm going to have the subroutine termgrades() return it instead, and return undef in case of errors. I also changed the messages so that an prepare error would not return the same message as an execute error.
my %quarternames = (1 => 'first', 2 => 'second', 3 => 'third', 4 => ' +fourth'); my %tablenames = map { $_ => "$quarternames{$_}quarter" } keys %quarternames; my %queries = map { $_ => " SELECT * FROM $tablenames{$_} WHERE userid = ? AND period = ? AND gradetype = +? " } keys %tablenames; sub termgrades { my $period = shift; my $quarter = shift; my $sth; return undef unless exists $queries{$quarter}; $sth = $dbh->prepare_cached($queries{$quarter}) or do { error ("2", "Could not prepare query for $quarterna +mes{$quarter} quarter for $userdata[0]: $dbh->errstr"); return undef; }; $sth->execute($userdata[0], $period, 2) or error("2", "Could not execute query for $quarternames{$qua +rter} quarter for $userdata[0]: $sth->errstr"); return $sth; }
Update: Made hash initialization slightly more clever.
|
|---|