Formatting added to match the rest of website...
Return to the notes... - Run the program
#!perl -w
#
# cgi_demo2.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.
#
################################################################################
# Pull in required Perl modules
use strict;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);
# DECLARE VARIABLES
my $returnurl = "/tech/cgi/cgi_style.shtml#notes";
my $sourceurl = "/tech/cgi/cgidemo_source2.html";
my $intemp;
my $inunit;
# 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_rightbar;
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></p>
<script type="text/javascript">doRelatedLinks('cgistyle');//</script>
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_top: Outputs the topmost part of the webpage
# Includes the required content-type HEADER line.
#
sub do_top
{print header;
print<<HERE;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Dandy Web Design : Techdocs: CGI Applications: Styled CGI Demo...</title>
HERE
&output_ssi("$ENV{'DOCUMENT_ROOT'}/ssi/head.ssi");
print<<HERE;
</head>
<body>
<div id="navbar">
HERE
&output_ssi("$ENV{'DOCUMENT_ROOT'}/ssi/navbar.ssi");
&output_ssi("$ENV{'DOCUMENT_ROOT'}/ssi/technavbar.ssi");
&output_ssi("$ENV{'DOCUMENT_ROOT'}/ssi/othnavbar.ssi");
print "</dl></div><!--navbar-->\n";
&output_ssi("$ENV{'DOCUMENT_ROOT'}/ssi/header.ssi");
}
#
# do_bottom: Outputs the bottom section of the webpage
# Includes 'exit' line as implicit end of processing
#
sub do_bottom
{print "</div><!--content-->\n";
&output_ssi("$ENV{'DOCUMENT_ROOT'}/ssi/footer.ssi");
print<<HERE;
</div><!--frame-->
</body>
</html>
HERE
exit;
}
#
# do_rightbar: Outputs the required right-bar
sub do_rightbar
{print "<div id=\"rightbar\">\n";
&output_ssi("$ENV{'DOCUMENT_ROOT'}/ssi/bespoke.ssi");
&output_ssi("$ENV{'DOCUMENT_ROOT'}/ssi/cginavlinks.ssi");
&output_ssi("$ENV{'DOCUMENT_ROOT'}/ssi/cgisrclinks.ssi");
print "</div><!--rightbar-->\n";
}
#
# 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...',
'example2');//</script>
</div><!--pathbar-->
HERE
}
#
# output_ssi: Output from CGI programs goes straight to the browser,
# it doesn't get parsed again by Apache and so server side includes
# (SSIs) cannot be used. This function provides SSI functionality to
# CGI scripts by writing the SSI file content into the output stream.
# The expected argument is the required SSI file path.
sub output_ssi
{my $file = $_[0];unless($file) {return;}
open(SSI,"$file") || die "Cannot open $file for read: $!";
while(<SSI>) {print $_;}
close(SSI) || die "Cannot close $file after read: $!";
}
# EOF