RFC - I intend to make this node a tutorial in writing a good, clean cgi/sql application - any suggestions on how best to structure it?

My main concern is that full code-listings are going to swamp it, but on the other hand I want such code-listing available as the code is revised and improved - should I put the code in a seperate node?

Moving on from the RFC, the purpose of this node is elaborated on below...

* * * * * * *

I'm often asked at work to implement a quick database with a web front-end for some task or other...

... and to be frank, a lot of the code I write sucks.

Now I have been asked to write yet-another-bit-of-perl/sql code, but I thought it would be more informative for both myself and others if I rewrote some existing code first and documented the process on perlmonks.

This would give a proper before/after flavour. Needless to say, I'm hoping for some input from the clergy.

So, I'm going to take the following shortish bit of code, and rewrite using CGI::Application and HTML::Template.

I'll also place and test-version of this app and make that available as a resource to accompany this node.

Now (take a deep breath), here is a few samples of the code that I'm going to re-engineer (missing quite a few 'states' and subroutines, but hopefully showing off a few of its sins):

#!/usr/bin/perl use strict; use DBI; use CGI qw(:standard); my $database = 'cvkb'; my ($script_url, $css_url, $host, $server_type, $data_source, $login, +$password); my %db_cols; $db_cols{'all'} = 'oid,question,answer,author,keywords,sites,server,cl +ient,application,fixed_in'; $db_cols{'list'} = 'oid,question'; $db_cols{'add'} = 'question,answer,author,keywords,sites,server,client +,application,fixed_in'; $db_cols{'edit'} = 'question,answer,author,keywords,sites,server,clien +t,application,fixed_in'; my %fld_type = ( 'answer'=>'textarea', 'question'=>'textarea', ); my $msg; $login = 'foobar'; $password = 'barfoo'; $host = 'GONK1'; $server_type = 'my-sql'; $data_source = "DBI:mysql:database=$database;host=$host"; $script_url = './cvfaq.cgi'; $css_url = '/cvfaq/cvfaq.css'; my $error_file = './cvkb.err'; my $log_file = './cvkb.sql'; my $log_sql = 1; ### Main Program open(ERR, ">>$error_file"); open(LOG, ">>$log_file") if $log_sql; my $dbcon = DBI->connect($data_source,$login,$password,{PrintError=>0,RaiseError=> +0,AutoCommit=>1}); my $connect_error = DBI::errstr; if($connect_error){ $msg .= $connect_error . ' : ' . $data_source; kb_top();kb_content('Connect Error', 0);kb_tail(); exit 0; } clean_input(); my $action = param('action'); CGI::delete('action'); if(!$action or $action eq 'list_kbs'){ return_list_page(); exit 0; } elsif($action eq 'add'){ my @cols = split /,/, $db_cols{'add'}; my $insert_vals; foreach(@cols){$insert_vals .= "'" . param($_) . "',";} chop $insert_vals; my $sql = "insert into kb ($db_cols{'add'}) values ($insert_vals)"; my $qh = run_sql($sql); $sql = "select max(oid) from kb"; $qh = run_sql($sql); my @data = $qh->fetchrow_array; return_kb_page($data[0]); } else{ return_full_page('Unknown command: $action',0); exit 0; } exit 0; #### Sub-Routines #### sub run_sql{ my $sql = $_[0]; $dbcon->{LongReadLen}=5000; $dbcon->{LongTruncOk}=1; my $qh = $dbcon->prepare($sql); if($dbcon->errstr){ $msg .= $dbcon->errstr . ' : ' . $sql; print ERR gettime() . $msg . "\n\n"; return 0; } else{ $qh->execute; if($qh->errstr){ $msg .= $qh->errstr . ' : ' . $sql; print ERR gettime() . $msg . "\n\n"; return 0; } } print LOG gettime() . ': ' . $sql . "\n" if $log_sql; return $qh; } sub return_kbs{ my($sql, $qh, @data, @result); if(ref $_[1]){ my $list = join ',', @{$_[1]}; $sql = "select $_[0] from kb where oid in($list) order by oid"; } elsif($_[1]){ $sql = "select $_[0] from kb where oid = $_[1] order by oid"; } else{ $sql = "select $_[0] from kb order by oid"; } $qh = run_sql($sql); if($qh){ while(@data = $qh->fetchrow_array){ push @result, [@data]; } $qh->finish; } return @result; } sub return_kb_page{ my @kb; my $content = '<table>'; @kb = return_kbs($db_cols{'all'},$_[0]); my @labels = split /,/, $db_cols{'all'}; my $count = 0; foreach(@{$kb[0]}){ $_ = html_escape($_); if($fld_type{$labels[$count]} eq 'textarea'){ s/^(.*)$/<p>$1<\/p>/s; s/^\s*$/<\/p><p>/gm; } $content .= "<tr><th>" . ucfirst $labels[$count] . "</th><td>$_</td></tr>\n"; $count ++; } $content .= '<tr><td>' . start_form(-method=>'GET') . hidden('oid', +$_[0]) . hidden('action','edit_form') . submit(-value=>'Edit KB') . end_form() . '</td></tr>'; $content .= '</table>'; return_full_page('CV KB', $content); }
Tom Melly, tom@tomandlu.co.uk

In reply to RFC: CGI/MySQL - improving my code by Melly

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.