Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Code factory

by tadman (Prior)
on Jul 07, 2003 at 17:59 UTC ( #272044=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Code factory
Select or Download Code
Re: Re: Code factory
by flounder99 (Friar) on Jul 07, 2003 at 18:07 UTC
    Why not go a step farther?
    sub getByFieldval { my ($table, $field, $val) = @_; my $dbh = sqlConnect(); my $sth = $dbh->prepare("SELECT * FROM $table WHERE $field=?"); $sth->execute($val); my $result = $sth->fetchrow_hashref(); $sth->finish(); return $result; }
    Of course I would probably do more error checking.

    --

    flounder

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://272044]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-07-29 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls