http://www.perlmonks.org?node_id=272042
Notromda has asked for the wisdom of the Perl Monks concerning the following question:

I'm starting a large (for me) project that has quite a few mysql tables, and I need to write quite a few functions to access these tables. I discoved that a lot of the code is the same, differing only in table names and such. For example,
sub getCustomerById { my $id = shift; my $dbh = sqlConnect(); my $sth = $dbh->prepare("SELECT * FROM customers WHERE cust_id=?") +; $sth->execute($id); my $result = $sth->fetchrow_hashref(); $sth->finish(); return $result; } sub getUserByUserId { my $id = shift; my $dbh = sqlConnect(); my $sth = $dbh->prepare("SELECT * FROM users WHERE user_id=?"); $sth->execute($id); my $result = $sth->fetchrow_hashref(); $sth->finish(); return $result; }
And there will be many similar instances. Is there a better way to generate all these funcions? Is that good practice, or should they all be written out in a source file somewhere?