Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: DBI, $dbh, and subroutines

by GrandFather (Cardinal)
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.

    fnord

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (19)
As of 2014-12-18 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (52 votes), past polls