Chapter 4 © 2002 by Addison Wesley Longman, Inc. 1 Chapter 4 Sebesta: Programming the World Wide Web.

Slides:



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

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 The Basics of Perl.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Scalar Data Types and Basic I/O
A Guide to Unix Using Linux Fourth Edition
Lecture 2 Introduction to C Programming
Introduction to C Programming
COS 381 Day 21. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
COS 381 Day 18. Agenda  Assignment 4 Corrected 2 A’s, 3 C’s, 1 D and 1 F nment4/answer.htm
COS 381 Day 19. Agenda  Assignment 5 Posted Due April 7  Exam 3 which was originally scheduled for Apr 4 is going to on April 13 XML & Perl (Chap 8-10)
COS 381 Day 19. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
COS 381 Day 17. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Introduction to C Programming
 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.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Practical Extraction & Report Language PERL Joseph Beltran.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Input, Output, and Processing
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Week 1 Algorithmization and Programming Languages.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 The Basics of Perl.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Chapter 9 The Basics of Perl. © 2006 Pearson Addison-Wesley. All rights reserved Origins and Uses of Perl - Began in the late 1980s as a more.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
PERL. Introduction Perl is a flexible, powerful, widely used programming language. Perl is widely used for Common Gateway Interface (CGI) programming.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Chapter 4 © 2009 by Addison Wesley Longman, Inc Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
COMP234-Perl Variables, Literals Context, Operators Command Line Input Regex Program template.
9.1 Origins and Uses of Perl
Definition of the Programming Language CPRL
Chapter 6 JavaScript: Introduction to Scripting
Chapter 14 Introduction to Ruby.
Introduction to C++ Programming
Chapter 8 JavaScript: Control Statements, Part 2
Lexical Elements & Operators
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Chapter 4 © 2002 by Addison Wesley Longman, Inc. 1 Chapter 4 Sebesta: Programming the World Wide Web

Chapter 4 © 2002 by Addison Wesley Longman, Inc Origins and Uses of Perl - "Perl" is an acronym for "Practical Extraction and Report Language“. It is created by Larry Wall. - Began in the late 1980s as a more powerful replacement for the capabilities of awk (text file processing) and sh (UNIX system administration) - Now includes sockets for communications and modules for OOP, among other things - Now the most commonly used language for CGI, in part because of its pattern matching capabilities - Perl programs are usually processed the same way as many Java programs, compilation to an intermediate form, followed by interpretation 4.2 Scalars and Their Operations - Scalars are variables that can store either numbers, strings, or references (discussed later) - Numbers are stored in double format; integers are rarely used - Numeric literals have the same form as in other common languages

Chapter 4 © 2002 by Addison Wesley Longman, Inc Scalars and Their Operations (continued) - Perl has two kinds of string literals, those delimited by double quotes and those delimited by single quotes - Single-quoted literals cannot include escape sequences - Double-quoted literals can include them - In both cases, the delimiting quote can be embedded by preceding it with a backslash - If you want a string literal with single-quote characteristics, but don’t want delimit it with single quotes, use q x, where x is a new delimiter - For double quotes, use qq - If the new delimiter is a parenthesis, a brace, a bracket, or a pointed bracket, the right delimiter must be the other member of the pair - A null string can be '' or ""

Chapter 4 © 2002 by Addison Wesley Longman, Inc Scalars and Their Operations (continued) - Scalar type is specified by preceding the name with a $ - Name must begin with a letter; any number of letters, digits, or underscore characters can follow - Names are case sensitive - By convention, names of variables use only lowercase letters - Names embedded in double-quoted string literals are interpolated e.g., If the value of $salary is 47500, the value of "Jack makes $salary dollars per year" is "Jack makes dollars per year" - Variables are implicitly declared - A scalar variable that has not been assigned a value has the value undef (numeric value is 0 ; string value is the null string) - Perl has many implicit variables, the most common of which is $_

