I've done some DBI with mysql and postgresql. Here are my thoughts...
First of all, you don't have to use OO to get this done. There are pros and cons to using OO and I won't belabor that here. Second, go straight to your local bookstore and pick up the following 2 O'Reilly books... 1. Programming Perl 3rd Ed. 2. Programming the Perl DBI The first book will give you insight about packages, modules, and OO. The second will give you working examples of DBI code. That's how I got started. Now, about your code... Your code blurb below is warning about subroutine redefinition because what appears to be an attempt at a function prototype is actually a function definition. I generally don't use function prototypes in Perl. Your code blurb below is failing because you are not exporting your CSAdmin package. You may want to do something like this in your package... require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(GetDBHandle someotherfunction yetanotherfunction etc) Something else to think about is what level of abstraction you want to achieve. In some code I did, I decided I didn't want to have to mess with any of the DBI stuff, so I buried all of it a package, much like you are doing. However, I didn't even want to touch the database handle. So, I made wrapper routines inside the package and made the database handle private. This resulted in calls like this in my top-level script... connect_to_my_sales_dbase($username, $password) || die. my query_string = "SELECT * FROM sales_figures..."; my %recs = query_sales($query_string); I made the package handle all of the dbase manipulation particulars. The most my top-level script needed to know was the wrapper names and how to write a few SQL query or ddl strings. The package itself must "use DBI" and whatever DBD you need. Hope this helps. cn -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mark Knoop Sent: Thursday, October 27, 2005 9:55 AM To: [email protected] Subject: Passing references to DB handles between main and subs Hello I am trying to rationalize some of my code and would like to use a set of common functions which do database stuff using DBI. The most logical way for me to do this would be to create a 'GetDBHandle' function which passes back a handle to the database connection which can then be passed to other functions which involve individual common queries. Bearing in mind that: a) I am unfamiliar with OO principals in perl b) I am a bit rusty on perl modules in general c) as well as wanting to do this properly I would like to keep it as simple as required could anyone give me pointers for things to bear in mind. I'm looking at something like the following: ############################################################### use strict; use warnings; use DBI; use DBD::ODBC; # can this live solely in the module? use Common::CSAdmin; # this is my module my $dbh = GetDBHandle('DSN'); my $sql = <<EOF; SELECT Test FROM tblTest EOF my $sth = $dbh->prepare($sql); $sth->execute() or die "$!"; while (my ($test) = $sth->fetchrow) { print $test . "\n"; } $sth->finish; ################################################################ which calls ################################################################# package Common::CSAdmin; use strict; use warnings; use DBI; use DBD::ODBC; sub GetDBHandle($) {} sub GetDBHandle($) { my $dsn = shift(@_); my ($dbusr,$dbpwd,$server,$dbh); $dbusr = 'usr'; $dbpwd = 'pwd'; $server = 'server'; $dbh = DBI-> connect("DBI:ODBC:$dsn", $dbusr, $dbpwd, {RaiseError => 1, PrintError => 1}) || die "Could not connect to datasource $dsn"; return $dbh; } 1; ##################################################################### but to start with I am getting the following Subroutine GetDBHandle redefined at D:\Code\iis/Common/CSAdmin.pm line 11. Undefined subroutine &main::GetDBHandle called at testcsadmin.pl line 7. which I'm not sure about. Aside from this I guess I have to do something a bit more clever with the returning of $dbh but I am having trouble finding examples. Any thoughts? Thanks Mark _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
