CS 898N – Advanced World Wide Web Technologies Lecture 7: PERL Chin-Chih Chang

Slides:



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

CSC 4630 Perl 1. Perl Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
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.
Practical Extraction & Report Language Picture taken from
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
JavaScript, Fourth Edition
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Practical Extraction & Report Language PERL Joseph Beltran.
1 An Introduction to Perl Part 1 CSC8304 – Computing Environments for Bioinformatics - Lecture 7.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
Input, Output, and Processing
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
COMP519: Web Programming Autumn 2010 Perl Tutorial: The very beginning  A basic Perl Program The first line Comments and statements Simple printing 
Introduction to Perl Giorgos Georgakilas Graduated from C.E.I.D.Graduated from C.E.I.D. M.Sc. degree in ITMBM.Sc. degree in ITMB Ph.D. student in DIANA-LabPh.D.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
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.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
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
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Chapter 11: Perl Scripting Off Larry’s Wall. In this chapter … Background Terminology Syntax Variables Control Structures File Manipulation Regular Expressions.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Interpolation Variable Interpolation, Backslash Interpolation.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
An Introduction to Perl – Part I
Perl Variables: Array Web Programming.
Control Structures: if Conditional
An Introduction to Perl
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
PHP.
The Selection Structure
INTRODUCTION to PERL PART 1.
Karan Thaker CS 265 Section 001
Presentation transcript:

CS 898N – Advanced World Wide Web Technologies Lecture 7: PERL Chin-Chih Chang

Perl - Elements The language elements of Perl fall into four categories: –Data types: scalars, arrays, and hash arrays –Statements –Regular expressions –File operations

Perl – Data Types The basic Perl data types are: –Scalar - $data –Array –Hash array - %data –File handle – data Perl variable names are case and type sensitive.

Perl – Data Types (Scalar) The variable name must start with a letter and may contain any combination of letters, numbers, and the underscore _ character. $scalar - $string, $number, $array[$n], $hash_array{$n} Scalar variables are prefixed with the $ sign and include numeric and string types. Individual array elements are represented using the $ sign and index.

Perl – Data Types (Scalar) Numeric types are internally represented as double-precision floating-point. Numeric can be assigned as integers, fixed- point, or in floating-point notation. For example, can be written as e5. String types are enclosed in either ‘single- quote pairs’ or “double-quote pairs”.

Perl – Data Types (Scalar) Strings in single quotes are true literals and use a backslash \ as an escape key, \’ as a single quote, double backslashes \\ as a backslash. Double-quoted strings support a Perl string replacement function called variable interpolation.

Perl – Data Types (Scalar) Any variable name found inside a double- quoted string will be replaced by the value of that variable. For example, the first two lines would result in the third line being printed: $name = “Larry Wall”; Print “The value of name is $name.”; The value of name is Larry Wall.

Perl – Data Types (Scalar) Variable interpolation can be avoided by breaking up the string into parts using the “.” string concatenation operator. For example, ‘The value of $name is ’. $name; # or ‘The value of $’. ”name is $name”; If a variable is used but not defined, a blank or 0 value is assigned.

Perl – Data Types (Scalar) If $name was not defined, the preceding statement would not result in an error but in an empty string as follows: The value of $name is Double-quoted strings support a full range of escaped characters including \\ (backslash), \” (double quote), \cX (any control character; e.g., control-x), \e (escape), \n (newline), \t (tab), and \xFF (any hexadecimal character).

Perl – Data Types (Array) The most common use of the escaped character is the newline in the print function. print “Hello A list (array) is a collection of scalar data in a specific order, and an array is a variable that holds a list. A list can contain both string and numeric values.

Perl – Data Types (Array) () represents an empty list with no elements. A list literal may be composed of a mixture of numeric and string values, scalar variables, ranges (..), and even other lists. In the following code, the quote word ( qw ) function is used to create arrays without typing = qw(red green = qw(cyan magenta yellow)

Perl – Data Types (Array) The following array is formed from a list of two scalar literals and two = The list constructor operator “.. ” can be used to create an integral range of numeric = (1..12,14..22); $x =.5; $y = 9.5;

Perl – Data Types = = ($x..$y); Perl is very flexible in how lists can be used on both sides of the equal sign. These statements are all valid ways to assign values: ($red, $green, $blue) = qw(red green blue); ($top, $bottom) = ($bottom, $top);

Perl – Data Types (Array) Array elements are referenced using the [] bracket pair, which may contain a literal, scalar variable, or expression. The following statement switches two array values by using two lists referencing the same array. ($name[$first],$name[$last])= ($name[$last],$name[$first]);

Perl – Data Types (Array) The following one accomplishes the switching by using a technique called slicing. A slice of the array is represented by using the format $array[element $last] 2] = qw(Larry, Wall); The highest element of an array is represented by the $#array variable.

