Probably the only thing you can do at this point, at least as far as I can tell from the DBI perldocs, is to fetch all the rows to an array reference and loop through the array reference each time you need the data.
my $dbh = DBI->connect('...', { RaiseError => 0 }) or die "Unable to connect to datasource: $DBI::errstr\n"; my $sth = $dbh->prepare('SELECT PK, Name, Memo...'); or die "Unable to prepare query statement: $DBI::errstr\n"; my $rv = $sth->execute() or die "Unable to execute query statement: $DBI::errstr\n"; my $data = $sth->fetchall_arrayref; my %hash = %{ &checksums($data) }; # some time later... foreach(@$data) { print "PK:\t", $_->[0], "\t"; print "Name:\t", $_->[1], "\t"; print "Digest:\t", $hash{$_->[0]}, "\n"; } sub checksums { my $recordset = shift; my %checksums; foreach(@$recordset) { $checksums{$_->[0]} = &digest($_->[2]); } return \%checksums; }

...untested, of course. That may not even work, it depends on exactly what fetchall_arrayref does.

Otherwise, maybe you can alias or copy the statement handle, although I'm not sure how that would work. If you're feeling ambitious, you could write a wrapper for DBI that caches the statement handle in memory and allows you to manipulate the cursor. Actually, there may already be a module to do that. Check CPAN. =) If you're feeling lazy, maybe you can just run the same query twice and generate two equivalent recordsets. Eew!

When I hear about problems like this I can't help but imagine there's a better way to approach the issue. If you can rewrite your code to avoid having to loop through the recordset twice, that might be the best solution. (see below for a rewrite of the above code)

Good luck,

'kaboo

(this node has been updated)

my @data; while($sth->fetch) { push @data, { PK => $_->[0], Name => $_->[1], Memo => $_->[2], Digest => &digest($_->[2]) }; } # some time later... foreach(@data) { print "PK:\t", $_->{PK}, "\t"; print "Name:\t", $_->{Name}, "\t"; print "Digest:\t", $_->{Digest}, "\n"; }

Not the strongest example, but you get the idea. :)


In reply to Re: Reusing DBI Statement Handle by mwp
in thread Reusing DBI Statement Handle by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.