Very cool stuff, but i think i may have found a bug. I tested with this:
my $sth = $dbh->prepare('select * from songs limit 5'); $sth->execute; tie my @data, Tie::STHRows, $sth; my $tmpl = q| <tmpl_loop data> <tmpl_var id> <tmpl_var artist> <tmpl_var album> <tmpl_var title> <tmpl_var year> <tmpl_var genre> </tmpl_loop> |; my $template = HTML::Template->new(scalarref => \$tmpl); $template->param(data => \@data); print $template->output;
and the output was:
5 Van Halen Van Halen
You Really Got Me 1978 Rock & Roll

5 Van Halen Van Halen
You Really Got Me 1978 Rock & Roll

5 Van Halen Van Halen
You Really Got Me 1978 Rock & Roll

5 Van Halen Van Halen
You Really Got Me 1978 Rock & Roll

5 Van Halen Van Halen
You Really Got Me 1978 Rock & Roll
The same record. I thought this might happen with my first test ... simply Data::Dumper the contents of @data:
The problem is context. Tie::STHRows::FETCH is returning a list of one hash ref (the rest in that list are references back to that hash ref) if you use your tied array outright. If you use it like an iterator, then all is well. I modifed Tie::STHRows::FETCH to return a (get this) new anonymous hash reference that contains the contents of the hash reference to be returned:
sub FETCH { my $self = shift; $self->[0]->fetch; my %row = %{$self->[1]}; return {%row}; }
However, this may hinder performance ... but it works for me now:
1 Van Halen Van Halen II
Spanish Fly 1979 Rock & Roll

2 Van Halen Van Halen II
Beautiful Girls 1979 Rock & Roll

3 Van Halen Van Halen II
Dance The Night Away 1979 Rock & Roll

4 Van Halen Van Halen
Eruption 1978 Rock & Roll

5 Van Halen Van Halen
You Really Got Me 1978 Rock & Roll
Like i said, i really like this module and would more than likely use it often if it were available via CPAN. I do suggest that you rename it to Tie::Array::STHRows - a while back japhy called for authors to pick better names for Tie'ed modules, and since yours inherits from Tie::Array, i think it is fitting to name it appropriately.

Fun stuff. :) antirice++

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: Tie::STHRows for HTML::Template loops by jeffa
in thread 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.