Perl – Data Types (Array) In Perl, the last element can be indexed by -1. $ants[$#ants] = “little one”; $ants[-1] = “little one”; Arrays can also be assigned to each other directly and by combing array slicing with array assignment.

Perl – Data Types (Hash = = (0, 2, 4, 6, = %hash_array - %hash_array A hash array is a list of string keys and values that are paired together. $ partridge = “a partridge in a pear tree” %christmas = ($patridge, 1, “turtule doves”, 2, “french hens”, 3);

Perl – Data Types (Hash Array) Individual hash array elements are assigned by using the string key in place of the element number. $christmas{“calling birds”} = 4; The following two lines should read and then restore the hash array with no = %christmas; %christmas

Perl – Data Types (Hash Array) The following line creates a new hash from three pairs converted from array. The following two lines copy hash arrays. %next_year = %christmas; %allpresents = (%mypresents, %yourpresents);

Perl – Data Types (Hash Array) Hash keys and values can be extracted separately using the keys and values = = values(%christmas); The defined() function returns a Boolean value based on whether the scalar or array element has had value assigned to it. if (defined(%presents(“new car”));

Perl – Statements (Operator) Perl supports an extended set of mathematical and string handling operators, and a standard set of comparison operators. –+-*/ : add, subtract, multiply, divide –** : exponentiation, e = m*c**2. –% : modulus, 10%3 = 1. –++ -- : auto- increment and decrement. –x : string replication, $test = test x 3. –. : string concatenation,

Perl – Statements (Operator) –.. : range, 1..5 = 1, 2, 3, 4, 5. –=~ : match. Used with regular expressions for string search and replacement, $val =~ tr/+/ /; –!~ : no match. –&& and || : logical AND and logical OR. Perl has separate comparison operators for numeric ( ==, !=,, >= ) and string ( eq, ne, lt, le, gt, ge ) variables: –Equals: ==, eq –Not equal: !=, ne

Perl – Statements (Operator) –Less than: <, lt –Less than or equal: <=, le –Greater than: >, gt –Greater than or equal: >=, ge String manipulation functions: –split() : Split returns a list of strings by split from a single string at the operator expression. $cgiinput = = split (/:/, $cgiinput); $pairs[0] = “http”; $pairs[1] = “//sun.com”

Perl – Statements (String Manipulation) –join() : Join returns a single string from a list of strings joined together by the glue = qw (Hello World Welcome); $string = join (“, $string = “Hello, World, Welcome”; –sort() : It does an alphanumeric sort. –reverse() : It reverses the = qw (Hello World Wide = sort

Perl – Statements (Array = = (‘Hello’, ‘Web’, ‘Wide’, = (‘Web’, ‘Wide’, ‘World’, ‘Hello’); There are four functions to add and remove array elements. –push() : It adds an element into the end of the array. –pop() : It removes an element from the end of the array.

Perl – Statements (Array Manipulation) –unshift() : It adds an element into the start of the array. –shift() : It removes an element from the start of the array. push $stage3); $stage3 = pop unshift $booster); $booster = shift

Perl – Statements (Hash Array Manipulation) Hash arrays have their own functions: –delete() : It is used to remove hash array elements. (“the avengers”); –each() : It provides easy looping through hash array pairs. while (($title, $star) = each (%movie)) { print “The star of $title is $star\n”; }

Perl – Statements (if) There are two types of if statements –if..else and elseif if (expression) {statements}; if (expression) {statements} else {statements}; if (expression) {statements} elseif {statements} else {statements}; –unless..else unless (expression) {statements};

Perl – Statements (if) unless (expression) {statements} –Perl supports the following extension: {statements} if (expression); {statements} unless (expression);