Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2018-06-25 17:59 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.