Thanks to all who replied.  My biggest concern was that copying the dbh
meant that a new connection would be created. And somehow had the impression
that arguements were passed by value not reference.

The reason for embedding the dbh into the obj: $obj->{dbh} is that most of
the functions require the entire object:
bar( $obj ); not just the dbh.

Thank you again.  -Alec



-----Original Message-----
From: David [mailto:[EMAIL PROTECTED]
Sent: Monday, January 10, 2005 11:10 AM
To: Dbi-Users
Subject: Re: passing dbh


On Mon, Jan 10, 2005 at 10:35:16AM -0500, Alec Brecher wrote:
> I am trying to figure out the most efficient method of passing around a
live
> dbh when calling a subs.

Since Perl passes sub arguments by reference, it is already fairly
efficient.  Plus, it is unlikely the overhead of passing a handle around
is even worth optimizing given that the database will be a larger
bottleneck than any subroutine overhead.  Nonetheless, for academic
curiosity:

> I have embedded a dbh into an object: $obj->{dbh}

This is not necessary for passing the handle around, but perhaps you
have other reasons for doing this.  The only way this would affect
passing the handle to subroutines is if they are methods on the same
object, e.g.,

  $obj->method

In that case, you would not need to pass the handle, as it can be
accessed in the method through the object.

> and would like to pass to
> it to other functions for reuse.
>
> Should I call: foo( $obj->{dbh} );

This is ok (unless you need the whole object in foo).  This passes the
handle, which is a blessed reference (object).  Normally in the
subrouting you would do something like this:

  sub foo { my ($dbh) = @_; ... }

to make the code more clear and avoid working with the reference passed
in (but it would incur some small overhead).

> or: foo( \$obj->{dbh} );

This is confusing.  What are you trying to pass?  A reference to the
object or a reference to the handle?  The -> operator binds more
tightly than the \, so you would end up passing a reference to the
handle (which is a blessed reference (object)).  This is unnecessary.

> or something else?

The first method is fine.

> Does the first method copy the dbh and create a new connection?

Copying the handle will not create a new connection.

dd
--
David Dooling

Reply via email to