# Pass these items # DBI object # table name # id column name # and id looking for # Example to get a row with id from user table # my $data = $r2h->($db, 'users', 'id', 10); my $r2h = sub { my $db = shift; my $table_name = shift; my $key_name = shift; my $id = shift; my $data = undef; eval { my $res = $db->prepare( "select * from `$table_name` where `$key_name`=? limit 1") or die $db->errstr(); $res->execute($id) or die $res->errstr(); my $name = $res->{'NAME'}; while(my @c = $res->fetchrow_array()) { my $x = 0; foreach my $n (@{$name}) { $data->{$n} = $c[$x]; $x++; } $data->{'HEADERS'} = $name; } $res->finish(); }; if($@) { $data = undef; } return $data; }; # r2h's in other modules return an array instead # ($data, $err) so that caller can distinguish between no # entry in the table and a real error.