Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

Replies are listed 'Best First'.
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]
[Discipulus]: water on moon? they can finally produce beer!
[Corion]: Discipulus++ # focus on the important things

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2017-07-25 09:07 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (366 votes). Check out past polls.