in reply to HTML::Template nested loops, DBI/MySQL and map
I only have some stylistic comments, prompted by the fact that add_subject_loop modifies one of its arguments.
I'm also a big fan of prepare_cached, since it can remove the need of global $sth variables.
Here's a suggested refactoring:
I'm not so sure anymore if the map is justified. I feel a foreach loop would probably be easier to read now.sub get_category_loop { return [ map { $_->{subject_loop} = get_subject_loop( delete $_->{category_id} ); # dele +te returns the value that was in the hash $_ } @{ $dbh->selectall_arrayref(sql_category(), {Slice => {}}) } ]; } sub get_subject_loop { my $category_id = shift; my $sth = $dbh->prepare_cached( sql_subject() ); $sth->execute($category_id); return $sth->fetchall_arrayref({}); } sub sql_category { return q{ SELECT category_id, category_name FROM category ORDER BY category_id }; } sub sql_subject { return q{ SELECT subject_id, subject_name FROM subject WHERE category_id = ? ORDER BY subject_name }; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: HTML::Template nested loops, DBI/MySQL and map
by wfsp (Abbot) on Nov 14, 2006 at 14:26 UTC |