Simple CGI Example #1

Temperature Converter v1

Return to the notes... - Run the program

001 #!perl -w
002 #
003 #  cgi_demo.pl    (c) Andy Belcher DandyWebDesign 2006
004 #
005 #  A simple web-page widget to convert temperatures and demonstrate simple
006 #  CGI forms. Takes a single input temperature and a unit (F, C or K) and
007 #  displays the equivalent in all three units.
008 #  NOTE!  This is a CGI demonstration example intended to show
009 #  different methods rather than an efficient piece of coding!
010 #
011 ######################################################################
012
013 #  Pull in required Perl modules
014 use strict;
015 use CGI qw(:all);
016 use CGI::Carp qw(fatalsToBrowser);
017
018 #  DECLARE VARIABLES
019 my $returnurl  = "/tech/cgi/cgi_simpledemo.shtml#notes";  #  LINK - RETURN PAGE
020 my $intemp;
021 my $inunit;
022
023 #  POPULATE OUTPUT SCALARS WITH DEFAULT VALUES FOR DISPLAY
024 my $outtempc  = 0;
025 my $outtempk  = 273.15;
026 my $outtempf  = 32;
027
028 #  READ PARAMETERS, FOR EACH PARAMETER USE THE 'param' FUNCTION (PART
029 #  OF THE CGI MODULE) TO EXTRACT THE ARGUMENTS FROM THE FORM.
030 #  NOTE THAT IF THE ARGUMENT IS NOT PRESENT $ARG{PARA} IS UNDEFINED
031 #  NOTE ALSO THAT THE ARGUMENT NAMES MATCH THE FIELD NAMES IN THE FORM
032 $intemp = param('intemp');
033 unless((defined($intemp)) && ($intemp)) {$intemp = 0;}
034 $inunit = param('inunit');
035 unless((defined($inunit)) && ($inunit)) {$inunit = "C";}
036
037 #  PERFORM REQUIRED CALCULATION
038 if($inunit eq "F")
039   {$outtempf  = $intemp;
040   $outtempc   = &conv_temp($intemp,"f2c");
041   $outtempk   = &conv_temp($outtempc,"c2k");
042   }
043 elsif($inunit eq "K")
044   {$outtempk  = $intemp;
045   $outtempc   = &conv_temp($intemp,"k2c");
046   $outtempf   = &conv_temp($outtempc,"c2f");
047   }
048 else
049   {$outtempc  = $intemp;
050   $outtempk   = &conv_temp($intemp,"c2k");
051   $outtempf   = &conv_temp($intemp,"c2f");
052   }
053
054 #  OUTPUT PAGE
055 print header;
056   #  NOTE! This MUST be the very first output else the program will fail.
057   #  'header' is a function from the CGI module which adds the required
058   #  HTTP headers to the output stream, if this does not get output first
059   #  an error will be returned "Malformed header from program..."
060   #
061 print<<HERE;
062 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
063   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
064 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
065 <head><title>Demo CGI Form Utility</title>
066 </head>
067 <body>
068 <h1>Perl CGI Demo Program : Temperature Converter</h1>
069 <p>Enter a temperature into the form below and when you submit it the
070 temperature will be displayed in all three units.</p>
071 <form method="post" action="$ENV{'SCRIPT_NAME'}">
072 <input type="text" name="intemp" size="5" value="" /> Enter a temperature...
073 <br /><select name="inunit">
074   <option value="C" selected="selected">Celsius</option>
075   <option value="F">Fahrenheit</option><option value="K">Kelvins</option>
076   </select> Specify the unit of temperature...
077 <br /><input type="submit" value="Calculate temperatures..." /></form>
078 <table>
079 <tr><th>Centigrade</th><th>Fahrenheit</th><th>Kelvin</th></tr>
080 <tr><td>$outtempc</td><td>$outtempf</td><td>$outtempk</td></tr>
081 </table>
082 <hr />
083 <p>Ok, that's the program demo, now click here to return to the <a
084   href="$returnurl"
085   title="Return to the documentation for this demo..."
086   >documentation page</a> for this demo program...</p>
087 </body>
088 </html>
089 HERE
090 exit;
091 #  END PROGRAM
092
093 #  SUB-ROUTINES
094 sub conv_temp
095   {my ($in,$un) = @_;
096   unless(defined($in)) {$in = 0;}
097   if($un eq "f2c")
098     {unless($in) {$in = 32;}
099     return ($in - 32) * (5/9);
100     }
101   elsif($un eq "k2c")
102     {unless($in) {$in = 273.15;}
103     return $in - 273.15;
104     }
105   elsif($un eq "c2f")
106     {unless($in) {$in = 0;}
107     return ($in * (9/5)) + 32;
108     }
109   elsif($un eq "c2k")
110     {unless($in) {$in = 0;}
111     return $in + 273.15;
112     }
113   else {return $in;}
114   }
115
116 #  EOF