in reply to passing DBI database handles to subroutines
(Database specific stuff is taken from a project I'm working on.)#!/usr/bin/perl -w use strict; use DBI; my $dbh = connect_db(); print ">>$dbh<<\n"; query($dbh); sub connect_db { my $dbh = DBI->connect('DBI:mysql:dbname', 'user', 'pass') || die "can't open DB: $DBI::errstr\n"; return $dbh; } sub query { my $handle = shift; my $sth = $dbh->prepare('Select id FROM page_data'); my $id; $sth->execute(); $sth->bind_columns(\$id); while ($sth->fetch()) { print "\tFound id $id.\n"; } $sth->finish(); }
However, I'd be more likely to do what you suggest, adding the DB handle to $self and adding wrapper methods to the class to operate on the handle and return data. Why not abstract all the way and make an OO interface around the database object? That way, you don't have to pass it to different objects, you just have one object that knows how to get at your data.
|
|---|