On Mon, Apr 30, 2001 at 11:17:06AM -0700, Jay Strauss wrote:
> Lets say I have a database handle object, $dbh.  Now I
> want to pass it to another subroutine like:
> 
> backup(dbh=>$dbh);
> 
> sub backup
> {
>    my (%arg) = @_;
>    my $sth = $arg{"dbh"}->prepare(some SQL);
>    my $rc  = $sth->execute();
> }
> 
> Now this works, but shouldn't I be passing a reference
> to the object, and then unreferencing it in the sub
> routine?

You already are!

> 
> I tried:
> 
> backup (dbh=>\$dbh);
> 
> sub backup
> {
>    my (%arg) = @_;
>    my $dbh = \$arg{"dbh"};

Hm...  $arg{"dbh"} is *already* a reference to a reference to an
object, so applying the \ operator makes it a reference to a reference
to a reference of an object.

I think $dbh = ${$arg{"dbh"}} would've worked, but this is pretty pointless.

>    my $sth = $dbh->prepare(some SQL);
>    my $rc  = $sth->execute();
> }
> 
> But that didn't work.  Maybe this is all stupid
> because I think $dbh is just a pointer to an object,
> and so passing by value just gives me another pointer
> to an object.

Pretty much, yeah - except that Perl doesn't have pointers.  But if
you meant reference, that's absolutely correct.

In fact, this is always the case, since Perl's definition of an object
is a reference (any kind, but usually a hashref) that has been
"blessed" into a class.

Reply via email to