Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: DBI, $dbh, and subroutines

by GrandFather (Sage)
on Jul 12, 2009 at 03:45 UTC ( #779301=note: print w/ replies, xml ) Need Help??

in reply to DBI, $dbh, and subroutines

Your current practise is exactly what is recommended and for just the reasons you give.

There is no need to worry about any 'overhead' issues in passing the db handle around - it is a scalar reference to a blessed object so all you are really passing around is essentially the address of the 'real' object.

Depending on context, in a situation where I have a number of related variables I'm inclined to use a light weight object. Consider:

use strict; use warnings; my %stuff = ( this => 'this important parameter', that => 'that important parameter', dbh => 'the all important database handle', ); my $obj = bless \%stuff; $obj->DoThis ('params', 'as', 'required'); sub DoThis { my ($self, $params, $as, $required) = @_; print "$self->{this} and $self->{that} and $params $as $required\n +"; }

True laziness is hard work

Comment on Re: DBI, $dbh, and subroutines
Download Code
Re^2: DBI, $dbh, and subroutines
by Illuminatus (Curate) on Jul 12, 2009 at 19:24 UTC
    GrandFather says it very well. The only other reason I would add is re-use. At some point, the 'only' db handle that you use will most likely turn into more than one. At that point, you will probably want to use many of the same routines you already have. This will obviously be easier when the handle is passed into those routines.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://779301]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2015-07-06 11:21 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (72 votes), past polls