US States

Perl

This is an updated Perl script. Mainly it is the HTML that is tidied up a little. This is just to show a Perl example of some code. It is best, IMO, to write so the code is convertible to another language without too much fussing and fiddling. If it is not easily translated, then it is probably bad code–not always, but probably… Here is a screen shot of the output.



Here is the code. #- #		 #		 #		 #		 #- use CGI qw(':standard'); use CGI::Carp; use CGI::Carp qw(fatalsToBrowser); use Time::localtime ; use DBI; use strict; use utl; # frequently needed local utilities print "Content-type: text/html\n\n" ; my $db_error_flag = "N" ; my ($dbh,$sql,$sth, $sth1); my $rc; my @pairs; my $pair; my $name; my $value; my %FORM; my $dt; my $Counter; my @row; my $record; my $tup_num; my $state_abbr; my $state_name; my $title; my $names; my $numFields; my $ntuples; my $ref; my $message; my $hh = localtime( time ) -> hour ; my $mi = localtime( time ) -> min ; my $ss = localtime( time ) -> sec ; my $mm = localtime( time ) -> mon ; # 0 - 11 my $dd = localtime( time ) -> mday ; my $wd = localtime( time ) -> wday ; my $yy = localtime( time ) -> year ; my $year = 1900 + $yy ; my $MM = $mm + 1; #$mm += 1; my $day = $dd; my @month = ( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ) ; my @weekday = ( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ) ; $title = "US State names and abbreviations" ; $dbh = &utl::openDatabase(&utl::getProperties); my $css = &utl::getCSS; print &lt;&lt;EOH ; &lt;!DOCTYPE html> &lt;HTML> &lt;HEAD> &lt;link rel="stylesheet" type="text/css" href="$css"> &lt;TITLE>$title&lt;/TITLE> &lt;SCRIPT LANGUAGE="JavaScript" SRC="angular.js">&lt;/SCRIPT> &lt;SCRIPT LANGUAGE="JavaScript" SRC="jquery.js">&lt;/SCRIPT> &lt;/HEAD> &lt;BODY> &lt;H1>$title&lt;/H1> &lt;br>&lt;br>&lt;br>&lt;br> &lt;p class="centered">Today is $weekday[$wd] $month[$mm] $dd, $year&lt;/p> &lt;div class="centered"> EOH if ( $db_error_flag eq "N" ) { $sql = "SELECT COUNT(*) AS states FROM state_reference " ; $sth = $dbh->prepare( $sql ); if ($DBI::err) { print "error preparing $sql\n"; print "$DBI::errstr\n"; die; } 	$rc = $sth->execute; if ($DBI::err) { print "error executing $sql\n"; print "$DBI::errstr\n"; die; } 	$ref = $sth->fetchrow_hashref; $ntuples = $ref->{states}; $sql = "SELECT ". "name, ". "abbr ". "FROM state_reference ". "ORDER BY name " ; 	$sth = $dbh->prepare( $sql ) ; $sth->execute ; $names = $sth->{'NAME'}; $numFields = $sth->{'NUM_OF_FIELDS'}; } if ( $ntuples &lt; 1 ) { print "&lt;h1 align=center>No records found.&lt;/h1>\n"; } if ( $ntuples > 0 ) { $tup_num = 0 ; print "&lt;table class=\"centered\" border=\"1\">"; print "&lt;tr BGCOLOR=\"LIGHTGREEN\" >"; print "&lt;TH>Name &lt;/TH>"; print "&lt;TH>Abbreviation&lt;/TH>"; print "&lt;/tr>"; while( $tup_num &lt; $ntuples ) { $ref = $sth->fetchrow_hashref; $state_name = $ref->{ 'name' }; $state_abbr = $ref->{ 'abbr' }; # on-error should go here... print "&lt;tr>"; print "&lt;td>"; print "$state_name"; print " "; print "&lt;/td>"; print "&lt;td>"; print "$state_abbr"; print " "; print "&lt;/td>"; print "&lt;/tr>"; $Counter++; $tup_num++; } # end while print "&lt;/table>"; } &utl::closeDatabase( $dbh, $sth ); print &lt;&lt;__END_OF_FOOTER__; &lt;/div> &lt;br>&lt;br>&lt;br>&lt;br> &lt;/BODY> &lt;/HTML> __END_OF_FOOTER__
 * 1) !/usr/bin/perl
 * 1) Author  : Alfred Wheeler
 * 2) Date    : 7/11/2013
 * 3) Program : ListStates.pl
 * 4) Purpose : List the states
 * 5) Notes   :
 * 6) 		$dsn   Database source name
 * 7) 		$dbh   Database handle object
 * 8) 		$sth   Statement handle object
 * 9) 		$h     Any of the handle types above ($dbh, $sth, or $drh)
 * 10) 		$rc    General Return Code  (boolean: true=ok, false=error)
 * 11) 		$rv    General Return Value (typically an integer)
 * 12) 		@ary   List of values returned from the database.
 * 13) 		$rows  Number of rows processed (if available, else -1)
 * 14) 		$fh    A filehandle
 * 15) 		undef  NULL values are represented by undefined values in Perl
 * 16) 		\%attr Reference to a hash of attribute values passed to methods
 * 1) 		\%attr Reference to a hash of attribute values passed to methods
 * 1) 		Database schema : ct227731, Table : state_reference
 * 1) 		field name                       data type
 * 2) 		name                             varchar(255)
 * 3) 		abbr                             varchar(2)
 * 1) 		abbr                             varchar(2)
 * 1) Declare variables
 * 1) Get today's date and time.
 * 1) get list from database
 * 1) Perform the SQL task(s): in this case SELECT
 * 1) 	$ntuples = $sth->{'NUMROWS'};

That's all there is to this example. It works.