#!/usr/bin/perl use bignum; #============================================================================= # Get POSTed parameters. # Read stadard input (POST) to pick up the parameters (key-value pairs). # Should be on a single line, but loop over lines, just in case. my %param; while (<>) { # Tokenize, splitting on "&". my @kv = split("&", $_); # key = value # Put into a hash (a "map" in Java-speak), for easy access. foreach my $pair (@kv) { $pair =~ /(.*)=(.*)/; $param{$1} = $2; } } #============================================================================= # HTTP Response #----------------------------------------------------------------------------- # Header print "Content-Type: text/html\n"; print "\n"; #============================================================================= # Payload #----------------------------------------------------------------------------- # HTML start print << "START"; Parke's Great Calculator! START #----------------------------------------------------------------------------- # Computation # Print the answer. if ($param{"op"} eq "none") { print "No operator specified!\n"; } else { if ($param{"op"} eq "add") { print $param{"one"}, " + ", $param{"two"}, " = "; print 1->bmul($param{"one"}) + 1->bmul($param{"two"}); } elsif ($param{"op"} eq "mult") { print $param{"one"}, " × ", $param{"two"}, " = "; print 1->bmul($param{"one"}) * 1->bmul($param{"two"}); } elsif ($param{"op"} eq "power") { use bignum; print "", $param{"one"}, "", $param{"two"}, " = "; print 1->bmul($param{"one"})** 1->bmul($param{"two"}); } else { print "Operator ", $param{"op"}, " not recognized!\n"; } print "\n"; } #----------------------------------------------------------------------------- # HTML finish print << "FINISH"; FINISH