Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Code factory

by Coruscate (Sexton)
on Jul 08, 2003 at 00:36 UTC ( [id://272165]=note: print w/replies, xml ) Need Help??


in reply to Re: Code factory
in thread Code factory

My select sub (part of Coruscate::DB (not on cpan) package):

sub select { my ($self, $want, $query, @args) = @_; die "Not connected to a database!" unless defined $self->{'dbh'}; my $results; if (ref $want eq "HASH") { my $sth = $self->{'dbh'}->prepare("SELECT $query"); $sth->execute(@args); while (my $row = $sth->fetchrow_hashref()) { push @{$results}, { map { $_, $row->{$_} } keys %{$row} }; } } elsif (ref $want eq "ARRAY") { $results = $self->{'dbh'}->selectall_arrayref("SELECT $query", {}, @ +args); undef $results if $#{$results} < 0; } return $results; }

I absolutely love my little module. Extremely simple interface that can do everything. Sweet :)


If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Replies are listed 'Best First'.
Re: Re: Re: Code factory
by duct_tape (Hermit) on Jul 08, 2003 at 14:36 UTC
    Instead of doing this:
    push @{$results}, { map { $_, $row->{$_} } keys %{$row} };
    Wouldn't it be better/faster to just do this:
    push @{$results}, $row;
    You already have a hash reference, why recreate it?
      If I recall correctly, DBI recycles the hash reference with each iteration. What you end up with is an ARRAY of exactly the same HASH data. You should duplicate it, as was done with that first bit of code, or better yet:
      push(@$results, { %$row });
        No, it does a new hashref every time. At least, this works for me:
        my @result; while (my $row = $sth->fetchrow_hashref()) { push @result, $row; }

        This is a late response since I tend to not pay attention too often.

        DBI recycles arrayref's, but not hashref's. It may be good to make a copy of the hashref just in case that changes in the future though....

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://272165]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-16 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found