Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Code factory

by tadman (Prior)
on Jul 07, 2003 at 17:59 UTC ( [id://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.

    --

    flounder

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-24 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found