in reply to Is this Bad form? (DBI)

I often leave a handle to a database global, and don't bother adding it as a parameter to different subs, where the first thing would be is shifting it off the parameter list. But, IMO, that only works well in programs where you can view the database as a single, omnipresent source. A bit like STDIN, STDOUT and STDERR. They are in most programs omnipresent, and we don't bother passing them around as parameters.

Abigail

Replies are listed 'Best First'.
Re: Re: Is this Bad form? (DBI)
by Anonymous Monk on Aug 25, 2003 at 16:15 UTC

    I agree: I use a similar style. If I expect to only have a single database handle for the program, I'll keep it global. On the other hand, if I expect to have lots of handles running around, I move all the connection code into sub-routines (e.g. one sub per transaction), like this:

    InsertRecord($val1, $val2); my @array = RetrieveValues($val2, $val3); sub InsertRecords { my $dbh = DBI->connect(xxxx); # stuff } sub RetrieveValues { my $dbh = DBI->connect(xxxx); # stuff }

    This is undoubtably inefficient, but it is very clear, and is the first step before I move the subs off into their own module (e.g. StandardTransactions.pm). YMMV