CS 330 Class 9 Programming plan for today: More of how data gets into a script Via environment variables Via the url From a form By editing the url directly.

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.
PZ15A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ15A - The Internet Programming Language Design and.
Python and Web Programming
CS 898N – Advanced World Wide Web Technologies Lecture 10: Examples of PERL and CGI Chin-Chih Chang
COS 381 Day 22. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
Guide To UNIX Using Linux Third Edition
Outcomes Know what are CGI Environment Variables Know how to use environment variables How to process A simple Query Form Able to use URL Encoding rules.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
CSCI 6962: Server-side Design and Programming History and Background.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Regular Expression A regular expression is a template that either matches or doesn’t match a given string.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Practical Extraction & Report Language PERL Joseph Beltran.
Internet Forms and Database Bob Kisel Amgraf, Inc.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Web Server Design Week 14 Old Dominion University Department of Computer Science CS 495/595 Spring 2010 Martin Klein 4/14/10.
Chapter 6: Server-side Processing of Submitted Data Environment Variables -- A group of variables in which the Web server stores information about the.
USING PERL FOR CGI PROGRAMMING
CSU - DCE Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the.
Lecture 21 Common Gateway Interface CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Website Development with PHP and MySQL Saving Data.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
Chapter 9: Perl and CGI Programming CGI Programming Acknowledgement: Some materials are taken from Teach Yourself CGI Programming with PERL 5 in a Week.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Matching in list context (Chapter 11 = ($str =~ /pattern/); This stores the list of the special ($1, $2,…) capturing variables into the.
Perl: Lecture 2 Advanced RE & CGI. Regular Expressions 2.
Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Introduction to Programming the WWW I CMSC Winter 2003.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Sending data, forms and variables Please use speaker notes for additional information!
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong. Adminstrivia Homework 4 not yet graded …
Digital Text and Data Processing Tokenisation. Today’s class □ Tokenisation and creation of frequency lists □ Keyword in context lists □ Moretti and distant.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Interpolation Variable Interpolation, Backslash Interpolation.
CS 330 Class 8 Homework A pattern that contains a word with an optional period A pattern that contains Fred with a space (not Freddy) See regexp.txt guest4.htm.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
URL Encoding When data is entered on a Web page form, it must be encoded before it can be passed to some program for processing. Each element on the form.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
The Scripting Programming Language
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
Perl References arrays and hashes can only contain scalars (numbers and strings)‏ if we want something more complicated (like an array of arrays) we use.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
Perl: Practical Extraction & Reporting Language RL Schwartz, Learning Perl, RL Schwartz & L Wall, Programming Perl, O’Reilly & Associates.
Web Server Design Week 13 Old Dominion University Department of Computer Science CS 495/595 Spring 2012 Michael L. Nelson 04/03/12.
Web Server Design Week 15 Old Dominion University Department of Computer Science CS 495/595 Spring 2009 Michael L. Nelson 4/20/09.
1 CGI (Common Gateway Interface) CmpE 587 Emir Bayraktar Onur Bük.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
1 The Internet Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CS 330 Class 10 Programming plan for today: More files Saving data from a form.
Chapter 18 The HTML Tag
Chapter 7 - Introduction to Common Gateway Interface (CGI)
CS 330 Class 7 Comments on Exam Programming plan for today:
Perl Programming Language Design and Implementation (4th Edition)
Perl Variables: Hash Web Programming.
PHP PART 2.
Presentation transcript:

CS 330 Class 9 Programming plan for today: More of how data gets into a script Via environment variables Via the url From a form By editing the url directly More perl

Review: How data gets into a script Client passes data to the server as environment variables Client passes data to the server as part of the URL Similar to data format in results from sendmail scripts/guest.cgi?Name=Fred&Age=25 Server passes data to the script. Script accesses it via an environment variables. env.cgi, env2.cgi, form1.cgi

Review Input from a Form: GET Form fields are appended to the URL and passed in the environment variable QUERY_STRING ? Indicates beginning of the data = Separates the input name from the input value Call form1.cgi from cs330 page Call form1.cgi by editing the URL

form1.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print " \n"; print " Simple Form Processing \n"; print " \n"; print ”Query string: ",$ENV{'QUERY_STRING'}," \n\n"; ($field_name, $data) = split (/=/,$ENV{'QUERY_STRING'}); print "The field: ",$field_name, "\n"; print "The data: ",$data, " \n"; print " \n";

Perl - Identifiers $Name: a scalar variable ($field_name in an array in is the array, $ an = (“mary”, “fred”, “joe”) $Name(0) has the value “mary” %ENV: an associative array Strings for subscripts, not integers: $ENV{‘SERVER_NAME’} The subscripts (SERVER_NAME) are called keys NAME: a filehandle (later) –like inFile in C++. Convention: capitalize filehandles

Forms with multiple fields Name/data pairs are separated by &: /form2.cgi?firstName=Fred&lastName=Smith... The list of pairs is passed in environment variable QUERY_STRING The CGI program has to unpack the pairs See form2.htm and form2a.cgi

form2.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print " \n"; print " Form Processing 2 \n"; print " Form Processing, Multiple Inputs \n"; print " \n"; print " Query string: ",$ENV{'QUERY_STRING'},"\n\n"; print " Field Value = split (/&/, $ENV{'QUERY_STRING'}); foreach $pair ($field_name, $value) = split(/=/,$pair); print $field_name," ",$value,"\n"; } print " \n";

More Regular Expressions Split split(/pattern/, expr) # separate expr at the first occurrence of pattern, # and return the pair of substrings Translate: tr /search list/repl. list/ Replace each character in search list $name = tr/+/ / # replace + in $name by a space $name = tr/A-z/a-z/ # make lowercase form2a.htm: ($field_name, $val) = split(/=/,$pair); $val =~ tr/+/ /;

More Regular Expressions Substitute: s/pattern/repl. pattern/ Replaces a pattern in a search list $name = s/henry\.wells/aurora\.wells/ Binding: $string =~ /pattern/ True if pattern is in $string restrict.cgi: #!/usr/bin/perl $remotehost = $ENV{’HTTP_HOST'}; if ($remotehost =~ /\.wells\.edu/){ print "Location:../examples/local.htm", "\n\n";} print “ Location:..” called server redirection

Associative Arrays, Again avggrade.pl: read name/grade pairs into an associative array %grades is the whole array keys(%grades) is the set of all keys $grades{‘key’} is the value associated with the key key Does this work correctly?

avggrd.pl: #!/usr/bin/perl open GRADES, "grades.dat" #file handler variable is GRADES || die "Could not open grades.dat\n"; print "The data as entered:\n"; while ($line = ){ #read one line and store in $line chop($line); #gets rid of CR ($student, $grade) = split(" ", $line); #split $line at the first blank space $grades{$student} =$grade; #$grades is the assoc.array with key $student print "$student $grade $grades{$student}\n"; } print "\nThe data as stored:\n"; #print name/grade pairs from the associative array $total=0; foreach $stud (keys(%grades)) { #$stud is one of the keys print "$stud: $grades{$stud}\n"; $total += $grades{$stud}; $count++; } $average = $total / $count; print "The average of the grades is $average\n";