Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2018-03-20 12:07 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (250 votes). Check out past polls.