Lately, I've been trying to improve my skills and style in Perl.  Much of what I work on in Perl involves the CGI module.  So I would like to get some criticisms of my skills and style in writing a CGI Perl script.  Below is a sample of one of my simpler CGI scripts--it's a collection of snippets I've culled from books and web sites and modified.  It's a bit long, but I think posting it could be useful to monks who are new to the CGI perl module in that this script does work and is complete.  But feedback from more senior monks would be useful to me in particular.

I'd like all the criticisms I can get, but please don't feel obligated to review and comment on everything.  You could just pick out one line that stands out.  I would like comments on functionality, documentation, layout, spacing, etc.  No criticism will be considered too minor.  Thanks in advance.

-Spenser

#!/usr/bin/perl -w # Program Name: emp-list.cgi # Primary Programmer: Spenser # Description of Program: # This is a Perl (vs. 5) script which lists # employees in a web format with hyper-links # to another script/web page containing details. # Set Perl Modules & Initial Variables use strict; use CGI qw/:standard/; use DBI; my $q = new CGI; my $sort = param("sort") || "emp_last"; my $style = "<link rel='stylesheet' href='/styles/hr.css' type='text/c +ss' />"; # Extract list of employees from mySQL my $dbh = DBI->connect("DBI:mysql:sys_main:localhost", "user", "passwo +rd") || die "Could not connect to database: " . DBI->errstr; my $sql_stmnt = "SELECT emp_id, CONCAT(emp_last, ', ', emp_first), dep +t_name FROM sys_main.humans, sys_main.depts WHERE status='AV' AND dept_id=dept ORDER BY '$sort'"; my $sth = $dbh->prepare($sql_stmnt); $sth->execute(); # Create web page for displaying data to user print $q->header( -type=>'text/html'), "\n\n", $q->start_html(-title=>'Employee Table', -bgcolor=>'#FFFFFF', -link=>'#4682B4', -vlink=>'#5F9EA0', -alink=>'#1E90FF'), "\n\n", $style, "\n\n", $q->start_table({-width=>'450', -border=>'0', -cellpadding=>'2', -cellspacing=>'0'}), "\n\n", $q->start_Tr, $q->start_td({-align=>'left', -width=>'100%', -colspan=>'4'}), "\n", "<span class=section-heading>Employee Table</span>", $q->hr, "\n\n", $q->p("To access an employee\'s records, just click on their name in the table below. &nbsp;To re-sort the list, click on the columnn heading to sort by"), "\n", $q->hr, $q->end_td, $q->end_Tr, "\n\n", $q->start_Tr, $q->start_td({-align=>'left', -width=>'20%'}), "\n", $q->a({-href=>"emp-list.cgi?sort=emp_last"}, "<span class=col-heading>Emp. ID</span>"), "\n", $q->end_td, "\n", $q->start_td({-align=>'left', -width=>'45%', -colspan=>'2'}), $q->a({-href=>"emp-list.cgi?sort=emp_id"}, "<span class=col-heading>Employee Name</span>"), "\n", $q->end_td, "\n", $q->start_td({-align=>'left', -width=>'35%'}), $q->a({-href=>"emp-list.cgi?sort=dept"}, "<span class=col-heading>Department</span>"), "\n", $q->end_td, $q->end_Tr, "\n\n", $q->start_Tr, $q->start_td({-align=>'left', -width=>'100%', -colspan=>'4'}), "\n", $q->hr, $q->end_td, $q->end_Tr, "\n\n"; # Loop through employee data and display info. while (@_ = $sth->fetchrow_array()) { $emp_id = $_[0]; $emp_name = $_[1]; $dept = $_[2]; print $q->start_Tr, $q->start_td({-align=>'left', -width=>'20%'}), "\n", $q->a({-href=>"emp-view.cgi?emp_id=$emp_id"}, "$emp_id"), "\n", $q->end_td, "\n", $q->start_td({-align=>'left', -width=>'45%', -colspan=>'2'}), $q->a({-href=>"emp-view.cgi?emp_id=$emp_id"}, "$emp_name"), "\n", $q->end_td, "\n", $q->start_td({-align=>'left', -width=>'35%'}), "\n", "$dept", $q->end_td, $q->end_Tr, "\n\n"; } $sth->finish(); $dbh->disconnect(); print $q->start_Tr, $q->start_td({-align=>'right', -width=>'100%', -colspan=>'4'}), $q->hr, "\n", $q->img({-src=>'/images/poweredbymysql.gif', -align=>'right', -border=>'0', -alt=>'This page was created by Perl & mySQL'}), $q->end_td, $q->end_Tr, "\n\n", $q->end_table, "\n", $q->end_html; exit;

In reply to A Matter of Style in CGI by Spenser

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.