in reply to Code factory
Even though it's below, this code is untested. Use at your own risk.sub build_selector { my ($dbh, $table, $field, @selectables) = @_; my $selectables = join ',', @selectables; return sub { my ($val) = @_; die "No val passed into selector" . $/ unless defined $val && length $val; my $sth = $dbh->prepare_cached(<<"_SQL_"); SELECT $selectables FROM $table WHERE $field = ? _SQL_ die "Couldn't prepare handle: " . $dbh->errstr unless $sth; $sth->execute($val) || die "Couldn't execute handle" . $dbh->errstr; my @vals = $sth->fetchrow_array; $sth->finish; return wantarray ? @vals : \@vals; }; }
Update: Removed superfluous quotes around the placeholder.
------
We are the carpenters and bricklayers of the Information Age.
Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Code factory
by Anonymous Monk on Jul 09, 2003 at 16:40 UTC |