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.

In reply to Re: Dereferencing nested references by chipmunk
in thread Dereferencing nested references by ryddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.