in reply to Tie::STHRows for HTML::Template loops

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)

Replies are listed 'Best First'.
Re: (jeffa) Re: Tie::STHRows for HTML::Template loops
by antirice (Priest) on Jul 30, 2003 at 14:03 UTC

    Agh! I showed a friend how to use this module when just trying to display database results day and he thought it was darned nifty and suggested I post it somewhere. When I went to copy and paste, I seem to have used an old version (was wondering why I had to remove code for shifting). Ah well, thanks for pointing this out to me. :)

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1