in reply to Fetchrow_hash/array question with DBI/Perl

Personally, i strive to avoid print HTML one line at time with a simple print statement. The main reason being that you have quoting issues to worry about, and they simply make your code ugly. HERE DOCS can help, but i am going to show you 3 other ways to do this, each a bit different.

Version 1: CGI.pm

use strict; use warnings; use Data::Dumper; use DBI; use CGI::Pretty qw(:standard); my $dbh = DBI->connect( qw(DBI:vendor:database:host user pass), {RaiseError=>1}, ); my $sth = $dbh->selectall_arrayref(' SELECT firstname,lastname,login,email FROM student '); print header,start_html, table( Tr( map th($_), ('First Name', 'Last Name',qw(Login Email)), ), map Tr(map td($_),@$_), @$sth );
We didn't concatenate First and Last name though ...

Version 2: Template Toolkit

use DBI; use CGI qw(header); use Template; my $dbh = DBI->connect( ... ); my $students = $dbh->selectall_arrayref(' SELECT firstname,lastname,login,email FROM student ',{Slice => {}}); print header; my $tt = Template->new; $tt->process(\*DATA, {students => $students}) || die $tt->error(); __DATA__ <table> <tr> <th>Name</th> <th>Login</th> <th>Email</th> </tr> [% FOREACH student = students %] <tr> <td>[% student.lastname %], [% student.firstname %]</td> <td>[% student.login %]</td> <td> <a href="mailto:[% student.email %]">[% student.email %]</a> </td> </tr> [% END %] </table>

Version 3: DBIx::XHTML_Table ... the most inflexible solution, but i still use it from time to time.

use DBIx::XHTML_Table; use CGI qw(header); print header, DBIx::XHTML_Table ->new(qw(DBI:vendor:database:host user pass)) ->exec_query('SELECT firstname,lastname,login,email FROM student') ->map_cell(sub {my$e=shift;qq|<a href="mailto:$e">$e</a>|},'email') ->output ;
Again, didn't concantenate First and Last name, and while i probably could do this with DBIx::XHTML_Table via jumping through hoops of fire ... i would be better off in the long run using Version 2. However, sometimes you do want to keep Last and First name separated -- easy searches and sorts. YMWV, but i highly recommend Version 2.

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)