Test Gen - Simple Perl Snippets

0. testgen.pl generates a new page and sends it to the browser. (The file resides on the server and has permission set to "executable.")

 

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<HTML>";
print "<HEAD><TITLE>Test Gen</TITLE></HEAD>";
print "<BODY BGCOLOR=\"#FFFFFF\"><P>Test Gen</P></BODY>";
print "</HTML>";


1. testgen1.pl generates a new page using a variable. (This is another file on the server in a sequence intended to illustrate the available features of the Perl programming language.)

 

#!/usr/bin/perl
$theTitle = "Test Generator";

print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD><TITLE>$theTitle</TITLE></HEAD>\n";
print "<BODY><P>The title is <B>$theTitle</B></P></BODY>\n";
print "</HTML>";


2. testgen2.html is a webpage that passes values to testgen2.pl using a FORM

 

<HTML>
<HEAD><TITLE>Test Gen 2</TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF">
<P><FORM ACTION="testgen2.pl" METHOD=POST enctype="text/plain">
<P><INPUT TYPE=text NAME=Name VALUE="" SIZE=30>
<INPUT TYPE=submit NAME=Submit VALUE="Submit">
</FORM></P>
</BODY>
</HTML>

testgen2.pl - Notice that because testgen2.html (above) uses METHOD=POST, the Name value is passed to the variable via Standard Input.

 

#!/usr/bin/perl
$theTitle = <STDIN>;

print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD><TITLE>$theTitle</TITLE></HEAD>\n";
print "<BODY><P>The title is <B>$theTitle</B></P></BODY>\n";
print "</HTML>";


3. testgen3.html is the same as testgen2.html except that it calls testgen3.pl

testgen3.pl has "theHtml" as a subroutine, passing the FORM input via element zero of the arguments array.

 

#!/usr/bin/perl

sub theHtml {

$theTitle = $_[0];
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD><TITLE>$theTitle</TITLE></HEAD>\n";
print "<BODY><P>The title is <B>$theTitle</B></P></BODY>\n";
print "</HTML>";
}


theHtml(<STDIN>);


4. testgen4.html is the same as testgen3 except for calling testgen4.pl which calls the subroutine which is now located in a separate file.

html.pl contains the subroutine. Note that the file returns "true."

 

sub theHtml {

$theTitle = $_[0];
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD><TITLE>$theTitle</TITLE></HEAD>\n";
print "<BODY><P>The title is <B>$theTitle</B></P></BODY>\n";
print "</HTML>";
}

1;

testgen4.pl now simply requires the file and calls the subroutine.

 

#!/usr/bin/perl
require "html.pl";
theHtml(<STDIN>);


5. testgen5.html's FORM ACTION is the GET method.

 

<HTML>
<HEAD><TITLE>Test Gen 5</TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF">
<P><FORM ACTION="testgen5.pl" METHOD=GET enctype="text/plain">
<P><INPUT TYPE=text NAME=Name VALUE="" SIZE=30>
<INPUT TYPE=submit NAME=Submit VALUE="Submit">
</FORM></P>
</BODY>
</HTML>

testgen5.pl needs to get the argument list from an Environment Variable.

 

#!/usr/bin/perl
require "html.pl";
theHtml(
$ENV{'QUERY_STRING'});

Notice that the arguments are now passed directly to the script via the URL, e.g., http://www.zapmap.com/bryan/testgen5.pl?hello there

http://www.zapmap.com/bryan