lab 1 print

Upload: celvy

Post on 06-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Lab 1 Print

    1/14

    PseudocodeDefine the getFormData() function.

    Initialize a buffer.

    If the GET method is used, copy the form information into the buffer.

    If the POST method is used, read the form information into the buffer.

    Listing 20.1-20LST01.PL - The First Step is to Get the Form Information.

    sub getFormData {

    my($buffer) = "";

    if ($ENV{'REQUEST_METHOD'} eq 'GET') {

    $buffer = $ENV{'QUERY_STRING'};

    }

    else {

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

    }

    }

    PseudocodeDeclare a hash variable to hold the form's input fields.

    Call the getFormData() function.

    Define the getFormData() function.

    Declare a local variable to hold the reference to the input field hash.

    Initialize a buffer.

    If the GET method is used, copy the form information into the buffer.

    If the POST method is used, read the form information into the buffer.

  • 8/3/2019 Lab 1 Print

    2/14

    Iterate over the array returned by the split() function.

    Decode both the input field name and value.

    Create an entry in the input field hash variable.

    Define the decodeURL() function.

    Get the encoded string from the parameter array.

    Translate all plus signs into spaces.

    Convert character coded as hexadecimal digits into regular characters.

    Return the decoded string.

    Listing 20.2-20LST02.PL - The First Step is to Get the Form Information.

    my(%frmFlds);

    getFormData(\%frmFlds);

    sub getFormData {

    my($hashRef) = shift;

    my($buffer) = "";

    if ($ENV{'REQUEST_METHOD'} eq 'GET') {

    $buffer = $ENV{'QUERY_STRING'};

    }

    else {

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

    }

    foreach (split(/&/, $buffer)) {

  • 8/3/2019 Lab 1 Print

    3/14

    my($key, $value) = split(/=/, $_);

    $key = decodeURL($key);

    $value = decodeURL($value);

    %{$hashRef}->{$key} = $value;

    }

    }

    sub decodeURL {

    $_ = shift;

    tr/+/ /;

    s/%(..)/pack('c', hex($1))/eg;

    return($_);

    }

    PseudocodeDeclare a hash variable to hold the form's input fields.

    Call the getFormData() function.

    Define the getFormData() function.

    Declare a local variable to hold the reference to the input field hash.

    Initialize a buffer.

    If the GET method is used, copy the form information into the buffer.

    If the POST method is used, read the form information into the buffer.

    Iterate over the array returned by the split() function.

    Decode both the input field name and value.

    Compress multiple

    tags into one.

    Convert < into < and > into > stopping HTML tags from interpretation.

    Turn back on the bold and italic HTML tags.

  • 8/3/2019 Lab 1 Print

    4/14

    Remove unneded carriage returns.

    Convert two newlines into a HTML paragraph tag.

    Convert single newlines into spaces.

    Create an entry in the input field hash variable.

    Define the decodeURL() function.

    Get the encoded string from the parameter array.

    Translate all plus signs into spaces.

    Convert character coded as hexadecimal digits into regular characters.

    Return the decoded string.

    Listing 20.3-20LST03.PL - The First Step is to Get the Form Information.

    my(%frmFlds);

    getFormData(\%frmFlds);

    sub getFormData {

    my($hashRef) = shift;

    my($buffer) = "";

    if ($ENV{'REQUEST_METHOD'} eq 'GET') {

    $buffer = $ENV{'QUERY_STRING'};

    }

    else {

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

    }

  • 8/3/2019 Lab 1 Print

    5/14

    foreach (split(/&/, $buffer)) {

    my($key, $value) = split(/=/, $_);

    $key = decodeURL($key);

    $value = decodeURL($value);

    $value =~ s/(

    \s*)+/

    /g; # compress multiple

    tags.

    $value =~ s//>/g;

    $value =~ s///ig; # turn on the bold tag.

    $value =~ s!!!ig;

    $value =~ s///ig; # turn on the italic tag.

    $value =~ s!!!ig;

    $value =~ s!\cM!!g; # Remove unneeded carriage returns.

    $value =~ s!\n\n!

    !g; # Convert 2 newlines into paragraph.

    $value =~ s!\n! !g; # Convert newline into spaces.

    %{$hashRef}->{$key} = $value;

    }

    }

    sub decodeURL {

    $_ = shift;

    tr/+/ /;

    s/%(..)/pack('c', hex($1))/eg;

    return($_);

    }

  • 8/3/2019 Lab 1 Print

    6/14

    PseudocodeStart the HTML web page.

    Define the web page header which holds the title.

    Start the body of the page.

    Display a header.

    Display some instructions.

    Start a HTML form.

    Start a HTML table.

    Each row of the table is another input field.

    Define the submit button.

    End the table.

    End the Form.

    End the body of the page.

    End the page.

    Listing 20.4-ADDGEST.htm - The Add Entry to Guest book HTML Form

    Add to our Guestbook

    Add to our Guestbook

    Fill in the blanks below to add to our Guestbook. The only fields that you

    have to fill in are the comments and name section. Thanks!

  • 8/3/2019 Lab 1 Print

    7/14

    Your Name:

    Email:

    Comments:

    The only thing you might need to change in order for this form to work is the ACTION modifier

    in the tag. The directory where you place the CGI program might not be /cgi-bin. The

    addgest.htm file will generate a web page that looks like the following figure.

    Fig. 20.2 - The Add Entry Form

    The CGI program in Listing 20.5 is invoked when a visitor clicks on the submit button of the

    Add Entry HTML form. This program will process the form information, save it to a data fileand then create a web page to display all of the entries in the data file.

    PseudocodeTurn on the warning option.

    Turn on the strict pragma.

  • 8/3/2019 Lab 1 Print

    8/14

    Declare a hash variable to hold the HTML form field data.

    Get the local time and pretend that it is one of the form fields.

    Get the data from the form.

    Save the data into a file.

    Send the HTTP header to the remote web browser.

    Send the start of page and header information.

    Send the heading and request a horizontal line.

    Call the readFormData() function to display the Guest book entries.

    End the web page.

    Define the getFormData() function.

    Declare a local variable to hold the reference to the input field hash.

    Initialize a buffer.

    If the GET method is used, copy the form information into the buffer.

    If the POST method is used, read the form information into the buffer.

    Iterate over the array returned by the split() function.

    Decode both the input field name and value.

    Compress multiple

    tags into one.

    Convert < into < and > into > stopping HTML tags from interpretation.

    Turn back on the bold and italic HTML tags.

    Remove unneded carriage returns.

    Convert two newlines into a HTML paragraph tag.

    Convert single newlines into spaces.

    Create an entry in the input field hash variable.

    Define the decodeURL() function.

    Get the encoded string from the parameter array.

  • 8/3/2019 Lab 1 Print

    9/14

    Translate all plus signs into spaces.

    Convert character coded as hexadecimal digits into regular characters.

    Return the decoded string.

    Define the zeroFill() function - turns "1" into "01".

    Declare a local variable to hold the number to be filled.

    Declare a local variable to hold the string length that is needed.

    Find difference between current string length and needed length.

    If the string is big enough (like "12") then return it.

    If the string is too big, prefix it with some zeroes.

    Define the saveFormData() function.

    Declare two local variables to hold the hash and file name.

    Open the file for appending.

    Store the contents of the hash in the data file.

    Close the file.

    Define the readFormData() function.

    Declare a local variable to hold the file name.

    Open the file for reading.

    Iterate over the lines of the file.

    Split the line into four variables using ~ as demlimiter.

    Print the Guest book entry using a minimal amount of HTML tags.

    Use a horizontal rule to separate entries.

    Close the file.Listing 20.5-20LST05.PL - A CGI Program to Add a Guest book Entry and

    Display a Guest book HTML Page

    #! /user/bin/perl -w

  • 8/3/2019 Lab 1 Print

    10/14

    use strict;

    my(%fields);

    my($sec, $min, $hour, $mday, $mon, $year) = (localtime(time))[0..5];

    my($dataFile) = "data/gestbook.dat";

    $mon = zeroFill($mon, 2);

    $hour = zeroFill($hour, 2);

    $min = zeroFill($min, 2);

    $sec = zeroFill($sec, 2);

    $fields{'timestamp'} = "$mon/$mday/$year, $hour:$min:sec";

    getFormData(\%fields);

    saveFormData(\%fields, $dataFile);

    print("Content-type: text/html\n\n");

    print("\n");

    print("Guestbook\n");

    print("Guestbook\n");

    print("\n");

    readFormData($dataFile);

    print("\n");

    print("\n");

    sub getFormData {

  • 8/3/2019 Lab 1 Print

    11/14

    my($hashRef) = shift;

    my($buffer) = "";

    if ($ENV{'REQUEST_METHOD'} eq "GET") {

    $buffer = $ENV{'QUERY_STRING'};

    }

    else {

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

    }

    foreach (split(/&/, $buffer)) {

    my($key, $value) = split(/=/, $_);

    $key = decodeURL($key);

    $value = decodeURL($value);

    $value =~ s/(

    \s*)+/

    /g; # compress multiple

    tags.

    $value =~ s//>/g;

    $value =~ s///ig; # turn on the bold tag.

    $value =~ s!!!ig;

    $value =~ s///ig; # turn on the italic tag.

    $value =~ s!!!ig;

    $value =~ s!\cM!!g; # Remove unneeded carriage returns.

    $value =~ s!\n\n!

    !g; # Convert 2 newlines into paragraph.

    $value =~ s!\n! !g; # convert newline into space.

  • 8/3/2019 Lab 1 Print

    12/14

    %{$hashRef}->{$key} = $value;

    }

    $fields{'comments'} =~ s!\cM!!g;

    $fields{'comments'} =~ s!\n\n!

    !g;

    $fields{'comments'} =~ s!\n!
    !g;

    }

    sub decodeURL {

    $_ = shift;

    tr/+/ /;

    s/%(..)/pack('c', hex($1))/eg;

    return($_);

    }

    sub zeroFill {

    my($temp) = shift;

    my($len) = shift;

    my($diff) = $len - length($temp);

    return($temp) if $diff

  • 8/3/2019 Lab 1 Print

    13/14

    sub saveFormData {

    my($hashRef) = shift;

    my($file) = shift;

    open(FILE, ">>$file") or die("Unable to open Guestbook data file.");

    print FILE ("$hashRef->{'timestamp'}~");

    print FILE ("$hashRef->{'name'}~");

    print FILE ("$hashRef->{'email'}~");

    print FILE ("$hashRef->{'comments'}");

    print FILE ("\n");

    close(FILE);

    }

    sub readFormData {

    my($file) = shift;

    open(FILE, "

  • 8/3/2019 Lab 1 Print

    14/14