Assignment 12.doc

advertisement
CST 334
Assignment 12
Perl/CGI Scripting
Due: Last day of class
Steve Litt's PERLs of Wisdom:
Hello World in Perl
Copyright (C) 1998 by Steve Litt
http://www.troubleshooters.com/codecorn/littperl/index.htm
Step 1 Hello.pl
1.1) In your home directory, make a directory called public_html. Then, go in this
directory and make a file called hello.pl with this one line:
print "Hello World\n";
Make sure the PERL executable is on your path.
1.2) Now type the following command at your command prompt:
perl hello.pl
If everything's OK, you should get the following output:
Hello World
Step 2 Hello.cgi
2.1) make a new file called hello.cgi with these lines of code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<H1>Hello World</H1>\n";
Note that the top line is the path to perl on your unix host.
2.2) Now type the following command at your command prompt:
perl hello.cgi
If everything's OK, you should get the following output:
Content-type: text/html
<H1>Hello World</H1>
CST 334
Assignment 12
Perl/CGI Scripting
Due: Last day of class
2.3) Next, execute this command:
perl hello.cgi > junk.htm
Now open file junk.htm in your browser. (http://mlc104.csumb.edu/~USER/junk.htm,
where USER is your user name on mlc104)
You should get Hello World in very large letters. If not, troubleshoot.
2.4) Make hello.cgi executable. chmod a+x hello.cgi should do it. Now, type this
command:
perl ./hello.cgi
Your output should look like this:
Content-type: text/html
<H1>Hello World</H1>
If it doesn't, check to see if the system administrator has pathed you correctly for perl.
Try this:
perl -v
Note the output it produces, and call the sysadmin.
2.5) Once you get the perl ./hello.cgi command to work correctly, try this:
./hello.cgi
Once again, you should get the same output as in step 2.4
If you don't, use the command which perl to see where perl resides on the machine, and
change the top line of hello.cgi accordingly. If that doesn't fix it, troubleshoot.
2.6) Once you get the right output typing just the filename, create a new file hello.htm
in the public_html directory:
Content-type: text/html
<P><A HREF="hello.cgi">RUN hello.cgi</A>.</P>
Now pull up hello.htm in your browser, click on the hyperlink that says "RUN hello.cgi.
If everything's cool it should come back with a screen saying Hello World in large letters.
Otherwise, troubleshoot.
CST 334
Assignment 12
Perl/CGI Scripting
Due: Last day of class
Step 3 HelloForm.cgi
3.1) Create HelloForm.cgi in a directory on the server, as follows:
HelloForm.cgi
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "Hello World.\n";
print "Heres the form info:<P>\n";
my($buffer);
my(@pairs);
my($pair);
#path to perl
#so output produces web page
#we believe in tradition
#various declarations
#POST method uses stdin
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'}); #Control/value pairs &
@pairs = split(/&/, $buffer);
delimited
foreach $pair (@pairs)
#print each ctrl/value pair
{
print "$pair<BR>\n"
}
print "<P>Note that further parsing is\n";
print "necessary to turn the plus signs\n";
print "into spaces and get rid of some\n";
print "other web encoding.\n";
3.2) Now make it executable with chmod:
chmod a+x HelloForm.cgi
3.3) Now add the following lines to your hello.htm file in the public_html directory
(same directory as HelloForm.cgi):
<form action="FormHello.cgi" method="POST">
<P>Text: <input type="text" size="20" name="TextLine">
<input type="submit" name="Button" value="Submit"></P>
</form>
3.4) Access the hello.htm web page, fill in the text line, click the button, and you'll see
the results come up.
Copyright (C)1998 by Steve Litt -- Legal
Download