On 07 Jun 2001 14:53:40 -0800, Michael Fowler wrote:
> On Thu, Jun 07, 2001 at 05:27:16PM -0400, Chas Owens wrote:
> > In DBI, it (fetchrow_hashref, not fetchhash) returns a hash where the
> > keys = column names and values = value of the columns on the row.  I
> > consider it a generaly bad thing since it forces you to alias your
> > columns when you have two (or more) columns with the same name or an
> > expersion as a column in you result set.
> 
> The flipside to this is code maintainability.  Consider:
> 
>     while (defined($row = $sth->fetchrow_arrayref)) {
>         print $$row[4];
>     }
> 
>     while (defined($row = $sth->fetchrow_hashref)) {
>         print $$row{'permissions'};
>     }
> 
> Quick, what kind of information is likely to be in the row being printed?  :)
> 
> Aliasing in the query is easy, and queries (at least in most of my code) are
> fairly static.  I'm more often wandering through code trying to figure out
> what's accessing what, and using hashes makes things much easier to
> understand at a glance.
> 
> 
> Michael
> --
> Administrator                      www.shoebox.net
> Programmer, System Administrator   www.gallanttech.com
> --

I agree; that is the _only_ thing going for fetchrow_hashref (well, that
and if I am going to stuff a hash into a complex data structure anyways).
The other bad thing about fetchrow_hashref is that destroys the order of
the columns.  This means you have to rebuild it if, say, you are building
and unload file (pipe delimited). Of course, I am biased since I am an old
ESQL/C programmer and when I don't care about order I tend to use :

$sth = $dbi->prepare($query);
$sth->execute;
while (($id, $name, $permissions) = sth->fetchrow_array) {
        do stuff
}
$sth->finish;

compare

$declare GetStuff cursor for :szQuery;
$open GetStuff;
do {
        $fetch GetStuff into :iID, :szName, :szPermissions;
        if (SQLCODE != 0) { break; }

        do stuff
} while (!bTerminate); /*bTerminate set by SIGTERM handler*/
$close GetStuff;
$free GetStuff;

Which is (IMHO) easier to maintain than a hash (the $ref->{} gets on my 
nerves).  As I get more used to Perl and its idioms I may change that 
habit (since array and hash refs are more efficient to pass around).  

--
Today is Pungenday, the 12nd day of Confusion in the YOLD 3167



Reply via email to