http://www.perlmonks.org?node_id=272044


in reply to Code factory

It's a simple case of re-factoring to strip out this redundancy. Make a simple template function first:
sub getById { my ($table, $column, $id) = @_; my $dbh = sqlConnect(); my $sth = $dbh->prepare("SELECT * FROM $table WHERE $column=?"); $sth->execute($id); my $result = $sth->fetchrow_hashref(); $sth->finish(); return $result; }
Now you can call this function with the appropriate parameters, or, try and make these wrapper functions:
sub getCustomerById { my ($id) = @_; return getById("customers", "cust_id", $id); }
This can be tuned to match any of your various "getXById" functions by simply supplying different parameters.