mwerner92 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to create a cgi perl script calculator but honestly have no idea where to start and can't get a hold of my teacher.. I have the following html code set up for my apache server saved in /var/www/html:

<html> <head> <title>A Simple Calculator </title> </head> <body> <h1> Welcome to the Training Center Calculator </h1> <form action=/cgi-bin/calc.pl method="get"> Variable 1: <input type="text" name="v1" size = 40 maxlength=40><p> Variable 2: <input type="text" name="v2" size = 40 maxlength=40><p> Choose an action: <br> <input type="radio" name=oper value="Add">Addition<br> <input type="radio" name=oper value="Sub">Subtraction<br> <input type="radio" name=oper value="Multiply">Multiply<br> <input type="radio" name=oper value="Divide">Divide<br> <input type="radio" name=oper value="Exponent">Exponent<br> <p> <input type="submit" value="Submit form"> <input type="Reset" value="Clear all fields"> </body> </html>

I'm just trying to get an idea of where to start. This is all I have for the perl script at the moment:

#!/usr/bin/perl require("cgi-lib.pl"); &ReadParse(*input); print "Content-type: text/html\n\n"; $na = param('v1'); $nb = param('v2'); $oper = param('oper'); $nc; $nd; $expo; $answ; if ($oper == "addition") { $answ = $na + $nb; $nd = "+";

any tips on where to go from here with elsifs and make sure it ties into the html form right, would be greatly appreciated!

Replies are listed 'Best First'.
Re: CGI calculator
by GrandFather (Saint) on Jul 07, 2016 at 02:38 UTC

    Note too that you can test your CGI script from the command line passing the CGI parameters on the command line to the script:

    #!/usr/bin/perl use strict; use warnings; use CGI qw(); my $na = CGI::param('v1'); my $nb = CGI::param('v2'); my $oper = CGI::param('oper'); print CGI::header(); print "<p>$na $oper $nb</p>\n";

    using a command line of v1=1 v2=2 oper="*" prints:

    Content-Type: text/html; charset=ISO-8859-1 <p>1 * 2</p>
    Premature optimization is the root of all job security
Re: CGI calculator
by hippo (Archbishop) on Jul 07, 2016 at 08:05 UTC
    #!/usr/bin/perl require("cgi-lib.pl"); &ReadParse(*input);

    That approach was considered old hat 15 years ago - have you been reading Matt's Script Archive or similar?. I suggest you lose those perl4-isms and instead look to CPAN for eg. CGI, CGI::Lite or CGI::Simple to get you started. Don't prepend your subroutine calls with an "&" and always use strict and warnings. Now would also be an excellent time to become familiar with taint mode.

Re: CGI calculator
by duyet (Friar) on Jul 07, 2016 at 10:03 UTC
    Once you have a working script, i think you still need to configure your apache server to run perl script as it's not set up by default. See how do i configure apache webserver to run perl scripts, or hit google for more ...

    Your code won't work:
    1. missing closing }
    2. your HTML is sending "Add" as value to the script, and you are testing on "addition" which will never match. I think you can just send the operators in the values, eg. +, -, *, /, **, and just use it for calculation. Then you don't have to test on $oper. You do need to test if it is one of the expected operator
    Also your HTML is incorrect, missing closing </form> tag
Re: CGI calculator
by Anonymous Monk on Jul 07, 2016 at 01:47 UTC
    start with perlintro, first write a program to do addition .... without any hint of CGI stuff, get that working first, the beginning