Using Html with Perl

This page helps you to get a Perl script to run from a web page, by providing minimal working examples, which you can copy, modify, and use.

There are many other web pages which aim to do that, generally written by more competent and better-informed programmers than me. But their sample code is longer than mine, in many cases much longer. It often uses unnecessary advanced techniques. In some cases, it uses a connection to an SQL database. In at least one case it uses cookies. In some cases, their code doesn't even work. I don't think that is helpful. If you want to teach someone to walk, don't try and teach them to fly at the same time.

I've made my examples minimal, so as not to ask too much of the user who wants to understand them. They're not intended as specimens of good code. I would never normally write a perl program with no "use strict"; nor an html page with no <html></html> tags. But here, my aim is to keep these code specimens very short, so as to make it easy for people to copy them and change them line by line to fulfil their own purposes, testing after each edit whether they still run.

First method

This is the simplest method. You have an html page with a form on it. If the user fills in the field(s) on the form and clicks the "Submit" button, it calls the Perl program, sending it the value(s) that the user supplied. Then the Perl program does some calculations (in this case, it doubles the number), and presents its answer.

A weakness of this method is the user left looking at the results; the page with the form has gone. In fact they can probably get back to it by clicking the "back" button on their browser; but that's not ideal.

Here's the code for this simple method.

Below is the html code.

Copy it, and replace "https://weddslist.com/cgi-bin/n.pl" by whatever URL your Perl program will be at.
Below is the Perl program.

You should probably put it within a suitable directory, with a name like https://yourdomain/cgi-bin/. You must change the "properties" of the copy you've uploaded to something like (octal) 0755, so that users will be able to have it executed.
<form method=GET
      action="https://weddslist.com/cgi-bin/n.pl">
Your number: <input NAME="yrnumber" type=TEXT>
<br>
<input name="Submit" TYPE=SUBMIT>
</form>
#!/usr/bin/perl -w
use CGI;
my $cgi=new CGI;
my $n = $cgi->param('yrnumber');
$n *= 2;
print $cgi->header();
print "<p>Twice your number is $n.</p>";

Here is a working example, that uses the code above.

Second method

This method is more complicated. The user sees an html page with a form on it, which though they may not realise it is the ouput of a Perl program. If they fills in the field(s) and clicks the "Submit" button, it calls itself, sending itself the value(s) that the user supplied. It does some calculations, and presents its answer.

This is more complicated, and harder to understand. As you can see, there is more than twice as much total code. But this method is widely used, as it is more intuitive for the user.

Here's the code for this one-file method:

Below is the merged code.

Copy it, and replace "https://weddslist.com/cgi-bin/n.pl" by whatever URL your Perl program will be at. As for the first method, you'll need to set the properties of the uploaded file.

Note that "ENTWINE" is an arbitrary label delimiting a slab of code to be treated as a single string.
#!/usr/bin/perl -w
use CGI;
my $cgi=new CGI;
my $n = $cgi->param('yrnumber');
my $nn = $n*2;
print $cgi->header();

print<<ENTWINE;
<form method=GET action="https://weddslist.com/cgi-bin/nn.pl">
Your number: <input NAME="yrnumber" type=TEXT value="$n" >
<input name="Submit" TYPE=SUBMIT></P>
</form>
<p>Twice your number is $nn</p>
ENTWINE

Here is a working example, that uses the code above.

weddslist.com