Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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.

Replies are listed 'Best First'.
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.



Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2016-10-27 20:02 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (370 votes). Check out past polls.