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.
http://genomics.mbl.edu/~username/test_prog.pl
into the location bar of your browswer.
Internal Server ErrorThe server encountered an internal error or misconfiguration and was unable to complete your request. |
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.
|
|
|---|