l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
January 6: Social gathering
Next Installfest:
TBD
Latest News:
Nov. 18: Club officer elections
Page last updated:
2001 Dec 30 17:04

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] A question of perl style
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] A question of perl style



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.


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Sunset Systems
Who graciously hosts our website & mailing lists!