Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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
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 perusing the Monastery: (7)
As of 2015-11-27 18:15 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (731 votes), past polls