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
yea if you just pass in $dbh you are passing in a reference. and it is not
a big mem copy or anything inefficient like passing in a whole array
instead of a ref to an array or something.
you can always do a ref() on something to find out what it is.
[dale@eng database]$ perldoc -f ref
=item ref EXPR
=item ref
Returns a TRUE value if EXPR is a reference, FALSE otherwise. If EXPR
is not specified, C<$_> will be used. The value returned depends on the
type of thing the reference is a reference to.
Builtin types include:
REF
SCALAR
ARRAY
HASH
CODE
GLOB
If the referenced object has been blessed into a package, then that
package
name is returned instead. You can think of C<ref()> as a C<typeof()>
operator.
if (ref($r) eq "HASH") {
print "r is a reference to a hash.\n";
}
unless (ref($r)) {
print "r is not a reference at all.\n";
}
if (UNIVERSAL::isa($r, "HASH")) { # for subclassing
print "r is a reference to something that isa hash.\n";
}
See also L<perlref>.
On Mon, 30 Apr 2001, 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?
>
> I tried:
>
> backup (dbh=>\$dbh);
>
> sub backup
> {
> my (%arg) = @_;
> my $dbh = \$arg{"dbh"};
> 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.
>
> Jay
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Auctions - buy the things you want at great prices
> http://auctions.yahoo.com/
>
--
Dale Bewley - Bewley Internet Solutions Inc. http://bewley.net/
|