<p>I want to write a little "DBQuery" function in perl so I can have one-liners which send an SQL statement and receive back and an array of hashes, i.e. a recordset. However, I'm running into an issue with Perl syntax (and probably some odd pointer/reference issue) which is preventing me from packing out the information from the hash that I'm getting from the database. The sample code below demonstrates the issue.</p>

<p>I can get the data "Jim" out of a hash inside an array with this syntax:</p>

<pre><code>print $records[$index]{'firstName'}
</code></pre>

<p>returns "Jim"</p>

<p>but if I copy the hash record in the array to its own hash variable first, then I strangely can't access the data anymore in that hash:</p>

<pre><code>
    %row = $records[$index];
    $row{'firstName'};
</code></pre>

<p>returns "" (blank)</p>

<p>Here is the full sample code showing the problem. Any help is appreciated:</p>

<pre><code>
my @records = (
   {'id' => 1, 'firstName' => 'Jim'},
   {'id' => 2, 'firstName' => 'Joe'}
);
my @records2 = ();

$numberOfRecords = scalar(@records);
print "number of records: " . $numberOfRecords . "\n";
for(my $index=0; $index &lt; $numberOfRecords; $index++) {

   #works
   print 'you can print the records like this: ' . $records[$index]{'firstName'} . "\n";

   #does NOT work
   %row = $records[$index];
   print 'but not like this: ' . $row{'firstName'} . "\n";

} 
</code></pre>

[{'answer_id': 51205, 'author': 'Commodore Jaeger', 'author_id': 4659, 'author_profile': 'https://Stackoverflow.com/users/4659', 'pm_score': 6, 'selected': True, 'text': '<p>The nested data structure contains a hash <em>reference</em>, not a hash.</p>\n\n<pre><code># Will work (the -&gt; dereferences the reference)\n$row = $records[$index];\nprint "This will work: ", $row-&gt;{firstName}, "\\n";\n\n# This will also work, by promoting the hash reference into a hash\n%row = %{ $records[$index] };\nprint "This will work: ", $row{firstName}, "\\n";\n</code></pre>\n\n<p>If you\'re ever presented with a deep Perl data structure, you may profit from printing it using <a href="http://search.cpan.org/perldoc?Data::Dumper" rel="noreferrer">Data::Dumper</a> to print it into human-readable (and Perl-parsable) form.</p>\n'}, {'answer_id': 51206, 'author': 'Matthew Watson', 'author_id': 3839, 'author_profile': 'https://Stackoverflow.com/users/3839', 'pm_score': 1, 'selected': False, 'text': '<p>what you actually have in your array is a hashref, not a hash. If you don\'t understand this concept, its probably worth reading the <a href="http://perldoc.perl.org/perlref.html" rel="nofollow noreferrer">perlref</a> documentation.</p>\n\n<p>to get the hash you need to do</p>\n\n<pre><code>my %hash = %{@records[$index]};\n</code></pre>\n\n<p>Eg.</p>\n\n<pre><code>my @records = (\n     {\'id\' =&gt; 1, \'firstName\' =&gt; \'Jim\'}, \n     {\'id\' =&gt; 2, \'firstName\' =&gt; \'Joe\'}\n  );\n\n  my %hash = %{$records[1]};\n\n  print $hash{id}."\\n";\n</code></pre>\n\n<p>Although. I\'m not sure why you would want to do this, unless its for academic purposes. Otherwise, I\'d recommend either fetchall_hashref/fetchall_arrayref in the DBI module or using something like Class::DBI.</p>\n'}, {'answer_id': 51209, 'author': 'Nathan Fellman', 'author_id': 1084, 'author_profile': 'https://Stackoverflow.com/users/1084', 'pm_score': 3, 'selected': False, 'text': "<p>The array of hashes doesn't actually contain hashes, but rather an <em>references</em> to a hash.\nThis line:  </p>\n\n<pre><code>%row = $records[$index];\n</code></pre>\n\n<p>assigns %row with one entry.  The key is the <em>scalar</em>:</p>\n\n<pre><code>   {'id' =&gt; 1, 'firstName' =&gt; 'Jim'},\n</code></pre>\n\n<p>Which is a reference to the hash, while the value is blank.</p>\n\n<p>What you really want to do is this:</p>\n\n<pre><code>$row = $records[$index];\n$row-&gt;{'firstName'};\n</code></pre>\n\n<p>or else:</p>\n\n<pre><code>$row = %{$records[$index];}\n$row{'firstName'};\n</code></pre>\n"}, {'answer_id': 51307, 'author': 'zigdon', 'author_id': 4913, 'author_profile': 'https://Stackoverflow.com/users/4913', 'pm_score': 2, 'selected': False, 'text': '<p>Others have commented on hashes vs hashrefs.  One other thing that I feel should be mentioned is your DBQuery function - it seems you\'re trying to do something that\'s already built into the DBI?  If I understand your question correctly, you\'re trying to replicate something like <a href="http://search.cpan.org/~timb/DBI-1.607/DBI.pm#selectall_arrayref" rel="nofollow noreferrer">selectall_arrayref</a>:</p>\n\n<blockquote>\n  <p>This utility method combines "prepare", "execute" and "fetchall_arrayref" into a single call. It returns a reference to an array containing a reference to an array (or hash, see below) for each row of data fetched.</p>\n</blockquote>\n'}, {'answer_id': 53145, 'author': 'theorbtwo', 'author_id': 4839, 'author_profile': 'https://Stackoverflow.com/users/4839', 'pm_score': 2, 'selected': False, 'text': '<p>To add to the lovely answers above, let me add that you should always, always, always (yes, three "always"es) use "use warnings" at the top of your code.  If you had done so, you would have gotten the warning "Reference found where even-sized list expected at -e line 1."</p>\n'}, {'answer_id': 70678, 'author': 'jonfm', 'author_id': 6924, 'author_profile': 'https://Stackoverflow.com/users/6924', 'pm_score': 0, 'selected': False, 'text': '<p>Also note a good perl idiom to use is</p>\n\n<pre>for my $rowHR ( @records ) {\n   my %row = %$rowHR; \n   #or whatever...\n}</pre> \n\n<p>to iterate through the list.</p>\n'}]
['https://Stackoverflow.com/questions/51195', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4639/']