in reply to Dereferencing nested references

Because of the way you created @BuySellHashref, each element is actually a SCALAR reference to a HASH reference. That's why ${buysell}->{BuyerOrSeller} works, when a straight HASH ref would be used as $buysell->{BuyerOrSeller}.

The error is generated for the print line, where you try to access $buysell->{PersonID}. Since $buysell is not a HASH reference, that expression causes an error. It would have to be ${$buysell}->{PersonID} instead.
 

Frankly, I don't see what advantage you get from this extra level of references. I would write the code as:

$SQLBuy = $dbh_metro->prepare(<<"EndOfSQL"); SELECT * FROM BuyersSellers WHERE FileNumber = ? EndOfSQL $rv = $SQLBuy->execute($filenumber); while (my $hash_ref = $SQLBuy->fetchrow_hashref) { push @BuySellHashref, { %{$hash_ref} }; } undef $SQLBuy; $SQLPEOPLE = $dbh_metro->prepare(<<"EndOfSQL"); SELECT * FROM People WHERE PersonID = ? EndOfSQL foreach (@BuySellHashref) { $rv = $SQLPEOPLE->execute($_->{PersonID}); while (my $hash_ref = $SQLPEOPLE->fetchrow_hashref) { $_->{PersonID} = { %{$hash_ref} }; } } undef $SQLPEOPLE; foreach $buysell (@BuySellHashref) { if ($buysell->{BuyerORSeller} =~ /BUYER/i) { print "<TR CLASS=$class><TD>$buysell->{PersonID}->{LastName}", "</TD></TR>"; } }
It is important to note how I am handling the reference returned by fetchrow_hashref: push @BuySellHashref, { %{$hash_ref} }; This is because of the following caveat from the DBI documentation for the fetchrow_hashref method:
Currently, a new hash reference is returned for each row. This will change in the future to return the same hash ref each time, so don't rely on the current behaviour.
If you don't create a new hash from the hashref each time, then after the implementation of DBI changes, each element in @BuySellHashref would point to exactly the same hash.

Replies are listed 'Best First'.
Re: Re: Dereferencing nested references
by ryddler (Monk) on Jan 16, 2001 at 19:50 UTC
    push @BuySellHashref, { %{$hash_ref} };

    This is what I'm having trouble wrapping myself around. I'm not sure I understand exactly what's happening in this snip, so I'll try to break it down the way I see it, and if I'm way out in left field, I'll get some feedback.

    %{$hash_ref}

    This dereferences $hash_ref and then...

    push @BuySellHashref, { dereferenced $hash_ref }

    This creates an anonymous hash containing the hash data from the dereferenced $hash_ref and pushes a reference to it into @BuySellHashref, which means instead of just having the original ref (or as in my original code a ref to a ref), I have a ref to an anonymous hash containing all the original data from $hash_ref.

    ryddler