perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Re: proper referencing of hashes returned by fetchrow_hashref . . .
Date Wed, 25 Jun 2003 04:08:37 GMT

This is the way I think of it. And you can extend this logic to all your
dereferencing needs. Forgive me if I tell you things you already know.

A scalar is one "thing". ${}
An array is many things stacked together, one after the other. @{}
A hash is a set of named things %{}.

So I want the zeroth thing from a stack of things. ${}[0]
Or I want the "bob" thing from a set of named things ${}{'bob'}

In the above syntax I never filled in the {}'s. But the {}'s always
contains a reference. Sometimes that reference is just a variable name
like in ${foo}. But other times that reference is a variable itself like
in ${$bar}. 

Fortunately perl lets us cheat and type a little less. So ${foo} is also
$foo and ${$bar} is the same as $$bar.

This means that when you do a $$hashed_row{'field_name'} you are
actually saying: I have a set of named things %{}. That set is called by
the reference $hashed_row. i.e. %{$hashed_row} But I only want the one
thing named field_name from %{$hashed_row} so... ${}{'field_name'} one
thing named field_name plus the reference $hash_row gives us
${$hash_row}{'field_name'}. But like I said, perl lets us cheat and that
is what gives you the syntax $$hash_row{'field_name'}.

my @array;
my $array_ref = \@array;

"@array" now has two names, @{array} and @{$array_ref} 

Confused yet?... good

But there is a cleaner way to do it. The arrow "->". The arrow syntax is
a subsitute for @{}. %{} and &{}. This is how it works. Lets say we have
a reference $zweeb. And it is actually a reference to an array. So it's
proper form is @{$zweeb}. But perl knows that every time we want
something out of an array we use the []'s to say which element we want.
So really the only thing we need to tell perl is that $zweeb not zweeb
is the reference to use. The -> does that. This means ${$zweeb}[0] and
$zweeb->[0] are the same thing. And so is ${$a}{'joe'} and $a->{'joe'}
or &{$fctn}() and $fctn->().

Hope that helps.

On Tue, 2003-06-24 at 21:05, Richard Schilling wrote:
> Just a quick question, and perhaps it's a Perl language question.  I 
> forget, but why do you have to reference a hash with a double "$$" when 
> you use fetchrow_hashref?
> 	[-
> 	use DBI;
> 	# code to open connection, run query, etc . . .
> 	-]
> 	[$ if $hashed_row = $query->fetchrow_hashref $]
> 	fieldname: [+ $$hashed_row{'fieldname'} +]<br>
> 	[$ endif $]
> Don't know why but for some reason I'm drawing a blank . . .
> Thanks.
> --Richard
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message