Chapter 4 © 2002 by Addison Wesley Longman, Inc Scalars and Their Operations (continued) - Numeric Operators - Like those of C, Java, etc. OperatorAssociativity ++, -- nonassociative unary - right ** right *, /, % left binary +, - left - String Operators - Catenation - denoted by a period e.g., If the value of $dessert is "apple", the value of $dessert. " pie" is "apple pie" - Repetition - denoted by x e.g., If the value of $greeting is "hello ", the value of $greeting x 3 is "hello hello hello "

Chapter 4 © 2002 by Addison Wesley Longman, Inc Scalars and Their Operations (continued) - String Functions - Functions and operators are closely related in Perl - e.g., if cube is a predefined function, it can be called with either cube(x) or cube x NameParametersResult chomp a stringthe string w/terminating newline characters removed length a stringthe number of characters in the string lc a stringthe string with uppercase letters converted to lower uc a stringthe string with lowercase letters converted to upper hex a stringthe decimal value of the hexadecimal number in the string join a character and the strings catenated a list of strings together with the character inserted between them

Chapter 4 © 2002 by Addison Wesley Longman, Inc Assignment Statements and Simple Input and Output - Assignment statements are as those in C++ & Java - All Perl statements except those at the end of blocks must be terminated with semicolons - Comments are specified with # - Keyboard Input - Files are referenced in Perl programs with filehandles - STDIN is the predefined filehandle for standard input, the keyboard - The line input operator is specified with $new = ; - If the input is a string value, we often want to trim off the trailing newline, so we use chomp($new = );

Chapter 4 © 2002 by Addison Wesley Longman, Inc Assignment Statements and Simple Input and Output (continued) - Screen Output print one or more string literals, separated by commas e.g., print "The answer is $result \n"; - Example program: print "Please input the circle’s radius: "; $radius = ; $area = * $radius * $radius; print "The area is: $area \n"; - One way to run a Perl program: perl prog1.pl - Two useful flags: -c means compile only (for error checking) -w means produce warnings for suspicious stuff (you should always use this!) - To get input from a file (read with <> ): perl prog1.pl prog1.dat

Chapter 4 © 2002 by Addison Wesley Longman, Inc Control Statements - Control Expressions 1. Scalar-valued expressions - If it’s a string, it’s true unless it is either the null string or it’s "0" - If it’s a number, it’s true unless it is zero 2. Relational Expressions - Operands can be any scalar-valued expressions Numeric OperandsString Operands ==eq !=ne <lt >gt <=le >=ge - If a string operator gets a numeric operand, the operand is coerced to a string; likewise for numeric operators

Chapter 4 © 2002 by Addison Wesley Longman, Inc Control Statements (continued) 3. Boolean Expressions - Operators: &&, ||, ! (higher precedence), as well as and, or, and not (lower precedence) - See Table 4.4, pp for the precedence and the associativity of operators - Assignment statements have values, so they can be used as control expressions while ($next = ) … - Because EOF is returned as the null string, this works - The keyboard EOF is specified with: Control+D for UNIX Control+Z for Windows COMMAND+. For Macintosh

Chapter 4 © 2002 by Addison Wesley Longman, Inc Control Statements (continued) - Selection Statements if ( control expression ) { then-clause } [ else { else-clause }] - Braces are required - elsif clauses can be included unless ( control expression ) { unless-clause } - Uses the inverse of the value of the control expression - Loop Statements while ( control expression ) { loop-body } until ( control expression ) { loop-body }

