CECS 5100

Survey of Educational Computer Languages

Fall 2003

Need a free MP3 Player to listen to these clips ?


Audio Segments and Illustrations #4
  • Introduction
    Audio (575k 1:13)
    1. Midterm Review
    2. Assignment 3a review
    3. CGI (Common gateway Interface) GET and PUT

  • MidTerm Review
    Audio (1.5M 3:29)

  • Relative and Absolute File References as it relates to PERL and HTML
    Audio (960k 2:03)
    Relative: open (GRADES, "grades") or die "Can't open grades: $!\n";
    Absolute: open (GRADES, "/home/gjones/grades") or die "Can't open grades: $!\n";

    Relative: <a href="index.html"></a>
    Absolute: <a href="http://saturn.cecs.unt.edu/~gjones/index.html"></a>

  • Common Gateway Interface (CGI)
    Audio (2.2M 4:54)

  • GET and POST methods of sending from a browser to a CGI
    Audio (5.1M 11:16)

    • GET:
      http://saturn.cecs.unt.edu/cgi-bin/gjones/example1.cgi?varname=value&
      varname2=value+value2&varname3=value%30
        Can only use the:
        1. A-Z,
        2. a-z,
        3. 0-9,
        4. ? (begins the GET),
        5. & (delimiter of value pairs),
        6. + (space),
        7. %## (extended ascii)

    • Example of CGI $ENV variables:

      #!/usr/bin/perl
      
      # example1.cgi
      
      # Print out a content-type for HTTP/1.0 compatibility
      print "Content-type: text/html\n\n";
      
      print "<html><body>
      
      About this Server<p>
      
      <pre>
      Server Name:        $ENV{SERVER_NAME}
      Listening on Port:  $ENV{SERVER_PORT}
      Server Software:    $ENV{SERVER_SOFTWARE}
      Server Protocol:    $ENV{SERVER_PROTOCOL}
      CGI Version:        $ENV{GATEWAY_INTERFACE}
      </pre>
      
      CONTENT_LENGTH = '$ENV{'CONTENT_LENGTH'}' 

      QUERY STRING = $ENV{'QUERY_STRING'}

      </body></html>";

    • POST:
      form.html
      
      <html>
      <body>
      
      <h2>Assignment 3b (Your Name Here)</h2>
      <form action="http://saturn.cecs.unt.edu/cgi-bin/gjones/example1.cgi" method="POST">
          Name: <INPUT NAME="name" TYPE="TEXT" SIZE="40">
      <P>
      <b>Thank you!</b> <INPUT TYPE="SUBMIT" value="Submit Request"> 
      </FORM>
      
      
      </body>
      </html>
      
      

  • example2.cgi: getting data sent by GET or POST

    #!/usr/bin/perl
    
    # Print out a content-type for HTTP/1.0 compatibility
    print "Content-type: text/html\n\n";
    print "<html><body>";
    
    # Determine where the info is coming from
      if ($ENV{'REQUEST_METHOD'} eq "POST") {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
      } else {
        $buffer = $ENV{'QUERY_STRING'};
      }
    
    # Split the name-value pairs
    @pairs = split(/&/, $buffer);
    
    foreach $pair (@pairs)
    {
        ($name, $value) = split(/=/, $pair);
    
        # Un-Webify plus signs and %-encoding
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ s/~!/ ~!/g;
    
        # Uncomment for debugging purposes
        print "Setting $name to $value<P>";
    
        $FORM{$name} = $value;
    }
    
    
    

  • Example of reading then outputting form information:

    #!/usr/bin/perl
    
    # Print out a content-type for HTTP/1.0 compatibility
    print "Content-type: text/html\n\n";
    
    # Determine where the info is coming from
      if ($ENV{'REQUEST_METHOD'} eq "POST") {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
      } else {
        $buffer = $ENV{'QUERY_STRING'};
      }
    
    # Split the name-value pairs
    @pairs = split(/&/, $buffer);
    
    foreach $pair (@pairs)
    {
        ($name, $value) = split(/=/, $pair);
    
        # Un-Webify plus signs and %-encoding
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ s/~!/ ~!/g;
    
        # Uncomment for debugging purposes
        #print "Setting $name to '$value'<P>";
    
        $FORM{$name} = $value;
    }
    
    
    print "<html><body>";
    print "<h2>Example use of Hash from Form</h2>";
    print "<pre>";
    $fullname = join (" ",$FORM{'first'},$FORM{'MI'},$FORM{'surname'});
    print "Name:         $fullname<p>";
    print "</pre>";
    
    print "
    <form action=\"http://saturn.cecs.unt.edu/cgi-bin/gjones/greg.cgi\" method=\"POST\">
    <P><b>Please enter your name:</b>
        First name: <INPUT NAME=\"first\" TYPE=\"TEXT\" SIZE=\"12\" MAXLENGTH=\"20\">
    </form>
    ";
    
    print "</body></html>";
    
    

  • Examples of Split and Hash:

    #!/usr/bin/perl
    
    print "Content-type: text/html\n\n";
    print "<html><body>\n";
    
    # You store the string into the hash array $FORM using the index value of 'test'.
    $FORM{'test'} = "Dr Greg Jones University of North Texas";
    
    # The first split uses assigned variables to place the values from the string
    ($a, $b, $c, $d, $e, $f, $g) = split (" ", $FORM{'test'});
    
    # this print statement prints out the assigned variable set above
    print "'$a', '$b', '$c', '$d', '$e', '$f', '$g' <p>\n";
    
    
    # Instead of assigning each variable as above,
    # you use the @tests method to place them in their own HASH array
    @tests = split (" ", $FORM{'test'});
    
    # use the foreach loop to walk through each assigned values in the hash array tests
    foreach $test (@tests) {
            print "'$test', ";
    }
    
    print "<p>\n";
    


    Copyright 2003, Dr. James G Jones