Addition using Perl/CGI in Ubuntu

CGI Perl

If Apache web server has installed in your system, you can run Perl/CGI script successfully. Create a HTML file and Perl script using following steps.

Step 1: Creating a web page add.html

1. Open your console and type:

$ sudo gedit /var/www/add.html

2. Copy the lines below in the file and save it.

<html>
  <head>
    <title>Addition of two numbers.</title>
  </head>
  <body>
    <br><br><br><br><br>
    <form action='cgi-bin/add.pl' method='post' enctype='multipart/form_data'>
      <table border='0' align='center'>
        <tr><td colspan='2' align='center'>
          <b>Adding Two Numbers</b>
        </td></tr>
        <tr>
          <td>Enter the first value:</td>
          <td><INPUT TYPE ='text' NAME='n1'></td>
        </tr>
        <tr>
          <td>Enter the second value:</td>
          <td><INPUT TYPE ='text' NAME='n2'></td>
        </tr>
        <tr><td colspan='2' align='center'>
          <INPUT TYPE ='submit' VALUE='ADD' NAME='submit'>
        </td></tr>
      </table>
    </form>
  </body>
</html>

Step 2: Creating a perl script add.pl

1. Open your console and type:

$ sudo gedit /usr/lib/cgi-bin/add.pl
$ sudo chmod a+x /usr/lib/cgi-bin/add.pl

The command chmod a+x is used to allow permission to run the script. Without using this command your program will not run. !!!

2. Copy the lines below in the file and save it.

#!/usr/bin/perl -w
use CGI;
CGI::ReadParse();
$numb1 = $in{'n1'};
chomp($numb1);
$numb2 = $in{'n2'};
chomp($numb2);
$numb3 = $numb1 + $numb2;
CGI::PrintHeader();
print "content-type:text/html\n\n";
print "<HTML>\n";
print "<head><title>Addition of two numbers</title></head>\n";
print "<body>\n";
print "<br><br><br><br><br><center>Addition of $numb1 and $numb2 is $numb3.";
print "</center>\n";
print "</body>\n";
print "</html>\n";

Step 3: Running the program

Open web browser and hit address http://localhost/add.html to run the Perl/CGI program.

CGI HTML

Comments

Most Popular Posts

TNEB Bill Calculator

TNEB Bill Calculator (New)

Technical Questions