Introduction to Programming the WWW I CMSC 10100-1 Winter 2003 Lecture 17.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
 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.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Regular Expressions Week 07 TCNJ Web 2 Jean Chu. Regular Expressions Regular Expressions are a powerful way to validate and format text strings that may.
Tutorial 14 Working with Forms and Regular Expressions.
Introduction to Shell Script Programming
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Input Validation with Regular Expressions COEN 351.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Introduction to Perl Yupu Liang cbio at MSKCC
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Topics Sending an Multipart message Storing images Getting confirmation Session tracking using PHP Graphics Input Validators Cookies.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Strings, output, quotes and comments
Storing and Retrieving Data
Introduction to Programming the WWW I CMSC Winter 2003.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Chapter Six Introduction to Shell Script Programming.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
Linux+ Guide to Linux Certification, Second Edition
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
CSE 374 Programming Concepts & Tools
CS 330 Class 7 Comments on Exam Programming plan for today:
Input from STDIN STDIN, standard input, comes from the keyboard.
CIRC Summer School 2017 Baowei Liu
Lecture 9 Shell Programming – Command substitution
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Introduction to Scripting
CSE 303 Concepts and Tools for Software Development
Writing Shell Scripts ─ part 3
Intro to PHP & Variables
Chapter Four UNIX File Processing.
JavaScript: Introduction to Scripting
Introduction to Bash Programming, part 3
Presentation transcript:

Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17

Environment variables When Perl is started from a browser, a special hash list is created describing how it was invoked. This is called the environmental hash list and is called %ENV Referring Web site, what browser, language, method, remote address, remote host

Environment variables HTTP_REFERRER, REQUEST_USER_AGENT, HTTP_ACCEPT_LANGUAGE, REQUEST_METHOD, REMOTE_ADDRESS, REMOTE_HOST Can configure response to browser or disallow/allow certain domains

What next? Scripts to write the forms Scripts to validate the form and spit it back if the user didn’t enter everything correctly Example: newform.pl

Files Files are opened via the open command: open(FILE,’filename’); First argument is the “handle” Second argument is a string -- the name of the file (perhaps including path)

Options for opening files Read (default): open(HANDLE,’<filename’); Write: open(HANDLE,’>filename’); Append open(HANDLE,’>>filename’); Uses redirection operators from Unix

What about errors? Errors can occur if a file that doesn’t exist is opened for reading, etc open(HANDLE,’<filename’) || die “Can’t open file $!”; sends error message to STDERR The variable $! contains the latest error message returned by a system call open returns 0 or 1

Reading from files (and STDIN) Use the syntax to get either a line or the whole file $aline = = ; By not specifying a location, the line of input appears in $_.

Example To read in a whole file and print it back to the screen, we use the code open(FILE,’filename’) || die $!; while( ) { print $_; } close FILE; An EOF is interpreted as false in the loop

Even more arcane open(FILE,’filename’) || die $!; while( ) { print; } close FILE; The default argument of print (and some other functions) is $_.

More implicit variables Records in a file (normally lines) are separated by $/ –changing this from “\n” to “” reads in paragraph mode, to “undef();” reads in the whole file Output field separator: $, –print “one”, “two” equivalent to –print “one”. $,. “two Output record separator: $\ –Typically blank –Changing it changes the terminal value of print statements

Example Read and print addresses Read, sort, and print addresses Read and print links to addresses

Editing a file Open file for reading Read everything into memory Close the file Make the changes (in memory) Open the file for writing Write the file Close the file

Functions You can define your own functions in Perl: sub foo { # do stuff # return a value }

Where are the parameters? You call foo(a,b); (or something) How do you get the values? Another cooky variable –arbitrary list of parameters read in

Example: maximum of a set sub nmax { my $local_max = $_[0]; for (my $i=1;$i<=$#_;$i++) { if ($_[$i] > $local_max) { $local_max = $_[$i]; } return $local_max; }

Pattern matching TARGET =~ m/PATTERN/ TARGET is the string we are searching =~ is the binding operator PATTERN is what we are looking for Returns either true or false Default target is $_

Example #!/usr/bin/perl print <<END; type a message till you say the word. Press ^D to quit END while ( ) { if (m/foo/) { print "you typed 'foo'!"; exit(); }

Applications Form validation –checking dates –ZIP codes Parsing/reading files

Matching constructs Don’t just have to match exact expressions Eg: m/foo/i (case insensitive) Eg: m/foo[bc]ar/ matches foobar and foocar Can specify the complement of a set: m/foo[^bc]ar/ to match foo?ar with ? not b or c

Other constructs Range: [a-g] same as [abcdefg] Backslash sequences: –\d same as [0-9], \D = [^0-9] –\w is “word character” [a-zA-Z0-9_] –\s is any whitespace character –. is anything but new line

Anchors We can match a string beginning with a value by: m/^abc/ We can match a string ending with a value by: m/abc$/ We can also match both

Quantifiers By default, regexp characters match one and only one thing. We can edit this with quantifiers: –? means 0 or 1 of the preceeding –* means 0 or more –+ means 1 or more –a{n,m} matches between n and m a’s

Capturing subpatterns Regexp in parentheses cause the matched text to be remembered for later m/foo(\w)bar/ stores whatever word character occurs between foo and bar into $1 This can be referred to as \1 in the regexp: m/foo(\w)bar\1/ matches fooTbarT but not foodbars

Substitution operator We can also replace one expression with another by s/PATTERN/REPLACEMENT/ pattern may have a regexp, replacement is a double- quoted string $string = “abcdefgh”; $string =~ s/def.*/...xyz/; # $string now abc...xyz

Next time Multi-page sessions Cookies Hidden fields