CGI Example #4

Temperature Converter v4

program environment initialisation routines out-sourced to dwd.pm

Return to the notes... - Run the program - View dwd.pm (v4) module code - View dwd.conf external config file
New additions are highlighted...

#!/usr/local/bin/perl -w
#
#  cgi_demo4.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.
#  Separates program environment from program function,
#  It now uses shared routines for initialisation
#
################################################################################
use strict;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);
use dwd;

#  INITIALISE PROGRAM ENVIRONMENT
my %G;      #  Stores global values within the program.
my %arg;    #  Stores the CGI form/URL parameters
my @parameters  = qw(inunit intemp);
initprogenv(\%G,\%arg,\@parameters);


#  DEFINE PROGRAM SPECIFIC VALUES
$G{'title'}      = "$G{'title'} Styled CGI Demo...";
$G{'returnurl'}    = "/tech/cgi/cgi_environment.shtml#notes";
$G{'sourceurl'}    = "/tech/cgi/cgidemo_source4.html";
$G{'pmsourceurl'}  = "/tech/cgi/dwdpm_source4.html";


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

#  PERFORM REQUIRED CALCULATION
if($arg{'inunit'} eq "F")
  {$outtempf  = $arg{'intemp'};
  $outtempc  = &conv_temp($arg{'intemp'},"f2c");
  $outtempk  = &conv_temp($outtempc,"c2k");
  }
elsif($arg{'inunit'} eq "K")
  {$outtempk  = $arg{'intemp'};
  $outtempc  = &conv_temp($arg{'intemp'},"k2c");
  $outtempf  = &conv_temp($outtempc,"c2f");
  }
else
  {$outtempc  = $arg{'intemp'};
  $outtempk  = &conv_temp($arg{'intemp'},"c2k");
  $outtempf  = &conv_temp($arg{'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="$G{'progname'}">
<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="$G{'returnurl'}" title="Return to the documentation for this demo..."
  >documentation page</a> for this demo program...
- <a href="$G{'sourceurl'}" title="View the source code for this program..."
  >View source code...</a>
- <a href="$G{'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
#  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;}

#  DO_PATHBAR:    Outputs the path bar section
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...',
  'example4');//</script></div><!--pathbar-->
HERE
  }

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;}
  }

#  EOF