Creating HTML using perl

Now we are going to write a perl program that works in your web browser to create html.

One note about the program we are creating. The line in the code "use CGI ...etc" is calling a package CGI has some useful functions. One thing that CGI does is supply a function called header which prints some information that makes the program output readable to a web browswer.

Step 1: Create a file that contains the following:

#!/usr/bin/perl
  
use strict;
use CGI qw/:standard/;
print header;

my($str);

$str = "This program intended to test if its possible for users\n";
$str .= "to install a program in cgi-bin and execute them to create\n";
$str .= "a web page.\n";

print "<HTML>\n";
print "<pre>\n";
print $str;

Step 2: Put the program into the directory in your user area called public_html.
Step 3: Permissions may be an issue. Run "chmod a+rwx" on the program.
Step 4: You can have your web browser launch this program by putting:

    http://genomics.mbl.edu/~username/test_prog.pl

into the location bar of your browswer.
Debugging. Its not uncommon for a broken script in your browswer to produce the text:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
Which is always a little intimidating. To avoid this problem, your programs have to be able to run successfully on the command line before you can expect them to work in cgi-bin. Get them to cooperate on the command line first, and then see what happens when you try to run them in the browser.

Another program

We're going to try another program. This time it gets some input that is entered into the location bar of the web browser. When we invoke the program in the web browser by typing:
 

       http://genomics.mbl.edu/~username/test_prog2.pl?word=blink

into the location bar, the program will recieve information for the variable "word".

Here's the program:

#!/usr/bin/perl

use strict;
use CGI qw/:standard/;
print header;

my($str);

$str = "This program intended to test if its possible for users\n";
$str .= "to install a program in cgi-bin and execute them to create\n";
$str .= "a web page.\n";

print "<HTML>\n";
print "<pre>\n";
print $str;

print "In this case we'd like to see that parameters are coming in from\n";
print "the location bar. The word you supplied is:" . param('word') . "\n";

How it works: 
The package CGI has some useful functions, one of them is called "param" which will give you information supplied after the "?" character of the URL.

Opening a file.

It is very common that when you're presenting data to the web that you have to open a local file and then present information from that file in your browser. One more baby program we'll do will be to open a file in your web browswer. In this case, we're passing in the name of the file using the param function, and then opening the file using perl code that should be pretty familiar to you.
Give it a try...
#!/usr/bin/perl

use strict;
use CGI qw/:standard/;
print header;

my $file = param('file');

print "<HTML>\n";
print "<h2>Displaying the contents of this file: $file</H2>\n";

open(F_IN, $file) || print "was not able to open file\n";
while(<F_IN>) {
    print;
}
Remember that you have to have a file that has the same name passed into the script sitting in the same directory as your script.
left right