I hate confusion, so I went to the source: perldoc perlsub, and here is 
what it had to say:

The Perl model for function call and return values is simple: all 
functions are passed as parameters one single flat
       list of scalars, and all functions likewise return to their caller 
one single flat list of scalars.  Any arrays or
       hashes in these call and return lists will collapse, losing their 
identities--but you may always use pass-by-reference
       instead to avoid this.

In other words, perl passes arguments by copy, not reference.

However, since the DBI handle is itself a reference, what is passed is a 
copy of that reference, which still points back to the same base object. 

In other words, yes, it is safe to just say  "foo($dbh)" , but don't start 
thinking that everytime you pass an argument to a subroutine that you are 
passing a reference, because you are not unless you explicitly do so.

____________________________
Jeff Seger
Fairchild Semiconductor
jeffrey.seger at fairchildsemi.com
____________________________




"Alec Brecher" <[EMAIL PROTECTED]>
01/10/2005 12:14 PM
Please respond to alec

 
        To:     "Dbi-Users" <[email protected]>
        cc: 
        Subject:        RE: passing dbh


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