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:
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:$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>"; } }
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.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.
In reply to Re: Dereferencing nested references
by chipmunk
in thread Dereferencing nested references
by ryddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |