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)

In reply to Re: Fetchrow_hash/array question with DBI/Perl by jeffa
in thread Fetchrow_hash/array question with DBI/Perl by perleager

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.