I use HTML::Template to generate webpages. An annoyance of mine has always been needing to collect hashrefs from a DBI statement handler into an array so I can use the information in loops. I finally broke down and wrote this module. Yes, I am aware of $sth->fetchall_hashref. Unfortunately, this is quite memory intensive if there is a lot of data to be fetched. I hope someone else finds this module useful.
package Tie::STHRows; use base Tie::Array; sub TIEARRAY { my $class = shift; my $sth = shift; die "You must pass a statement handler!" unless ref($sth) eq "DBI::s +t"; my %row; $sth->bind_columns( \( @row{ @{$sth->{NAME_lc} } } )); bless ([$sth,\%row],$class); } sub FETCH { my $self = shift; $self->[0]->fetch; return +{ %{$self->[1]} }; # per tye's suggestion } sub FETCHSIZE { ${+shift}[0]->rows; }; sub DESTROY { ${+shift}[0]->finish; } 1; __DATA__ sample snippet for how it works: $sth = $dbh->prepare(...); $sth->execute; tie @loopdata, Tie::STHRows, $sth; $template->param(LOOPDATA => \@loopdata); and your template: <TMPL_LOOP NAME=LOOPDATA> ... </TMPL_LOOP>

In reply to Tie::STHRows for HTML::Template loops by antirice

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.