This is a very common error. So common, in fact, that it's listed in the Common Mistakes section of the Perl Data Structures Cookbook.
The problem (as you can see) is that you are reusing the same reference each time round the loop. There are two ways round this. You can either take a copy of the array each time like this:
foreach $key (@keys) { $sth->execute($key); $columns_ref = $sth->fetchrow_arrayref(); push(@entries, [@$columns_ref]); }
Or you can make the reference variable lexical to the block, thus forcing a new variable to be created each time, like this:
foreach $key (@keys) { $sth->execute($key); my $columns_ref = $sth->fetchrow_arrayref(); push(@entries, $columns_ref); }
Personally, I prefer the second alternative.
Update: Having read other responses in this thread saying that DBI will always return the same reference, it looks like the second option won't work in this case. The first one will tho'.
--Perl Training in the UK <http://www.iterative-software.com>
In reply to Re: Multiple references pointing to the same memory location
by davorg
in thread Multiple references pointing to the same memory location
by ezekiel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |