Perl lab 2

Document Sample
Perl lab 2 Powered By Docstoc
					Perl lab 2.
Using perl for cgi-bin: You do this anytime you access a perl application using a url to
your running apache server.

To convert a perl program into a cgi-bin program:
   1. Remove any user-input, put it in CGI-BIN (or wherever you did it from before)
      and see if you can run it from apache. (We did this a few weeks ago).

   2. Next, try to pass information to your process from a form. This will be in the
      form of query string values made available to a running perl program from
      environment variables.

    3. Processing parameters depends whether you have access to a perl package called Here is a link to the text‟s old perl cgi powerpoint. I have examples in
        my perl powerpoints as well.
Without assuming get method:
#in perl
$querystring= $ENV{„QUERY_STRING‟};
#now you have something like “name=Bob&number=7” in $querystring

# A CGI program
use CGI ":standard";

# First produce the header part of the HTML return value

print header();
print start_html("CGI-Perl My App, using");

# Set local variables to the parameter values

my($name, $number) =
     (param("name"), param("number")); #or whatever
# Produce the result information to the browser and finish the page

print "<h3>Stuff goes here using html tags:</h3>\n",

#and so on
print end_html();

   4. Build a form using HTML. Provide one or two text fields and a submit button.
      Supply an action attribute to your form that specifies a perl (.pl or .cgi ) program
      and method =”get” For example
<form action = “http://localhost/cgi-bin/” method=”get”>

Construct a “business site” with two perl programs and two html forms as described

   1. Build a (html) page which has two anchor links, one for a login form and one for
      an order form (see item 2 below). To handle user login, provide an html form
      with two fields - one for text and one for password - as well as a submit button.
      The submit button (action) runs a perl program which <HARDER> reads names
      and passwords from a file <or EASIER constructs a literal hash inline> and
      compares these valid user/pw pairs with the values passed in the query string to
      see if this user is valid. If so, send a cookie (Valid=true, for example) otherwise
      send no cookie or a cookie (Valid=false, for example). You are welcome to use
      session instead of cookies. You should also return/rebuild to a page like the
      original one (with the two links) but with a message at the top about login status.
   2. Build an order form - you can use the popcorn sales example shown in the text
      and class powerpoints or something else. Provide a perl program to do the
      processing. Make the following modification, though: Check for a cookie on
      form submit (“valid user”) before processing the order and reject this order if the
      user has no cookie or an invalid login status.