CGI Example #3

Temperature Converter v3

Formatting added to match the rest of website...
Code broken down into subroutines and out-sourced to the dwd.pm perl module

Return to the notes... - Run the program - View dwd.pm module code

#!/usr/local/bin/perl -w
#
cgi_demo3.pl    (c) Andy Belcher DandyWebDesign 2006
#
#  A simple web-page widget to convert temperatures and demonstrate simple
#  CGI forms. Takes a single input temperature and a unit (F, C or K) and
#  displays the equivalent in all three units.
#  NOTE!  This is a CGI demonstration example intended to show
#  different methods rather than an efficient piece of coding!
#
#  This is the enhanced demo.
#  This one produces a page that matches the rest of the site.
Uses a shared perl module dwd.pm as part of a program suite demo.
#
################################################################################

#  Pull in required Perl modules
use strict;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);
use dwd;

#  DECLARE VARIABLES
my $returnurl  = "/tech/cgi/cgi_share.shtml#notes";
my $sourceurl  = "/tech/cgi/cgidemo_source3.html";
my $pmsourceurl  = "/tech/cgi/dwdpm_source.html";
my $intemp;
my $inunit;
my %G;
$G{'title'}    = "Dandy Web Design : Techdocs: CGI Applications: Styled CGI Demo...";
$G{'doc_root'}    = $ENV{'DOCUMENT_ROOT'};
$G{'headssi'}    = "$G{'doc_root'}/ssi/head.ssi";
$G{'headerssi'}    = "$G{'doc_root'}/ssi/header.ssi";
$G{'footerssi'}    = "$G{'doc_root'}/ssi/footer.ssi";
$G{'navbarssi'}    = "$G{'doc_root'}/ssi/navbar.ssi";
$G{'technavbarssi'}  = "$G{'doc_root'}/ssi/technavbar.ssi";
$G{'othnavbarssi'}  = "$G{'doc_root'}/ssi/othnavbar.ssi";
$G{'bespokessi'}  = "$G{'doc_root'}/ssi/bespoke.ssi";
$G{'cginavlinksssi'}  = "$G{'doc_root'}/ssi/cginavlinks.ssi";
$G{'cgisrclinksssi'}  = "$G{'doc_root'}/ssi/cgisrclinks.ssi";


#  POPULATE OUTPUT SCALARS WITH DEFAULT VALUES FOR DISPLAY
my $outtempc  = 0;
my $outtempk  = 273.15;
my $outtempf  = 32;

#  READ PARAMETERS, FOR EACH PARAMETER USE THE 'param' FUNCTION (PART
#  OF THE CGI MODULE) TO EXTRACT THE ARGUMENTS FROM THE FORM.
#  NOTE THAT IF THE ARGUMENT IS NOT PRESENT $ARG{PARA} IS UNDEFINED
#  NOTE ALSO THAT THE ARGUMENT NAMES MATCH THE FIELD NAMES IN THE FORM
$intemp = param('intemp');
unless((defined($intemp)) && ($intemp)) {$intemp = 0;}
$inunit = param('inunit');
unless((defined($inunit)) && ($inunit)) {$inunit = "C";}

#  PERFORM REQUIRED CALCULATION
if($inunit eq "F")
  {$outtempf  = $intemp;
  $outtempc  = &conv_temp($intemp,"f2c");
  $outtempk  = &conv_temp($outtempc,"c2k");
  }
elsif($inunit eq "K")
  {$outtempk  = $intemp;
  $outtempc  = &conv_temp($intemp,"k2c");
  $outtempf  = &conv_temp($outtempc,"c2f");
  }
else
  {$outtempc  = $intemp;
  $outtempk  = &conv_temp($intemp,"c2k");
  $outtempf  = &conv_temp($intemp,"c2f");
  }

#  OUTPUT PAGE
&do_top;
&do_pathbar;
print "<div id=\"content\">\n";
do_std_rightbar($G{'bespokessi'},$G{'cginavlinksssi'},$G{'cgisrclinksssi'});
print<<HERE;
<h2>Perl CGI Demo Program</h2>
<h3>Temperature Converter</h3>
<div id="nextlink"></div><!--nextlink - DHTML loaded -->
<div class="main">
<p>Enter a temperature into the form below and when you submit it the
temperature will be displayed in all three units.</p>
<p><form method="post" action="$ENV{'SCRIPT_NAME'}">
<input type="text" name="intemp" size="5" value="" /> Enter a temperature...
<br /><select name="inunit">
  <option value="C" selected="selected">Celsius</option>
  <option value="F">Fahrenheit</option><option value="K">Kelvins</option>
  </select> Specify the unit of temperature...
<br /><input type="submit" value="Calculate temperatures..." class="button"
  /></form></p>
<table>
<tr><th>Centigrade</th><th>Fahrenheit</th><th>Kelvin</th></tr>
<tr><td>$outtempc</td><td>$outtempf</td><td>$outtempk</td></tr>
</table>
</div><!--main-->
<hr />
<p>Ok, that's the program demo, now click here to return to the <a
  href="$returnurl" title="Return to the documentation for this demo..."
  >documentation page</a> for this demo program...
- <a href="$sourceurl" title="View the source code for this program..."
  >View source code...</a>
- <a href="$pmsourceurl" title="View the perl module source code for this program..."
  >View module source code...</a></p>
<script type="text/javascript">doRelatedLinks('cgistyle');//</script>
</div><!--content-->
HERE
  &do_bottom;
#  END PROGRAM

#  SUB-ROUTINES
sub conv_temp
  {my ($in,$un) = @_;
  unless(defined($in)) {$in = 0;}
  if($un eq "f2c")
    {unless($in) {$in = 32;}
    return ($in - 32) * (5/9);
    }
  elsif($un eq "k2c")
    {unless($in) {$in = 273.15;}
    return $in - 273.15;
    }
  elsif($un eq "c2f")
    {unless($in) {$in = 0;}
    return ($in * (9/5)) + 32;
    }
  elsif($un eq "c2k")
    {unless($in) {$in = 0;}
    return $in + 273.15;
    }
  else {return $in;}
  }

#  DO_PATHBAR:    Outputs the path bar

sub do_pathbar
  {print<<HERE;
<div id="pathbar"><script type="text/javascript">drawPathBar(
  'tech','/tech/tech.shtml','To the Technical pages index...',
  'CGI','/tech/cgi/cgi_index.shtml','To the Perl CGI scripting index...',
  'styling','/tech/cgi/cgi_style.shtml','Making CGI pages match your site...',
  'example3');//</script></div><!--pathbar-->
HERE
  }

#  DO_TOP:  Outputs the topmost part of the webpage
#  Note! </head>-<body> boundary kept here so that extra program specific
#  code can be inserted into the page output.


sub do_top
  {do_std_head(\%G);
  print "</head>\n<body>\n";
  do_std_header(\%G);
  }


#  DO_BOTTOM:  Outputs the bottom section of the webpage
#  Includes 'exit' line as implicit end of processing.

sub do_bottom
  {do_std_bottom($G{'footerssi'});
  exit;
  }


#  EOF