[Melbourne-pm] fetchrow_array() vs fetchrow_arrayref() WAS RE: Perl DBI reference recommendations

Craig Sanders cas at taz.net.au
Thu Jul 16 23:45:18 PDT 2009


On Fri, Jul 17, 2009 at 11:47:34AM +1000, Leigh Sharpe wrote:
> Sam's example earlier used this:
>  
> >while (my $row = $sth->fetchrow_arrayref) {
> >  my ($foo, $bar) = @$row;
> >  print "$foo  $bar\n";
> >}
> 
> Is there any advantage to using fetchrow_arrayref() here, instead of:
> 
> while (my ($foo, $bar) = $sth->fetchrow_array()) {
>   print "$foo  $bar\n";
> }
> 
> The latter just 'feels' more intuitive to me, but am I missing something
> here which makes a reference more applicable?

i tend to use fetchrow_hashref() or fetchall_hashref() - that way i can
use the sql column names to access the data and don't have to guess
which array element matches which field....and, more importantly, don't
have to renumber things when i add or remove a field to the select.


oh, and arrayref/hashref are almost certainly faster than the non-ref
versions, and use less memory - they return only a reference to the data
rather than the a copy of the entire data structure.

craig

-- 
craig sanders <cas at taz.net.au>


More information about the Melbourne-pm mailing list