Chapter 4 © 2002 by Addison Wesley Longman, Inc Control Statements (continued) - Loop Statements (continued) for ( initial-expr ; control-expr ; increment-expr ) { loop-body } - The initial and increment expressions can be ‘comma’ expressions - Switch - Perl does not have one - Can be built with the last operator, which transfers control out of the block whose label is given as its operand SWITCH: { # SWITCH is the block label if ($input eq "bunny") { $rabbit++; last SWITCH; } if ($input eq "puppy") { $dog++; last SWITCH; } print "\$input is neither a bunny", " nor a puppy \n"; }

Chapter 4 © 2002 by Addison Wesley Longman, Inc Control Statements (continued) - The implicit variable $_ is used as the default operand for operators and the default parameter in function calls while ( ) { print; chomp; if ($_ eq "gold") { print "I’m rich, I’m rich!!! \n"; } 4.5 Fundamentals of Arrays - Perl arrays store only scalar values, which can store strings, numbers, and references - A list is an ordered sequence of scalar values - A list literal is a parenthesized list of scalar expressions - Used to specify lists in programs - Examples: ("Apples", $sum / $quantity, 2.732e-21) qw(Bob bib Bing bobble)

Chapter 4 © 2002 by Addison Wesley Longman, Inc Fundamentals of Arrays (continued) - An array is a variable that can store a list - Array names all begin with at signs ) - Arrays can be assigned other arrays or list = (2, 4, 6, - If an array is used where a scalar is expected, the length of the array is = (1, 55, 193); $len # $len now has the value 3 - A list literal that has only scalar names can be the target of a list assignment ($one, $two, $three) = (1, 2, 3); - When an array element is referenced or assigned, the name is a scalar name $list[3] = 17; $age = $list[1]; - The length of an array is dynamic; it is always the highest subscript that has been assigned, plus 1 (It is NOT necessarily the number of elements)

Chapter 4 © 2002 by Addison Wesley Longman, Inc Fundamentals of Arrays (continued) - The last subscript of an array is its name, preceded by $# - This value can be assigned - Scalar context versus list context - Often forced by an operator or a function - Scalar context can be forced with the scalar function - The foreach statement - to process arrays and hashes foreach $price { $price += 0.20; } - The foreach variable acts as an alias for the elements of the array - List Operators shift - removes and returns the first element of its list operand $left =

Chapter 4 © 2002 by Addison Wesley Longman, Inc Fundamentals of Arrays (continued) - List Operators (continued) unshift - puts its second operand (a scalar of a list) on the left end of its first operand (an array) 47; pop - a shift on the right end push - an unshift of the right end split - breaks strings into parts using a specific character as the split character $stuff = "233:466:688"; $numbers = split /:/, $stuff; sort - sorts using string comparisons (numbers are coerced to strings)

Chapter 4 © 2002 by Addison Wesley Longman, Inc Fundamentals of Arrays (continued) # process_names.pl - A simple program to # illustrate the use of arrays # Input: A file, specified on the command # line, of lines of text, where each # line is a person's name # Output: The input names, after all letters # are converted to uppercase, in # alphabetical order $index = 0; # Loop to read the names and process them while($name = <>) { # Convert the name's letters to uppercase # and put it in the names array $names[$index++] = uc($name); } # Display the sorted list of names print "\nThe sorted list of names is:\n\n\n"; foreach $name { print ("$name \n"); }

Chapter 4 © 2002 by Addison Wesley Longman, Inc Hashes (Associative Array) - Differ from arrays in two fundamental ways: 1. Arrays use numerics as indices, hashes use strings 2. Array elements are ordered, hash elements are not - Hash names begin with percent signs ( % ) - List literals are used to initialize hashes - Can be comma-separated values, as in %hash1 = ("Monday", 10451, "Tuesday", 10580); - Or, implication symbols can be used between a key and its value, as in %hash2 = ("Monday" => 10451, "Tuesday" => 10580); - The left operand of => need not be quoted - Subscripts are keys (strings) placed in braces $salary = $salaries{"Joe Schmoe"}; $salaries{"Michel Angelo"} = ;

Chapter 4 © 2002 by Addison Wesley Longman, Inc Hashes (continued) - Elements can be deleted with delete delete $salaries{"Bill Clinton"}; - Use exists to determine whether a key is in a hash if (exists $salaries{"George Bush"}) … - Keys and values can be moved from a hash to an array with keys and values foreach $name (keys %salaries) { print "Salary of $name is: $salaries{$name} \n"; = values %salaries; print "All of the salaries - Perl has a predefined hash named %ENV, which stores operating system environment variables and their values

Chapter 4 © 2002 by Addison Wesley Longman, Inc References - A reference is a scalar variable that references another variable or a literal - A reference to an existing variable is obtained with the backslash operator $ref_sum = \$sum; - A reference to a list literal is created by placing the literal in brackets $ref_list = [2, 4, 6, 8]; - A reference to a hash literal is created by placing the literal in braces $ref_hash = {Mom => 47, Dad => 48}; - All dereferencing in Perl is explicit - For scalars, add a $ to the beginning - For arrays and hashes, 1. Add a $ to the beginning of the name, or 2. Put the -> operator between the name and its subscript $ref_hash -> {"Mom"} = 48;

Chapter 4 © 2002 by Addison Wesley Longman, Inc Functions - A function definition is the function header and a block of code that defines its actions - A function header is the reserved word sub and the function’s name - A function declaration is a message to the compiler that the given name is a function that will be defined somewhere in the program - Syntactically, a function declaration is just the function’s header - Function definitions can appear anywhere in a program - Function calls can be embedded in expressions (if they return something useful) or they can be standalone statements (if they don’t) - A function that has been previously declared can be treated as a list operator - A function can specify a return value in two ways: 1. As the operand of a return statement (a function can have zero or more return s) 2. As the value of the last evaluated expression in the function

Chapter 4 © 2002 by Addison Wesley Longman, Inc Functions (continued) - Implicitly declared variables have global scope - Variables can be forced to be local to a function by naming them in a my declaration, which can include initial values my $sum = 0; my ($total, $pi) = (0, ); - Parameters - Actual parameters vs. formal parameters - Pass-by-value is one-way, to the function - Pass-by-reference is two-way - Parameters are passed through the implicit (implicitly copied in) - Elements are aliases for the actual parameters - Every function call has its own version - In the called function, parameters can be manipulated directly or in local variables initialized to elements

Chapter 4 © 2002 by Addison Wesley Longman, Inc Functions (continued) sub fun1 { my($first) = $_[0]; ++$first * ++$_[1]; } - Pass-by-reference parameters can be implemented by passing references sub sub1 { my($ref_len, $ref_list) my $count; for ($count = 0; $count < $$ref_len; $$ref_list[$count++]--){ } - An example call to sub1 : sub1(\$len,

Chapter 4 © 2002 by Addison Wesley Longman, Inc Functions (continued) sub median { my $len = $_[0]; # Discard the first element of the array # Sort the parameter = # Compute the median if ($len % 2 == 1) { # length is odd return $list[$len / 2]; } else { # length is even return ($list[$len / 2] + $list[$len / 2 - 1]) / 2; } } # End of function median $med = print "The median of is: $med \n";

Chapter 4 © 2002 by Addison Wesley Longman, Inc The pack and unpack Functions - Used to convert numbers to text and vice versa - Both take two parameters, a template and a list - The template specifies the particular conversion - The list specifies the data to be packed or unpacked - pack is used to build a text string from an array of numbers - unpack can be used to get the numeric codes of the characters in a string and make them elements of an array - Both of these conversions are specified with the "C" template $str[0] = 'C'; $str[1] = 'U'; $cu_str = # work? $cu_str = - Now, $cu_str has = unpack("CC", $cu_str); - has (67, 85)

Chapter 4 © 2002 by Addison Wesley Longman, Inc Pattern Matching - The pattern-matching operator is m, but if slashes are used to delimit the pattern operand, the m can be omitted - The default string against which the pattern is matched is in $_ - Character and character-class patterns - Metacharacters: \ | ( ) [ ] { } ^ $ * + ?. - A non-meta, or normal character matches itself if (/gold/) { print "There's gold in that thar string!! \n"; } - Metacharacters can match themselves if they are backslashed - The period matches any character except newline /a.b/ matches "aab", "abb", "acb",...

Chapter 4 © 2002 by Addison Wesley Longman, Inc Pattern Matching (continued) - A character class is a string in brackets [abc] means a | b | c - A dash can be used to specify a range of characters [A-Za-z] - If a character class begins with a circumflex, it means the opposite [^A-Z] matches any character except an uppercase letter - Predefined character classes: NameEquivalent Pattern Matches \d[0-9] a digit \D[^0-9] not a digit \w[A-Za-z_0-9] a word character \W[^A-Za-z_0-9] not a word character \s[ \r\t\n\f] a whitespace character \S[^ \r\t\n\f] not a whitespace character

Chapter 4 © 2002 by Addison Wesley Longman, Inc Pattern Matching (continued) - Pattern Quantifiers - pattern { n } means repeat the pattern n times /a{5}bc{5}/ - pattern * means repeat the pattern zero or more times /a*bc*/ - pattern + means repeat the pattern 1 or more times - pattern ? means zero or one match /\d*b?c+/ - Two more useful predefined patterns: \b - matches the boundary position between a \w character and a \W character, in either order \B - matches a non-word boundary - These two do not match characters, they match positions between characters

Chapter 4 © 2002 by Addison Wesley Longman, Inc Pattern Matching (continued) - Binding Operators - to match against a string other than the string in $_ $str =~ /\w/; $str !~ /\w/; - Anchors - match positions, not characters 1. ^ in front of a pattern (not in a character class) means the pattern must match at the beginning 2. $ at the end of a pattern means the pattern must match at the end - Pattern modifiers (after the pattern) 1. i makes letters in the pattern match either uppercase or lowercase 2. x allows whitespace in the pattern, including comments

Chapter 4 © 2002 by Addison Wesley Longman, Inc Pattern Matching (continued) - Remembering matches - After the match, the implicit variables $1, $2, … have the parts of the string that matched the first, second, … parenthesized subpattern "John Fitzgerald Kennedy" =~ /(\w+) (\w+) (\w+)/; Now, $1 has "John", $2 has "Fitzgerald", and $3 has "Kennedy" - Inside the pattern, \1, \2, … can be used $` has the part of the string before the part that matched $& has the part of the string that matched $’ has the part of the string after the part that matched - Substitutions - Used to find and replace a substring s/ Pattern / New_String / $_ = "Darcy is her name, yes, it’s Darcy" s/Darcy/Darcie/;

Chapter 4 © 2002 by Addison Wesley Longman, Inc Pattern Matching (continued) - Substitutions (continued) - Modifiers - The g modifier means find and replace all of them in the string - The e modifier means the New_String must be interpreted as Perl code - Example: Find a single hex character and replace it with its decimal value s/%([\dA-Fa-f])/pack("C", hex($1))/e; - The i modifier does what it does for pattern matching - Transliterate Operator - Translates a character or character class into another character or character class tr/a-z/A-Z/; - Transliterates all lowercase letters to upper

Chapter 4 © 2002 by Addison Wesley Longman, Inc File Input and Output - The open function is used to create the connection between a filehandle and the external name of a file; it also specifies the file’s use - A file’s use is specified by attaching < (input), > (output, starting at the beginning of the file), or >> (output, starting at the end of the existing file) to the beginning of its name open (INDAT, "<prices"); open (OUTDAT, ">averages"); - Because open can fail, it is usually used with die open (OUTDAT, ">>salaries") or die "Error - unable to open salaries $!"; - One line of output to a file: print OUTDAT "The answer is: $result \n"; - One line of input from a file: $next = ; - Buffers (of any size) of input can be read from a file with the read function read (filehandle, buffer, length [, offset]);