COMP284 Scripting Languages – Handouts
COMP284 Scripting Languages
Lecture 1: Overview of COMP284
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
1 Introduction
Motivation
Scripting languages
2 COMP284
Aims
Learning outcomes
Delivery
Assessment
COMP284 Scripting Languages Lecture 1 Slide L1 – 1
Introduction Motivation
How many programming languages should you learn?
1 Academic / Educational viewpoint:
Learn programming language concepts and
use programme languages to gain practical experience with them
– imperative / object-oriented — C, Java
– functional — Maude, OCaml, Haskell
– logic/constraint — Prolog, DLV
– concurrent
then all (other) programming languages can be learned easily
2 An employer’s viewpoint:
Learn exactly those programming languages that the specific employer
needs
3 Compromise: Spend most time on 1 but leave some time for 2 to
allow more than one language from a class/paradigm to be learned
4 Problem: Which additional language do you cover?
; Look what is used/demanded by employers
COMP284 Scripting Languages Lecture 1 Slide L1 – 2
Introduction Motivation
Programming languages: Job ads
Software Developer
(Digital Repository)
University of Liverpool – University Library
£31,020 – £35,939 pa
To work as part of a small team based in the University Library, working closely
with the University’s Computing Services Department on the institutional digital
repository, recommending and developing technical solutions, tools and
functionality to integrate the repository with other internal systems and to enable
research outputs to be shared externally. You will be an experienced Software
Developer with knowledge of LAMP technologies such as XML, XSLT, Perl and
Javascript. You will hold a degree in Computer Science or a related discipline
and/or have proven industrial experience of software development. The post is
full time, 35 hours per week.
Job Ref: A-576989
COMP284 Scripting Languages Lecture 1 Slide L1 – 3
Introduction Motivation
Programming languages: Job ads
Senior Software Development Manager
IMDb Video and Recommendations (Seattle, WA)
IMDb (a wholly-owned subsidiary of Amazon) is recruiting for a Senior Software
Development Manager to lead our “What to Watch” team. You’ll be charged
with transforming IMDb from a reference site to a place where hundreds of
millions of people find and discover what to watch across a variety of video
providers, and seamlessly connect them with watching the movies and TV shows
best suited for them, wherever and whenever they may be.
Basic qualifications:
• Bachelor’s degree in Computer Science, Computer Engineering or
related technical discipline
• 10+ years of experience as a software developer
• 5+ years experience managing people
• Software development experience in OOP, Java, Perl, HTML, CSS,
JavaScript, Linux/UNIX, AJAX, MySQL
COMP284 Scripting Languages Lecture 1 Slide L1 – 4
Introduction Motivation
Programming languages: Job ads
Full-time Remote Worker
AOL Tech (Engadget, TUAW, Joystiq, Massively)
AOL Tech is looking for a great front-end developer who can help us take
Engadget and our other blogs to new levels.
The ideal candidate is highly proficient in JavaScript/jQuery, comfortable with
PHP / mySQL and experienced in web design, optimization and related
technologies for desktop and mobile. A solid understanding of mobile-first design
is a must.
Requirements:
• High proficiency in JavaScript/jQuery
• Familiar with spriting, lazy loading, and other general
performance-optimized techniques
• Mac access for compatibility with current tools
• HTML5/CSS3
• Git, SSH
COMP284 Scripting Languages Lecture 1 Slide L1 – 5
Introduction Motivation
Websites and Programming Languages
Website Client-Side Server-Side Database
Google JavaScript C, C++, Go, Java,
Python, PHP
BigTable, MariaDB
Facebook JavaScript Hack, PHP, Python,
C++, Java, . . .
MariaDB, MySQL,
HBase Cassandra
YouTube Flash,
JavaScript
C, C++, Python, Java,
Go
BigTable, MariaDB
Yahoo JavaScript PHP MySQL, PostgreSQL
Amazon JavaScript Java, C++, Perl Oracle Database
Wikipedia JavaScript PHP, Hack MySQL, MariaDB
Twitter JavaScript C++, Java, Scala MySQL
Bing JavaScript ASP.NET MS SQL Server
Wikipedia Contributors: Programming languages used in most popular websites. Wikipedia, The Free Encyclopedia,
20 October 2017, at 11:28. http://en.wikipedia.org/wiki/Programming_languages_used_in_most_popular_websites
[accessed 23 October 2017]
COMP284 Scripting Languages Lecture 1 Slide L1 – 6
http://en.wikipedia.org/wiki/Programming_languages_used_in_most_popular_websites
Introduction Scripting languages
Scripting languages
Script
A user-readable and user-modifiable program that performs simple
operations and controls the operation of other programs
Scripting language
A programming language for writing scripts
Classical example: Shell scripts
#!/bin/sh
for file in *; do
wc -l “$file”
done
Print the number of lines and name for each file in the current directory
COMP284 Scripting Languages Lecture 1 Slide L1 – 7
Introduction Scripting languages
Scripting languages: Properties
• Program code is present at run time and starting point of execution
• compilation by programmer/user is not needed
• compilation to bytecode or other low-level representations
may be performed ‘behind the scenes’ as an optimisation
• Presence of a suitable runtime environment is required for the execution
of scripts
• includes an interpreter, or just-in-time compiler, or bytecode compiler plus
virtual machine
• typically also includes a large collection of libraries
• Executation of scripts is typically slower then the execution of code that
has been fully pre-compiled to machine code
#!/bin/sh
for file in *; do
wc -l “$file”
done
COMP284 Scripting Languages Lecture 1 Slide L1 – 8
Introduction Scripting languages
Scripting languages: Properties
• Rich and easy to use interface to the underlying operating system,
in order to run other programs and communicate with them
• rich input/output capabilities, including pipes, network sockets, file I/O,
and filesystem operations
• Easy integration within larger systems
• often used to glue other systems together
• can be embedded into other applications
#!/bin/sh
for file in *; do
wc -l “$file”
done
COMP284 Scripting Languages Lecture 1 Slide L1 – 9
Introduction Scripting languages
Scripting languages: Properties
• Variables, functions, and methods
typically do not require type declarations
(automatic conversion between types, e.g. strings and numbers)
• Some built-in data structures
(more than in C, fewer than in Java)
• Ability to generate, load, and interpret source code at run time
through an eval function
JavaScript:
var x = 2;
var y = 6;
var str = “if (x > 0) { z = y / x } else { z = -1 }”;
console.log(’z is ’, eval(str )); // Output: z is 3
x = 0;
console.log(’z is ’, eval(str )); // Output: z is -1
COMP284 Scripting Languages Lecture 1 Slide L1 – 10
Introduction Scripting languages
Scripting languages: Properties
• The evolution of a scripting language typically starts
with a limited set of language constructs for a specific purpose
Example: PHP started as set of simple ‘functions’
for tracking visits to a web page
• The language then accumulates more and more language constructs
as it is used for a wider range of purposes
• These additional language constructs may or may not fit well together
with the original core and/or may duplicate existing language constructs
• During this evolution of the language, backward compatibility
may or may not be preserved
; Language design of scripting languages is often sub-optimal
COMP284 Scripting Languages Lecture 1 Slide L1 – 11
COMP284 Aims
Aims
1 To provide students with an understanding of
the nature and role of scripting languages
2 To introduce students to some popular scripting languages
and their applications
3 To enable students to write simple scripts using these languages
for a variety of applications
COMP284 Scripting Languages Lecture 1 Slide L1 – 12
COMP284 Learning outcomes
Learning Outcomes
At the end of the module students should be able to
1 compare and contrast languages such as JavaScript, Perl and PHP
with other programming languages
2 document and comment applications witten using a scripting language
3 rapidly develop simple applications, both computer and web-based,
using an appropriate scripting language
COMP284 Scripting Languages Lecture 1 Slide L1 – 13
COMP284 Delivery
Delivery of the module (1)
1 Lectures
• Structure:
16 to 18 lectures
• Schedule:
1 or 2 lectures per week spread over 9 weeks
See your personal timetable and e-mail announcements for details
• Lecture notes and screencasts are available at
cgi.csc.liv.ac.uk.uk/~ullrich/COMP284/notes
• Revise the lectures before the corresponding practical
• Additional self study using the recommended textbooks
and the on-line material is essential
COMP284 Scripting Languages Lecture 1 Slide L1 – 14
cgi.csc.liv.ac.uk.uk/~ullrich/COMP284/notes
COMP284 Delivery
Delivery of the module (1)
2 Practicals
• Structure:
– 7 practicals with worksheets (3 Perl, 2 PHP, 2 JavaScript)
; gain understanding via practice
; get answers to questions about the lecture material
– Up to 3 additional practicals for questions about the assignments
• Schedule:
1 practical per week for about 10 weeks
Practicals start in week 2
• Practicals assume familiarity with Linux and departmental Linux systems
; To recap, use the worksheets available at
cgi.csc.liv.ac.uk.uk/~ullrich/COMP284/notes
• Practicals assume familiarity with the related lecture material
COMP284 Scripting Languages Lecture 1 Slide L1 – 15
cgi.csc.liv.ac.uk.uk/~ullrich/COMP284/notes
COMP284 Delivery
How to learn a new programming language
• Once you know how to program in one programming language,
additional programming languages are best learned by a process of
enquiry and practice guided by existing experience
• Typically, the questions that guide you are
• What kind of . . . are there?
Example: What kind of control structures are there?
• What is the syntax for . . . ?
Example: What is the syntax for conditional statements?
• What happens if . . . ?
Example: What happens if 1 is divided by 0?
• How do I . . . ?
Example: How do I catch an exception?
• Talk to other people who are currently trying to learn the same
language or have already learned it
; Ask what has surprised them most
COMP284 Scripting Languages Lecture 1 Slide L1 – 16
COMP284 Delivery
How to learn a new programming language
• Once you know how to program in one programming language,
additional programming languages are best learned by a process of
enquiry and practice
• The best kind of learning is learning by doing
; The questions posed on the previous slide are often best explored
by experimenting with small sample programs (‘toy’ programs)
• Work on substantive programs
; You need to convince employers that you have worked on programs
more substantive than ‘toy’ programs
; The assignments are ‘pretend’ substantive programs
but in reality are too small
• Employers value experience, in particular, the experience that you get
from overcoming challenges
; Assignments that are not challenging are of limited value
COMP284 Scripting Languages Lecture 1 Slide L1 – 17
COMP284 Delivery
Delivery of the module (3)
3 Office hours
Monday, 16:00 Ashton, Room 1.03
but always arrange a meeting by e-mail first
(U.Hustadt@liverpool.ac.uk)
4 Announcements will be send by e-mail
• You should check you university e-mail account at least every other day
• Always use your university e-mail account
if you want to contact me or any other member of staff
COMP284 Scripting Languages Lecture 1 Slide L1 – 18
COMP284 Delivery
Recommended texts
• Core reading
• R. Nixon:
Learning PHP, MySQL, & JavaScript. Learning PHP. . . , 4th edition.
O’Reilly, 2009. O’Reilly, 2014.
Harold Cohen Library: 518.561.N73 or e-book
• R. L. Schwartz, brian d foy, T. Phoenix:
Learning Perl. Learning Perl, 7th edition.
O’Reilly, 2011. O’Reilly, 2016.
Harold Cohen Library: 518.579.86.S39 or e-book
• Further reading
• M. David:
HTML5: designing rich Internet applications.
Focal Press, 2010.
Harold Cohen Library: 518.532.D24 or e-book
• N. C. Zakas:
Professional JavaScript for Web Developers.
Wiley, 2009.
Harold Cohen Library: 518.59.Z21 or e-book
COMP284 Scripting Languages Lecture 1 Slide L1 – 19
COMP284 Assessment
Assessment
• This is a coursework-based module
(no exam)
• Three assessment tasks need to be completed throughout the semester:
– Perl Deadline: Friday, 2 March, 17:00
– PHP Deadline: Monday, 9 April, 12:00
– JavaScript Deadline: Friday, 27 April, 17:00
• Effort required: about 10 hours each
• Available at: http://cgi.csc.liv.ac.uk/~ullrich/COMP284/
COMP284 Scripting Languages Lecture 1 Slide L1 – 20
http://cgi.csc.liv.ac.uk/~ullrich/COMP284/
COMP284 Assessment
Attendance and Performance
Average Average Average
Lecture Practical Module
Students Attendance Attendance Mark
2011-12 33 76.0% 70.0% 63.1
2012-13 58 82.0% 69.0% 64.5
2013-14 107 80.0% 60.0% 59.1
2014-15 119 71.3% 65.2% 54.5
2015-16 76 67.4% 46.8% 57.9
2016-17 114 43.8% 38.3% 53.0
• From 2014-15, screencasts of the lectures were available to students
• From 2015-16, the requirement to write a report on each program was dropped
• Hypothesis 1:
Lecture Attendance > 75% and Practical Attendance > 65% ⇔ Module Mark > 62
• Hypothesis 2:
Screencasts Available ⇔ Module Mark < 59
COMP284 Scripting Languages Lecture 1 Slide L1 – 21
COMP284 Assessment
Academic Integrity
• Plagiarism occurs when a student misrepresents, as his/her own work,
the work, written or otherwise, of any other person (including another
student) or of any institution
• Collusion occurs where there is unauthorised co-operation between a
student and another person in the preparation and production of work
which is presented as the student’s own
• Fabrication of data occurs when a student enhances, exaggerates, or
fabricates data in order to conceal a lack of legitimate data
If you are found to have plagiarised work, colluded with others, or
fabricated data, then you may fail COMP284
Serious ‘offenders’ may be excluded from the University
Do not try to take a ‘shortcut’
You must do the work yourself!
COMP284 Scripting Languages Lecture 1 Slide L1 – 22
COMP284 Assessment
Academic Integrity: Lab rules
• Do not ask another student to see any part of their code for a
COMP284 assignment
; contravention of this leads to collusion
• Do not show or make available any part of your code relating for a
COMP284 assignment to any other student
; contravention of this leads to collusion
• Do not share (links to) on-line material that might help with a
COMP284 assignment
; contravention of this leads to collusion
• Lock your Lab PC when you leave it alone
• Where you use any material/code found on-line for a COMP284
assignment, you must add comments to your code indicating its origin
by a proper academic reference
; contravention of this is plagiarism
; acknowledged code re-use may still result in a lower mark
COMP284 Scripting Languages Lecture 1 Slide L1 – 23
COMP284 Scripting Languages
Lecture 2: Perl (Part 1)
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
3 Perl: Overview
History
Applications
Java vs Perl
4 Scalars
Definition
Integers and Floating-point numbers
Strings
‘Booleans’
Comparisons
5 Variables, Constants, and Assignments
Variables
Constants
Assignments
Variable interpolation
COMP284 Scripting Languages Lecture 2 Slide L2 – 1
Perl: Overview History
Perl
• Originally developed by Larry Wall in 1987
Perl 6 was released in December 2015
• Borrows features from
• C
imperative language with variables, expressions, assignment statements,
blocks of statements, control structures, and procedures / functions
• Lisp
lists, list operations, functions as first-class citizens
• AWK (pattern scanning and processing language)
hashes / associative arrays, regular expressions
• sed (stream editor for filtering and transforming text)
regular expressions and substitution s///
• Shell
use of sigils to indicate type ($ – scalar, @ – array, % – hash, & – procedure)
• Object-oriented programming languages
classes/packages, inheritance, methods
COMP284 Scripting Languages Lecture 2 Slide L2 – 2
Perl: Overview Applications
Perl: Uses and applications
• Main application areas of Perl
• text processing
; easier and more powerful than sed or awk
• system administration
; easier and more powerful than shell scripts
• Other application areas
• web programming
• code generation
• bioinformatics
• linguistics
• testing and quality assurance
COMP284 Scripting Languages Lecture 2 Slide L2 – 3
Perl: Overview Applications
Perl: Applications
• Applications written in Perl
• Movable Type – web publishing platform
http://www.movabletype.org/
• Request Tracker – issue tracking system
http://bestpractical.com/rt/
• Slash – database-driven web application server
http://sourceforge.net/projects/slashcode/
COMP284 Scripting Languages Lecture 2 Slide L2 – 4
http://www.movabletype.org/
http://bestpractical.com/rt/
http://sourceforge.net/projects/slashcode/
Perl: Overview Applications
Perl: Applications
• Organisations using Perl
• Amazon – online retailer
http://www.amazon.co.uk
• BBC – TV/Radio/Online entertainment and journalism
http://www.bbc.co.uk
• Booking.com – hotel bookings
http://www.booking.com
• craigslist – classified ads
http://www.craigslist.org
• IMDb – movie database
http://www.imdb.com
• Monsanto – agriculture/biotech
http://www.monsanto.co.uk/
• Slashdot – technology related news
http://slashdot.org
COMP284 Scripting Languages Lecture 2 Slide L2 – 5
http://www.amazon.co.uk
http://www.bbc.co.uk
http://www.booking.com
http://www.craigslist.org
http://www.imdb.com
http://www.monsanto.co.uk/
http://slashdot.org
Perl: Overview Java vs Perl
Java versus Perl: Java
1 /* Author: Clare Dixon
2 * The HelloWorld class implements an application
3 * that prints out "Hello World".
4 */
5 public class HelloWorld {
6 // ---------------METHODS -------------
7 /* Main Method */
8 public static void main(String [] args) {
9 System.out.println("Hello World");
10 }
11 }
Edit-compile-run cycle:
1 Edit and save as HelloWorld.java
2 Compile using javac HelloWorld.java
3 Run using java HelloWorld
COMP284 Scripting Languages Lecture 2 Slide L2 – 6
Perl: Overview Java vs Perl
Java versus Perl: Perl
1 #!/usr/bin/perl
2 # Author: Ullrich Hustadt
3 # The HelloWorld script implements an application
4 # that prints out "Hello World".
5
6 print "Hello World\n";
Edit-run cycle:
1 Edit and save as HelloWorld
2 Run using perl HelloWorld
1 Edit and save as HelloWorld
2 Make it executable chmod u+x HelloWorld
This only needs to be done once!
3 Run using ./HelloWorld
COMP284 Scripting Languages Lecture 2 Slide L2 – 7
Perl: Overview Java vs Perl
Perl
• Perl borrows features from a wide range of programming languages
including imperative, object-oriented and functional languages
• Advantage: Programmers have a choice of programming styles
• Disadvantage: Programmers have a choice of programming styles
• Perl makes it easy to write completely incomprehensible code
; Documenting and commenting Perl code is very important
COMP284 Scripting Languages Lecture 2 Slide L2 – 8
Perl: Overview Java vs Perl
Perl
• Perl makes it easy to write completely incomprehensible code
; Documenting and commenting Perl code is very important
#!/usr/bin/perl
# Authors: Schwartz et al. / Ullrich Hustadt
# Text manipulation using regular expressions
#
# Retrieve the Perl documentation of function ’atan2’
@lines = ‘perldoc -u -f atan2 ‘;
# Go through the lines of the documentation , turn all text
# between angled brackets to uppercase and remove the
# character in front of the opening angled bracket , then
# print the result
foreach (@lines) {
s/\w<([^\ >]+) >/\ U$1/g;
print;
}
In the example, there are more lines of comments than there are lines of code
COMP284 Scripting Languages Lecture 2 Slide L2 – 9
Perl: Overview Java vs Perl
Perl for Java programmers
• In the following we will consider various constructs of the Perl
programming language
• numbers, strings
• variables, constants
• assignments
• control structures
• These will often be explained with reference to Java
(’like Java’, ’unlike Java’)
• Note that Perl predates Java
; common constructs are almost always inherited by both languages
from the programming language C
COMP284 Scripting Languages Lecture 2 Slide L2 – 10
Perl: Overview Java vs Perl
Perl scripts
• A Perl script consists of one or more statements and comments
; there is no need for a main function (or classes)
• Statements end in a semi-colon
• Whitespace before and in between statements is irrelevant
(This does not mean its irrelevant to someone reading your code)
• Comments start with a hash symbol # and run to the end of the line
• Comments should precede the code they are referring to
COMP284 Scripting Languages Lecture 2 Slide L2 – 11
Perl: Overview Java vs Perl
Perl scripts
• Perl statements include
• Assignments
• Control structures
Every statement returns a value
• Perl data types include
• Scalars
• Arrays / Lists
• Hashes / Associative arrays
• Perl expressions are constructed from values and variables using
operators and subroutines
• Perl expressions can have side-effects
(evaluation of an expression can change the program state)
Every expression can be turned into a statement by adding a semi-colon
COMP284 Scripting Languages Lecture 2 Slide L2 – 12
Scalars Definition
Scalar data
• A scalar is the simplest type of data in Perl
• A scalar is either
• an integer number
0 2012 -40 1_263_978
• a floating-point number
1.25 256.0 -12e19 2.4e-10
• a string
’hello world’ “hello world\n”
• Note:
• There is no ‘integer type’, ‘string type’ etc
• There are no boolean constants (true / false)
COMP284 Scripting Languages Lecture 2 Slide L2 – 13
Scalars Integers and Floating-point numbers
Integers and Floating-point numbers
• Perl provides a wide range of pre-defined mathematical functions
abs(number) absolute value
log(number) natural logarithm
random(number) random number between 0 and number
sqrt(number) square root
• Additional functions are available via the POSIX module
ceil(number) round fractions up
floor(number) round fractions down
Note: There is no pre-defined round function
use POSIX;
print ceil (4.3); // prints ’5’
print floor (4.3); // prints ’4’
• Remember: Floating-point arithmetic has its peculiarities
David Goldberg: What Every Computer Scientist Should Know About Floating-Point
Arithmetic. Computing Surveys 23(1):5–48.
http://perso.ens-lyon.fr/jean-michel.muller/goldberg.pdf
COMP284 Scripting Languages Lecture 2 Slide L2 – 14
http://perso.ens-lyon.fr/jean-michel.muller/goldberg.pdf
Scalars Integers and Floating-point numbers
Mathematical functions and Error handling
• Perl, PHP and JavaScript differ in the way they deal with applications
of mathematical functions that do not produce a number
In Perl we have
• log(0) produces an error message: Can’t take log of 0
• sqrt(-1) produces an error message: Can’t take sqrt of -1
• 1/0 produces an error message: Illegal division by zero
• 0/0 produces an error message: Illegal division by zero
and execution of a script terminates when an error occurs
• A possible way to perform error handling in Perl is as follows:
eval { … run the code here …. # try
1;
} or do { … handle the error here using $@… # catch
};
The special variable $@ contains the Perl syntax or routine
error message from the last eval, do-FILE, or require command
COMP284 Scripting Languages Lecture 2 Slide L2 – 15
Scalars Strings
Strings
Perl distinguishes between
• single-quoted strings and
• double-quoted strings
single-quoted strings double-quoted strings
(‘taken literally’) (‘interpreted’/‘evaluated’)
’hello’ ; hello
’don\’t’ ; don’t
’”hello”’ ; “hello”
’backslash\\’ ; backslash\
’glass\\table’ ; glass\table
’glass\table’ ; glass\table
“hello” ; hello
“don’t” ; don’t
“\”hello\”” ; “hello”
“backslash\\” ; backslash\
“glass\\table” ; glass\table
“glass\table” ; glass able
In Java, single quotes are used for single characters and
double quotes for strings
COMP284 Scripting Languages Lecture 2 Slide L2 – 16
Scalars Strings
Double-quoted string backslash escapes
• In a single-quoted string \t is simply a string consisting of \ and t
• In a double-quoted string \t and other backslash escapes have the
following meaning
Construct Meaning
\n Logical Newline (actual character is platform dependent)
\f Formfeed
\r Return
\t Tab
\l Lower case next letter
\L Lower case all following letters until \E
\u Upper case next letter
\U Upper case all following letters until \E
\Q Quote non-word characters by adding a backslash until \E
\E End \L, \U, \Q
COMP284 Scripting Languages Lecture 2 Slide L2 – 17
Scalars Strings
UTF-8
• Perl supports UTF-8 character encodings which give you access to
non-ASCII characters
• The pragma
use utf8;
allows you to use UTF-8 encoded characters in Perl scripts
• The function call
binmode(STDIN , “:encoding(UTF -8)”);
binmode(STDOUT , “:encoding(UTF -8)”);
ensures that UFT-8 characters are read correctly from STDIN and
printed correctly to STDOUT
• The Unicode::Normalize module enables correct decomposition
of strings containing UTF-8 encoded characters
use Unicode :: Normalize;
COMP284 Scripting Languages Lecture 2 Slide L2 – 18
Scalars Strings
UTF-8
Example:
binmode(STDOUT , “:utf8”);
print “\x{4f60}\x{597d}\x{4e16}\x{754c}\n”; # chinese
print “\x{062d}\x{fef0}\n”; # arabic
For further details see Schwartz et al., Appendix C
COMP284 Scripting Languages Lecture 2 Slide L2 – 19
Scalars Strings
String operators and automatic conversion
• Two basic operations on strings are
• string concatenation.
“hello” . “world” ; “helloworld”
“hello” . ’ ’ . “world” ; ’hello world’
“\Uhello” . ’ \LWORLD ’ ; ’HELLO \LWORLD’
• string repetition x:
“hello ” x 3 ; “hello hello hello ”
• These operations can be combined
“hello ” . “world ” x 2 ; “hello world world ”
• Perl automatically converts between strings and numbers
2 . ” worlds” ; “2 worlds”
“2” * 3 ; 6
2e-1 x 3 ; “0.20.20.2” (“0.2” repeated three times)
“hello”* 3 ; 0
COMP284 Scripting Languages Lecture 2 Slide L2 – 20
Scalars ‘Booleans’
‘Booleans’
• Unlike Java, Perl does not have a boolean datatype
• Instead the values
0 # zero
’’ # empty string
’0’ # string consisting of zero
undef # undefined
() # empty list
all represent false while all other values represent true
COMP284 Scripting Languages Lecture 2 Slide L2 – 21
Scalars ‘Booleans’
‘Boolean operators’
• Perl offers the same short-circuit boolean operators as Java: &&, ||, !
Alternatively, and, or, not can be used
A B (A && B)
true true B (true)
true false B (false)
false true A (false)
false false A (false)
A B (A || B)
true true A (true)
true false A (true)
false true B (true)
false false B (false)
A (! A)
true ’’ (false)
false 1 (true)
• Note that this means that && and || are not commutative, that is,
(A && B) is not the same as (B && A)
($denom != 0) && ($num / $denom > 10)
COMP284 Scripting Languages Lecture 2 Slide L2 – 22
Scalars Comparisons
Comparison operators
Perl distinguishes between numeric comparison and string comparison
Comparison Numeric String
Equal == eq
Not equal != ne
Less than < lt
Greater than > gt
Less than or equal to <= le
Greater than or equal to >= ge
Examples
35 == 35.0 # true
’35’ eq ’35.0’ # false
’35’ == ’35.0’ # true
35 < 35.0 # false ’35’ lt ’35.0’ # true ’ABC’ eq "\Uabc" # true COMP284 Scripting Languages Lecture 2 Slide L2 – 23 Variables, Constants, and Assignments Variables Scalar variables • Scalar variables start with $ followed by a Perl identifier • A Perl identifier consists of letters, digits, and underscores, but cannot start with a digit Perl identifiers are case sensitive • In Perl, a variable does not have to be declared before it can be used • Scalar variables can store any scalar value (there are no ‘integer variables’ versus ‘string variables’) COMP284 Scripting Languages Lecture 2 Slide L2 – 24 Variables, Constants, and Assignments Variables Scalar variables • A variable also does not have to be initialised before it can be used, although initialisation is a good idea • Uninitialised variables have the special value undef However, undef acts like 0 for numeric variables and like ’’ for string variables if an uninitialised variable is used in an arithmetic or string operation • To test whether a variable has value undef use the routine defined $s1 = ""; print ’$s1 eq undef: ’,($s1 eq undef) ? ’TRUE’:’FALSE’,"\n"; print ’$s1 defined: ’,(defined($s1)) ? ’TRUE’:’FALSE’,"\n"; print ’$s2 defined: ’,(defined($s2)) ? ’TRUE’:’FALSE’,"\n"; $s1 eq undef: TRUE $s1 defined: TRUE $s2 defined: FALSE COMP284 Scripting Languages Lecture 2 Slide L2 – 25 Variables, Constants, and Assignments Variables Special Variables • Perl has a lot of ‘pre-defined’ variables that have a particular meaning and serve a particular purpose Variable Explanation $_ The default or implicit variable @_ Subroutine parameters $a, $b sort comparison routine variables $& the string matched by the last successful pattern match $/ input record separator, newline by default $\ output record separator, undef by default $] version of Perl used • For a full list see https://perldoc.perl.org/perlvar.html#SPECIAL-VARIABLES COMP284 Scripting Languages Lecture 2 Slide L2 – 26 https://perldoc.perl.org/perlvar.html#SPECIAL-VARIABLES Variables, Constants, and Assignments Constants Constants Perl offers three different ways to declare constants • Using the constant pragma: use constant PI => 3.14159265359;
(A pragma is a module which influences some aspect of the
compile time or run time behaviour of Perl)
• Using the Readonly module:
use Readonly;
Readonly $PI => 3.14159265359;
• Using the Const::Fast module:
use Const::Fast;
const $PI => 3.14159265359;
With our current Perl installation only constant works
; variable interpolation with constants does not work
COMP284 Scripting Languages Lecture 2 Slide L2 – 27
Variables, Constants, and Assignments Assignments
Assignments
• Just like Java, Perl uses the equality sign = for assignments:
$student_id = 200846369;
$name = “Jan Olsen”;
$student_id = “E00481370”;
But no type declaration is required and the same variable can hold a
number at one point and a string at another
• An assignment also returns a value,
namely (the final value of) the variable on the left
; enables us to use an assignment as an expressions
Example:
$b = ($a = 0) + 1;
# $a has value 0
# $b has value 1
COMP284 Scripting Languages Lecture 2 Slide L2 – 28
Variables, Constants, and Assignments Assignments
Binary assignments
There are also binary assignment operators that serve as shortcuts for
arithmetic and string operations
Binary assignment Equivalent assignment
$a += $b $a = $a + $b
$a -= $b $a = $a – $b
$a *= $b $a = $a * $b
$a /= $b $a = $a / $b
$a %= $b $a = $a % $b
$a **= $b $a = $a ** $b
$a .= $b $a = $a . $b
Example:
# Convert Fahrenheit to Celsius:
# Subtract 32, then multiply by 5, then divide by 9
$temperature = 105; # temperature in Fahrenheit
($temperature -= 32) *= 5/9; # converted to Celsius
COMP284 Scripting Languages Lecture 2 Slide L2 – 29
Variables, Constants, and Assignments Assignments
Variable declarations
• In Perl, variables can be declared using the my function
(Remember: This is not a requirement)
• The pragma
use strict;
enforces that all variables must be declared before their use,
otherwise a compile time error is raised
Example:
use strict;
$studentsOnCOMP284 = 133;
Global symbol “$studentOnCOMP284” requires explicit
package name at ./ script line 2.
Execution of ./ script aborted due to compilation errors.
use strict;
my $studentsOnCOMP281;
$studentsOnCOMP281 = 154;
my $studentsOnCOMP283 = 53;
COMP284 Scripting Languages Lecture 2 Slide L2 – 30
Variables, Constants, and Assignments Variable interpolation
Variable interpolation
Variable interpolation
Any scalar variable name in a double quoted string
is (automatically) replaced by its current value
Example:
$actor = “Jeff Bridges”;
$prize = “Academy Award for Best Actor”;
$year = 2010;
print “1: $actor won the $prize in $year\n”;
print “2: “,$actor ,” won the “,$prize ,” in “,$year ,”\n”;
Output:
1: Jeff Bridges won the Academy Award for Best Actor in 2010
2: Jeff Bridges won the Academy Award for Best Actor in 2010
COMP284 Scripting Languages Lecture 2 Slide L2 – 31
Variables, Constants, and Assignments Variable interpolation
Revision
Read
• Chapter 2: Scalar Data
of
R. L. Schwartz, brian d foy, T. Phoenix:
Learning Perl.
O’Reilly, 2011.
Harold Cohen Library: 518.579.86.S39 or e-book
COMP284 Scripting Languages Lecture 2 Slide L2 – 32
COMP284 Scripting Languages
Lecture 3: Perl (Part 2)
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
6 Control structures
Conditional statements
Switch statements
While- and Until-loops
For-loops
7 Lists and Arrays
Identifiers
List literals
Contexts
List and array functions
Foreach-loops
8 Hashes
Identifiers
Basic hash operations
Foreach
COMP284 Scripting Languages Lecture 3 Slide L3 – 1
Control structures Conditional statements
Control structures: conditional statements
The general format of conditional statements is very similar to that in
Java:
if (condition) {
statements
} elsif (condition) {
statements
} else {
statements
}
• condition is an arbitrary expression
• the elsif-clauses is optional and there can be more than one
• the else-clause is optional but there can be at most one
• in contrast to Java, the curly brackets must be present
even if statements consist only of a single statement
COMP284 Scripting Languages Lecture 3 Slide L3 – 2
Control structures Conditional statements
Control structures: conditional statements
• Perl also offers two shorter conditional statements:
statement if (condition );
and
statement unless (condition );
• In analogy to conditional statements
Perl offers conditional expressions:
condition ? if_true_expr : if_false_expr
Examples:
$descr = ($distance < 50) ? "near" : "far";
$size = ($width < 10) ? "small" :
($width < 20) ? "medium" :
"large";
COMP284 Scripting Languages Lecture 3 Slide L3 – 3
Control structures Conditional statements
Blocks
• A sequence of statements in curly brackets is a block
; an alternative definition of conditional statements is
if (condition) block
elsif (condition) block
else block
• In
statement if (condition );
statement unless (condition );
only a single statement is allowed
but do block counts as a single statement,
so we can write
do block if (condition );
do block unless (condition );
COMP284 Scripting Languages Lecture 3 Slide L3 – 4
Control structures Switch statements
Control structures: switch statement/expression
Starting with Perl 5.10 (released Dec 2007), the language includes a
switch statement and corresponding switch expression
But these are considered experimental and need to be enabled explicitly
Example:
use feature "switch";
given ($month) {
when ([1,3,5,7,8,10,12]) { $days = 31 }
when ([4 ,6 ,9,11]) { $days = 30 }
when (2) { $days = 28 }
default { $days = 0 }
}
Note: No explicit break statement is needed
COMP284 Scripting Languages Lecture 3 Slide L3 – 5
Control structures While- and Until-loops
Control structures: while- and until-loops
• Perl offers while-loops and until-loops
while (condition) {
statements
}
until (condition) {
statements
}
• A ‘proper’ until-loop where the loop is executed at least once
can be obtained as follows
do { statements } until (condition );
The same construct also works for if, unless and while
In case there is only a single statement it is also possible to write
statement until (condition );
Again this also works for if, unless and while
COMP284 Scripting Languages Lecture 3 Slide L3 – 6
Control structures For-loops
Control structures: for-loops
• for-loops in Perl take the form
for (initialisation; test; increment) {
statements
}
Again, the curly brackets are required even if the body of the loop only
consists of a single statement
• Such a for-loop is equivalent to the following while-loop:
initialisation;
while (test) {
statements;
increment;
}
COMP284 Scripting Languages Lecture 3 Slide L3 – 7
Lists and Arrays Identifiers
Lists and Arrays
• A list is an ordered collection of scalars
• An array (array variable) is a variable that contains a list
• Array variables start with @ followed by a Perl identifier
@identifier
An array variable denotes the entire list stored in that variable
• Perl uses
$identifier[index]
to denote the element stored at position index in @identifier
The first array element has index 0
• Note that
$identifier
@identifier
are two unrelated variables (but this situation should be avoided)
COMP284 Scripting Languages Lecture 3 Slide L3 – 8
Lists and Arrays List literals
List literals
• A list can be specified by a list literal, a comma-separated list of values
enclosed by parentheses
(1, 2, 3)
("adam", "ben", "colin", "david")
("adam", 1, "ben", 3)
( )
(1..10 , 15, 20..30)
($start ..$end)
• List literals can be assigned to an array:
@numbers = (1..10 , 15, 20..30);
@names = ("adam", "ben", "colin", "david");
• Examples of more complex assignments, involving array concatenation:
@numbers = (1..10 , undef , @numbers , ( ));
@names = (@names ,@numbers );
• Note that arrays do not have a pre-defined size/length
COMP284 Scripting Languages Lecture 3 Slide L3 – 9
Lists and Arrays List literals
Size of an array
• There are three different ways to determine the size of an array
$arraySize = scalar(@array);
$arraySize = @array;
$arraySize = $#array + 1;
• One can access all elements of an array using indices
in the range 0 to $#array
• But Perl also allows negative array indices:
The expression $array[-index]
is equivalent to $array[scalar(@array)-index]
Example:
$array[-1] is the same as $array[scalar(@array)-1]
is the same as $array[$#array]
that is the last element in @array
COMP284 Scripting Languages Lecture 3 Slide L3 – 10
Lists and Arrays List literals
Array index out of bound
• Perl, in contrast to Java, allows you to access array indices that are
out of bounds
• The value undef will be returned in such a case
@array = (0, undef , 22, 33);
print ’$array [1] = ’,$array [1], ’, which ’,
(defined($array [1]) ? "IS NOT" : "IS", "undef\n";
print ’$array [5] = ’,$array [5], ’, which ’,
(defined($array [5]) ? "IS NOT" : "IS", "undef\n";
$array [1] = , which IS undef
$array [5] = , which IS undef
• The function exists can be used to determine whether an array index
is within bounds and has a value (including undef) associated with it
print ’$array [1] exists: ’,exists($array [1]) ? "T":"F","\n";
print ’$array [5] exists: ’,exists($array [5]) ? "T":"F","\n";
$array [1] exists: T
$array [5] exists: F
COMP284 Scripting Languages Lecture 3 Slide L3 – 11
Lists and Arrays Contexts
Scalar context versus list context
• Scalar context
when an expression is used as an argument of an operation that requires
a scalar value, the expression will be evaluated in a scalar context
Example:
$arraySize = @array;
; @array stores a list, but returns the number of elements of @array
in a scalar context
• List context
when an expression is used as an argument of an operation that requires
a list value, the expression will be evaluated in a list context
Example:
@sorted = sort 5;
; A single scalar value is treated as a list with one element in a
list context
COMP284 Scripting Languages Lecture 3 Slide L3 – 12
Lists and Arrays Contexts
Scalar context versus list context
Expressions behave differently in different contexts following these rules:
• Some operators and functions automatically return different values in
different contexts
$line =
@lines =
• If an expression returns a scalar value in a list context, then by default
Perl will convert it into a list value with the returned scalar value being
the one and only element
• If an expression returns a list value in a scalar context, then by default
Perl will convert it into a scalar value by take the last element of the
returned list value
COMP284 Scripting Languages Lecture 3 Slide L3 – 13
Lists and Arrays List and array functions
List functions
Function Semantics
grep(expr,list) in a list context, returns those elements of
list for which expr is true;
in a scalar context, returns the number of
times the expression was true
join(string,list) returns a string that contains the elements
of list connected through a separator
string
reverse(list) returns a list with elements in reverse order
sort(list) returns a list with elements sorted in
standard string comparison order
split(/regexpr/,string) returns a list obtained by splitting string
into substring using regexpr as separator
(list) x number returns a list composed of number copies
of list
COMP284 Scripting Languages Lecture 3 Slide L3 – 14
Lists and Arrays List and array functions
Array functions: push, pop, shift, unshift
Perl has no stack or queue data structures,
but has stack and queue functions for arrays:
Function Semantics
push(@array1,value)
push(@array1,list)
appends an element or an entire list to the
end of an array variable;
returns the number of elements in the
resulting array
pop(@array1) extracts the last element from an array
and returns it
shift(@array1) shift extracts the first element of an array
and returns it
unshift(@array1,value)
unshift(@array1,list)
insert an element or an entire list at the
start of an array variable;
returns the number of elements in the
resulting array
COMP284 Scripting Languages Lecture 3 Slide L3 – 15
Lists and Arrays List and array functions
Array operators: push, pop, shift, unshift
Example:
1 @planets = (“earth”);
2 unshift(@planets ,”mercury”,”venus”);
3 push(@planets ,”mars”,”jupiter”,”saturn”);
4 print “Array\@1: “, join(” “,@planets),”\n”;
5 $last = pop(@planets );
6 print “Array\@2: “, join(” “,@planets),”\n”;
7 $first = shift(@planets );
8 print “Array\@3: “, join(” “,@planets),”\n”;
9 print ” \@4: “,$first , ” “,$last , “\n”;
Output:
Array@1: mercury venus earth mars jupiter saturn
Array@2: mercury venus earth mars jupiter
Array@3: venus earth mars jupiter
@4: mercury saturn
COMP284 Scripting Languages Lecture 3 Slide L3 – 16
Lists and Arrays List and array functions
Array operators: delete
• It is possible to delete array elements
• delete($array[index])
– removes the value stored at index in @array and returns it
– only if index equals $#array will the array’s size shrink to the
position of the highest element that returns true for exists()
@array = (0, 11, 22, 33);
delete($array [2]);
print ’$array [2] exists: ’,exists($array [2])?”T”:”F”, “\n”;
print ’Size of $array: ’ ,$#array+1,”\n”;
delete($array [3]);
print ’$array [3] exists: ’,exists($array [3])?”T”:”F”, “\n”;
print ’Size of $array: ’ ,$#array+1,”\n”;
$array [2] exists: F
Size of $array: 4
$array [3] exists: F
Size of $array: 2
COMP284 Scripting Languages Lecture 3 Slide L3 – 17
Lists and Arrays Foreach-loops
Control structures: foreach-loop
Perl provides the foreach-construct to ‘loop’ through the elements of a
list
foreach $variable (list) {
statements
}
where $variable, the foreach-variable, stores a different element of the
list in each iteration of the loop
Example:
@my_list = (1..5 ,20 ,11..18);
foreach $number (@my_list) {
$max = $number if (! defined($max) || $number > $max);
}
print(“Maximum number in “,join(’,’,@my_list),” is $max\n”);
Output:
Maximum number in 1,2,3,4,5,20,11,12,13,14,15,16,17,18 is 20
COMP284 Scripting Languages Lecture 3 Slide L3 – 18
Lists and Arrays Foreach-loops
Control structures: foreach-loop
Changing the value of the foreach-variable changes the element of the list
that it currently stores
Example:
@my_list = (1..5 ,20 ,11..18);
print “Before: “.join(“, “,@my_list ).”\n”;
foreach $number (@my_list) {
$number ++;
}
print “After: “.join(“, “,@my_list ).”\n”;
Output:
Before: 1, 2, 3, 4, 5, 20, 11, 12, 13, 14, 15, 16, 17, 18
After: 2, 3, 4, 5, 6, 21, 12, 13, 14, 15, 16, 17, 18, 19
Note: If no variable is specified, then the special variable $_ will be
used to store the array elements
COMP284 Scripting Languages Lecture 3 Slide L3 – 19
Lists and Arrays Foreach-loops
Control structures: foreach-loop
An alternative way to traverse an array is
foreach $index (0..$#array) {
statements
}
where an element of the array is then accessed using $array[$index] in
statements
Example:
@my_list = (1..5 ,20 ,11..18);
foreach $index (0..$# my_list) {
$max = $my_list[$index] if ($my_list[$index] > $max);
}
print(“Maximum number in “,join(’,’,@my_list),” is $max\n”);
COMP284 Scripting Languages Lecture 3 Slide L3 – 20
Lists and Arrays Foreach-loops
Control structures: foreach-loop
• In analogy to while- and until-loops, there are the following variants of
foreach-loops:
do { statements } foreach list;
statement foreach list;
In the execution of the statements within the loop, the special variable
$_ will be set to consecutive elements of list
• Instead of foreach we can also use for:
do { statements } for list;
statement for list;
Example:
print “Hello $_!\n” foreach (“Peter”,”Paul”,
“Mary”);
COMP284 Scripting Languages Lecture 3 Slide L3 – 21
Lists and Arrays Foreach-loops
Control structures: last and next
• The last command can be used in while-, until-, and foreach-loops
and discontinues the execution of a loop
while ($value = shift($data)) {
$written = print(FILE $value );
if (! $written) { last; }
}
# Execution of ‘last’ takes us here
• The next command stops the execution of the current iteration
of a loop and moves the execution to the next iteration
foreach $x ( -2..2) {
if ($x == 0) { next; }
printf(“10 / %2d = %3d\n”,$x ,(10/ $x));
}
10 / -2 = -5
10 / -1 = -10
10 / 1 = 10
10 / 2 = 5
COMP284 Scripting Languages Lecture 3 Slide L3 – 22
Hashes Identifiers
Hashes
• A hash is a data structure similar to an array but it associates scalars
with a string instead of a number
• Alternatively, a hash can be seen as a partial function mapping strings
to scalars
• Remember that Perl can auto-magically convert any scalar into a string
• Hash variables start with a percent sign followed by a Perl identifier
%identifier
A hash variable denotes the entirety of the hash
• Perl uses
$identifier{key}
where key is a string, to refer to the value associated with key
COMP284 Scripting Languages Lecture 3 Slide L3 – 23
Hashes Identifiers
Hashes
• Note that
$identifier
%identifier
are two unrelated variables (but this situation should be avoided)
• An easy way to print all key-value pairs of a hash %hash is the following
use Data:: Dumper;
$Data :: Dumper ::Terse = 1;
print Dumper \%hash;
Note the use of \%hash instead of %hash
(\%hash is a reference to %hash)
Data::Dumper can produce string representations for
arbitrary Perl data structures
COMP284 Scripting Languages Lecture 3 Slide L3 – 24
Hashes Basic hash operations
Basic hash operations
• Initialise a hash using a list of key-value pairs
%hash = (key1 , value1 , key2 , value2 , …);
• Initialise a hash using a list in big arrow notation
%hash = (key1 => value1 , key2 => value2 , …);
• Associate a single value with a key
$hash{key} = value;
• Remember that undef is a scalar value
$hash{key} = undef;
extends a hash with another key but unknown value
COMP284 Scripting Languages Lecture 3 Slide L3 – 25
Hashes Basic hash operations
Basic hash operations
• One can use the exists or defined function to check
whether a key exists in a hash:
if (exists $hash{key}) { … }
Note that if $hash{key} eq undef, then exists $hash{key} is true
• The delete function removes a given key and its corresponding value
from a hash:
delete($hash{key});
After executing delete($hash{key}), exists $hash{key} will be
false
• The undef function removes the contents and memory allocated to
a hash:
undef %hash
COMP284 Scripting Languages Lecture 3 Slide L3 – 26
Hashes Basic hash operations
Basic hash operations
• It is also possible to assign one hash to another
%hash1 = %hash2;
In contrast to C or Java this operation creates a copy of %hash2
that is then assigned to %hash1
Example:
%hash1 = (’a’ => 1, ’b’ => 2);
%hash2 = %hash1;
$hash1{’b’} = 4;
print “\$hash1{’b ’} = $hash1{’b ’}\n”;
print “\$hash2{’b ’} = $hash2{’b ’}\n”;
Output:
$hash1{’b’} = 4
$hash2{’b’} = 2
COMP284 Scripting Languages Lecture 3 Slide L3 – 27
Hashes Foreach
The each, keys, and values functions
each %hash returns a 2-element list consisting of the key and
value for the next element of %hash, so that one can
iterate over it
values %hash returns a list consisting of all the values of %hash,
resets the internal iterator for %hash
keys %hash returns a list consisting of all keys of %hash,
resets the internal iterator for %hash
Examples:
while ( ($key ,$value) = each %hash ) {
statements
}
foreach $key (sort keys %hash) {
$value = $hash{$key};
}
COMP284 Scripting Languages Lecture 3 Slide L3 – 28
Hashes Foreach
Example: Two-dimensional hash as a ‘database’
1 use List::Util “sum”;
2 $name{’200846369 ’} = ’Jan Olsen ’;
3 $marks{’200846369 ’}{’COMP201 ’} = 61;
4 $marks{’200846369 ’}{’COMP207 ’} = 57;
5 $marks{’200846369 ’}{’COMP213 ’} = 43;
6 $marks{’200846369 ’}{’COMP219 ’} = 79;
7
8 $average = sum(values($marks{’200846369 ’}))/
9 scalar(values($marks{’200846369 ’});
10 print(“avg: $average\n”);
Output:
avg: 60
COMP284 Scripting Languages Lecture 3 Slide L3 – 29
Hashes Foreach
Example: Frequency of words
1 # Establish the frequency of words in a string
2 $string = “peter paul mary paul jim mary paul”;
3
4 # Split the string into words and use a hash
5 # to accumulate the word count for each word
6 ++ $count{$_} foreach split (/\s+/,$string );
7
8 # Print the frequency of each word found in the
9 # string
10 while ( ($key ,$value) = each %count ) {
11 print(“$key => $value; “);
12 }
Output:
jim => 1; peter => 1; mary => 2; paul => 3
COMP284 Scripting Languages Lecture 3 Slide L3 – 30
Hashes Foreach
Revision
Read
• Chapter 3: Lists and Arrays
• Chapter 6: Hashes
of
R. L. Schwartz, brian d foy, T. Phoenix:
Learning Perl.
O’Reilly, 2011.
Harold Cohen Library: 518.579.86.S39 or e-book
COMP284 Scripting Languages Lecture 3 Slide L3 – 31
COMP284 Scripting Languages
Lecture 4: Perl (Part 3)
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
9 Regular expressions (1)
Introduction
Characters
Character classes
Quantifiers
COMP284 Scripting Languages Lecture 4 Slide L4 – 1
Regular expressions (1) Introduction
Regular expressions: Motivation
Suppose you are testing the performance of a new sorting algorithm by
measuring its runtime on randomly generated arrays of numbers
of a given length:
Generating an unsorted array with 10000 elements took 1.250 seconds
Sorting took 7.220 seconds
Generating an unsorted array with 10000 elements took 1.243 seconds
Sorting took 10.486 seconds
Generating an unsorted array with 10000 elements took 1.216 seconds
Sorting took 8.951 seconds
Your task is to write a program that determines the average runtime of
the sorting algorithm:
Average runtime for 10000 elements is 8.886 seconds
Solution: The regular expression /^Sorting took (\d+\.\d+) seconds/
allows us to get the required information
; Regular expressions are useful for information extraction
COMP284 Scripting Languages Lecture 4 Slide L4 – 2
Regular expressions (1) Introduction
Regular expressions: Motivation
Suppose you have recently taken over responsibility for a company’s
website. You note that their HTML files contain a large number of
URLs containing superfluous occurrences of ‘..’, e.g.
http://www.myorg.co.uk/info/refund/../vat.html
Your task is to write a program that replaces URLs like these with
equivalent ones without occurrences of ‘..’:
http://www.myorg.co.uk/info/vat.html
while making sure that relative URLs like
../video/disk.html
are preserved
Solution: s!/[^\/]+/\.\.!!; removes a superfluous dot-segment
; Substitution of regular expressions is useful for text manipulation
COMP284 Scripting Languages Lecture 4 Slide L4 – 3
Regular expressions (1) Introduction
Regular expressions: Introductory example
\Ahttps ?:\/\/[^\/]+\/.\w.\/( cat|dog )\/\1
• \A is an assertion or anchor
• h, t, p, s, :, \/, c, a, t, d, o, g are characters
• ? and + are quantifiers
• [^\/] is a character class
• . is a metacharacter and \w is a special escape
• (cat|dog) is alternation within a capture group
• \1 is a backreference to a capture group
COMP284 Scripting Languages Lecture 4 Slide L4 – 4
Regular expressions (1) Introduction
Pattern match operation
• To match a regular expession regexpr against the special variable $_
simply use one of the expressions /regexpr/ or m/regexpr/
• This is called a pattern match
• $_ is the target string of the pattern match
• In a scalar context a pattern match returns true (1) or false (’’)
depending on whether regexpr matches the target string
if (/\ Ahttps ?:\/\/[^\/]+\/.\w.\/( cat|dog )\/\1/) {
… }
if (m/\ Ahttps ?:\/\/[^\/]+\/.\w.\/( cat|dog )\/\1/) {
… }
COMP284 Scripting Languages Lecture 4 Slide L4 – 5
Regular expressions (1) Characters
Regular expressions: Characters
The simplest regular expression just consists of a sequence of
• alphanumberic characters and
• non-alphanumeric characters escaped by a backslash:
that matches exactly this sequence of characters occurring as a substring
in the target string
$_ = “ababcbcdcde”;
if (/cbc/) { print “Match\n”} else { print “No match\n” }
Output:
Match
$_ = “ababcbcdcde”;
if (/dbd/) { print “Match\n”} else { print “No match\n” }
Output:
No match
COMP284 Scripting Languages Lecture 4 Slide L4 – 6
Regular expressions (1) Characters
Regular expressions: Special variables
• Often we do not just want to know whether a regular expession matches
a target string, but retrieve additional information
• The special variable $-[0] can be used to retrieve the start position of
the match
Note that positions in strings are counted starting with 0
• The special variable $+[0] can be used to retrieve the first position
after the match
• The special variable $& returns the match itself
$_ = “ababcbcdcde”;
if (/cbc/) { print “Match found at position $-[0]: $&\n”}
Output:
Match found at position 4: cbc
COMP284 Scripting Languages Lecture 4 Slide L4 – 7
Regular expressions (1) Characters
Regular expressions: Special escapes
There are various special escapes and metacharacters that match more
then one character:
. Matches any character except \n
\w Matches a ‘word’ character (alphanumeric
plus ‘_’, plus other connector punctuation
characters plus Unicode characters
\W Matches a non-‘word’ character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a decimal digit character
\D Match a non-digit character
\p{UnicodeProperty} Match UnicodeProperty characters
\P{UnicodeProperty} Match non-UnicodeProperty characters
COMP284 Scripting Languages Lecture 4 Slide L4 – 8
Regular expressions (1) Characters
Regular expressions: Unicode properties
• Each unicode character has one or more properties,
for example, which script it belongs it
• \p{UnicodeProperty} matches all characters that have a particular
property
• \P{UnicodeProperty} matches those that do not
• Examples of unicode properties are
Arabic Arabic characters
ASCII ASCII characters
Currency_Symbol Currency symbols
Digit Digits in all scripts
Greek Greek characters
Han Chinese kanxi or Japanese kanji characters
Space Whitespace characters
See http://perldoc.perl.org/perluniprops.html for a complete list
COMP284 Scripting Languages Lecture 4 Slide L4 – 9
http://perldoc.perl.org/perluniprops.html
Regular expressions (1) Character classes
Regular expressions: Character class
• A character class, a list of characters, special escapes, metacharacters
and unicode properties enclosed in square brackets, matches any single
character from within the class,
for example, [ad\t\n\-\\09]
• One may specify a range of characters with a hyphen -,
for example, [b-u]
• A caret ^ at the start of a character class negates/complements it,
that is, it matches any single character that is not from within the class,
for example, [^01a-z]
$_ = “ababcbcdcde”;
if (/[bc][b-e][^ bcd ]/) {
print “Match at positions $-[0] to “,$+[0]-1,”: $&\n”};
Output:
Match at positions 8 to 10: cde
COMP284 Scripting Languages Lecture 4 Slide L4 – 10
Regular expressions (1) Quantifiers
Quantifiers
• The constructs for regular expressions that we have so far are not
sufficient to match, for example, natural numbers of arbitrary size
• Also, writing a regular expressions for, say, a nine digit number
would be tedious
This is made possible with the use of quantifiers
regexpr* Match regexpr 0 or more times
regexpr+ Match regexpr 1 or more times
regexpr? Match regexpr 1 or 0 times
regexpr{n} Match regexpr exactly n times
regexpr{n,} Match regexpr at least n times
regexpr{n,m} Match regexpr at least n but not more than m times
Quantifiers are greedy by default and match the longest leftmost sequence
of characters possible
COMP284 Scripting Languages Lecture 4 Slide L4 – 11
Regular expressions (1) Quantifiers
Quantifiers
regexpr* Match regexpr 0 or more times
regexpr+ Match regexpr 1 or more times
regexpr? Match regexpr 1 or 0 times
regexpr{n} Match regexpr exactly n times
regexpr{n,} Match regexpr at least n times
regexpr{n,m} Match regexpr at least n but not more than m times
Example:
$_ = “Sorting took 10.486 seconds”;
if (/\d+\.\d+/) {
print “Match at positions $-[0] to “,$+[0]-1,”: $&\n”};
$_ = “E00481370”;
if (/[A-Z]0{2}(\d+)/) {
print “Match at positions $-[1] to “,$+[1]-1,”: $1\n”};
Output:
Match at positions 13 to 18: 10.486
Match at positions 3 to 8: 481370
COMP284 Scripting Languages Lecture 4 Slide L4 – 12
Regular expressions (1) Quantifiers
Quantifiers
Example:
$_ = “E00481370”;
if (/\d+/) {
print “Match at positions $-[0] to “,$+[0]-1,”: $&\n”};
Output:
Match at positions 1 to 8: 00481370
• The regular expression \d+ matches 1 or more digits
• As the example illustrates, the regular expression \d+
• matches as early as possible
• matches as many digits as possible
; quantifiers are greedy by default
COMP284 Scripting Languages Lecture 4 Slide L4 – 13
Regular expressions (1) Quantifiers
Revision
Read
• Chapter 7: In the World of Regular Expressions
• Chapter 8: Matching with Regular Expressions
of
R. L. Schwartz, brian d foy, T. Phoenix:
Learning Perl.
O’Reilly, 2011.
• http://perldoc.perl.org/perlre.html
• http://perldoc.perl.org/perlretut.html
• http://www.perlfect.com/articles/regextutor.shtml
COMP284 Scripting Languages Lecture 4 Slide L4 – 14
http://perldoc.perl.org/perlre.html
http://perldoc.perl.org/perlretut.html
http://www.perlfect.com/articles/regextutor.shtml
COMP284 Scripting Languages
Lecture 5: Perl (Part 4)
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
10 Regular expressions (2)
Capture groups
Alternations
Anchors
Modifiers
Binding operator
COMP284 Scripting Languages Lecture 5 Slide L5 – 1
Regular expressions (2) Capture groups
Regular expressions: Capture groups and backreferences
• We often encounter situations where we want to identify the repetition
of the same or similar text, for example, in HTML markup:
…
• We might also not just be interested in the repeating text itself,
but the text between or outside the repetition
• We can characterise each individual example above
using regular expressions:
.* <\/ strong >
but we cannot characterise both without losing fidelity, for example:
<\w+>.*<\/\w+>
does not capture the ‘pairing’ of HTML tags
COMP284 Scripting Languages Lecture 5 Slide L5 – 2
Regular expressions (2) Capture groups
Regular expressions: Capture groups
The solution are capture groups and backreferences
(regexpr) creates a capture group
(?
(?:regexpr) creates a non-capturing group
\N, \gN, \g{N} backreference to capture group N
(where N is a natural number)
\g{name} backreference to a named capture group
Examples:
1 /Sorting took (\d+\.\d+) seconds/
2 /<(\w+)>.*<\/\1 >/
3 /([A-Z])0{2}(\d+)/
4 /(?
5 /((?
COMP284 Scripting Languages Lecture 5 Slide L5 – 3
Regular expressions (2) Capture groups
Regular expressions: Capture groups
Via capture variables the strings matched by a capture group are also
available outside the pattern in which they are contained
$N string matched by capture group N
(where N is a natural number)
$+{name} string matched by a named capture group
The matched strings are available until the end of the enclosing
code block or until the next successful match
Example:
$_ = “Yabba dabba doo”;
if (/((?
print “Match found: $1\n” }
Output:
Match found: abba
COMP284 Scripting Languages Lecture 5 Slide L5 – 4
Regular expressions (2) Alternations
Regular expressions: Alternations
• The regular expression regexpr1|regexpr2 matches
if either regexpr1 or regexpr2 matches
This type of regular expression is called an alternation
• Within a larger regular expression we need to enclose alternations
in a capture group or non-capturing group:
(regexpr1|regexpr2) or (?:regexpr1|regexpr2)
Examples:
1 /Mr|Ms|Mrs|Dr/
2 /cat|dog|bird/
3 /(?: Bill|Hillary) Clinton/
COMP284 Scripting Languages Lecture 5 Slide L5 – 5
Regular expressions (2) Alternations
Regular expressions: Alternations
• The order of expressions in an alternation only matters
if one expression matches a sub-expression of another
Example:
1 $_ = “cats and dogs”;
2 if (/( cat|dog|bird )/) { print “Match 1: $1\n” }
3 if (/( dog|cat|bird )/) { print “Match 2: $1\n” }
4 if (/( dog|dogs )/) { print “Match 3: $1\n” }
5 if (/( dogs|dog )/) { print “Match 4: $1\n” }
Output:
Match 1: cat
Match 2: cat
Match 3: dog
Match 4: dogs
COMP284 Scripting Languages Lecture 5 Slide L5 – 6
Regular expressions (2) Anchors
Regular expressions: Anchors
Anchors allow us to fix where a match has to start or end
\A Match only at string start
^ Match only at string start (default)
Match only at a line start (in //m)
\Z Match only at string end modulo a preceding \n
\z Match only at string end
$ Match only at string end modulo a preceding \n
Match only at a line end (in //m)
\b Match word boundary (between \w and \W)
\B Match except at word boundary
Example:
$_ = “The girl who\nplayed with fire\n”;
if (/fire\z/) { print “‘fire’ at string end\n” }
if (/fire\Z/) { print “‘fire’ at string end modulo \\n\n” }
‘fire’ at string end modulo \n
COMP284 Scripting Languages Lecture 5 Slide L5 – 7
Regular expressions (2) Modifiers
Regular expressions: Modifiers
Modifiers change the interpretation of certain characters in a regular
expression or the way in which Perl finds a match for a regular expression
/ / Default
‘.’ matches any character except ‘\n’
‘^’ matches only at string start
‘$’ matches only at string end modulo preceding \n
/ /s Treat string as a single long line
‘.’ matches any character including ‘\n’
‘^’ matches only at string start
‘$’ matches only at string end modulo preceding \n
/ /m Treat string as a set of multiple lines
‘.’ matches any character except ‘\n’
‘^’ matches at a line start
‘$’ matches at a line end
COMP284 Scripting Languages Lecture 5 Slide L5 – 8
Regular expressions (2) Modifiers
Regular expressions: Modifiers
Modifiers change the interpretation of certain characters in a regular
expression or the way in which Perl finds a match for a regular expression
/ /sm Treat string as a single long line, but detect multiple lines
‘.’ matches any character including ‘\n’
‘^’ matches at a line start
‘$’ matches at a line end
/ /i perform a case-insensitive match
Example:
$_ = “bill\nClinton”;
if (/( Bill|Hillary ). Clinton )/smi) { print “Match: $1\n” }
Output:
Match: bill
Clinton
COMP284 Scripting Languages Lecture 5 Slide L5 – 9
Regular expressions (2) Modifiers
Regular expressions: Modifiers (/ /g and / /c)
Often we want to process all matches for a regular expression,
but the following code has not the desired effect
$_ = “11 22 33”;
while (/\d+/) { print “Match starts at $ -[0]: $&\n” }
The code above does not terminate and endlessly prints out the same text:
Match starts at 0: 11
To obtain the desired behaviour of the while-loop we have to use
the / /g modifier:
/ /g In scalar context, successive invocations against a string will
move from match to match, keeping track of the position in the
string
In list context, returns a list of matched capture groups, or
if there are no capture groups, a list of matches to
the whole regular expression
COMP284 Scripting Languages Lecture 5 Slide L5 – 10
Regular expressions (2) Modifiers
Regular expressions: Modifiers (/ /g and / /c)
With the / /g modifier our code works as desired:
$_ = “11 22 33”;
while (/\d+/g) { print “Match starts at $ -[0]: $&\n” }
Output:
Match starts at 0: 11
Match starts at 3: 22
Match starts at 6: 33
An example in a list context is the following:
$_ = “ab 11 cd 22 ef 33”;
@numbers = (/\d+/g);
print “Numbers: “,join(” | “,@numbers),”\n”;
Output:
Numbers: 11 | 22 | 33
Read / /g as: Start to look for a match from the position where the last
match using / /g ended
COMP284 Scripting Languages Lecture 5 Slide L5 – 11
Regular expressions (2) Modifiers
Regular expressions: Modifiers (/ /g and / /c)
The current position in a string for a regular expression regexpr
is associated with the string, not regexpr
; different regular expressions for the same strings will move forward the
same position when used with / /g
; different strings have different positions and their respective positions
move forward independently
Example:
$_ = “ab 11 cd 22 ef 33”;
if (/\d+/g) { print “Match starts at $-[0]: $&\n” }
if (/[a-z]+/g) { print “Match starts at $-[0]: $&\n” }
if (/\d+/g) { print “Match starts at $-[0]: $&\n” }
Output:
Match starts at 3: 11
Match starts at 6: cd
Match starts at 9: 22
COMP284 Scripting Languages Lecture 5 Slide L5 – 12
Regular expressions (2) Modifiers
Regular expressions: Modifiers (/ /g and / /c)
A failed match or changing the target string resets the position
1 $_ = “ab 11 cd 22 ef 33”;
2 if (/\d+/g) { print “2: Match starts at $-[0]: $&\n” }
3 if (/ab/g) { print “3: Match starts at $-[0]: $&\n” }
4 if (/\d+/g) { print “4: Match starts at $-[0]: $&\n” }
Output:
2: Match starts at 3: 11
4: Match starts at 3: 11
To prevent the reset, an additional modifier / /c can be used
1 $_ = “ab 11 cd 22 ef 33”;
2 if (/\d+/g) { print “2: Match starts at $-[0]: $&\n” }
3 if (/ab/gc) { print “3: Match starts at $-[0]: $&\n” }
4 if (/\d+/g) { print “4: Match starts at $-[0]: $&\n” }
Output:
2: Match starts at 3: 11
4: Match starts at 9: 22
COMP284 Scripting Languages Lecture 5 Slide L5 – 13
Regular expressions (2) Binding operator
Generating regular expressions on-the-fly
The Perl parser will expand occurrences of $variable and @variable
in regular expressions
; regular expessions can be constructed at runtime
Example:
$_ = “Bart teases Lisa”;
@keywords = (“bart”,”lisa”,”marge”,’L\w+’,”t\\w+”);
while ($keyword = shift(@keywords )) {
print “Match found for $keyword: $&\n” if /$keyword/i;
}
Output:
Match found for bart: Bart
Match found for lisa: Lisa
Match found for L\w+: Lisa
Match found for t\w+: teases
COMP284 Scripting Languages Lecture 5 Slide L5 – 14
Regular expressions (2) Binding operator
Binding operator
Perl offers two binding operators for regular expressions
string =∼ /regexpr/ true iff regexpr matches string
string !∼ /regexpr/ true iff regexpr does not match string
• Note that these are similar to comparison operators not assignments
• Most of the time we are not just interested whether these expressions
return true or false, but in the side effect they have on the special
variables $N that store the strings matched by capture groups
Example:
$name = “Dr Ullrich Hustadt”;
if ($name =∼ /(Mr|Ms|Mrs|Dr)?\s*(\w+)/) {print “Hello $2\n”}
$name = “Dave Shield”;
if ($name =∼ /(Mr|Ms|Mrs|Dr)?\s*(\w+)/) {print “Hello $2\n”}
Hello Ullrich
Hello Dave
COMP284 Scripting Languages Lecture 5 Slide L5 – 15
Regular expressions (2) Binding operator
Pattern matching in a list context
• When a pattern match /regexpr/ is used in a list context,
then the return value is
• a list of the strings matched by the capture groups in regexpr
if the match succeeds and regexpr contains capture groups, or
• (a list containing) the value 1
if the match succeeds and regexpr contains no capture groups, or
• an empty list if the match fails
$name = “Dr Ullrich Hustadt”;
($t ,$f ,$l) = ($name =∼ /(Mr|Ms|Mrs|Dr)?\s*(\w+)\s+(\w+)/);
print “Name: $t, $f , $l\n”;
$name = “Dave Shield”;
($t ,$f ,$l) = ($name =∼ /(Mr|Ms|Mrs|Dr)?\s*(\w+)\s+(\w+)/);
print “Name: $t, $f , $l\n”;
Output:
Name: Dr, Ullrich , Hustadt
Name: , Dave , Shield
COMP284 Scripting Languages Lecture 5 Slide L5 – 16
Regular expressions (2) Binding operator
Pattern matching in a list context
• When a pattern match /regexpr/g is used in a list context,
then the return value is
• a list of the strings matched by the capture groups in regexpr
each time regex matches
provided that regexpr contains capture groups, or
• a list containing the string matched by regexpr each time regexpr
matches provided that regexpr contains no capture groups, or
• an empty list if the match fails
$string = “firefox: 10.3 seconds; chrome: 9.5 seconds”;
%performance = ($string =∼ /(\w+)\:\s+(\d+\.\d+)/g);
foreach $system (keys %performance) {
print “$system -> $performance{$system }\n” }
Output:
firefox -> 10.3
chrome -> 9.5
COMP284 Scripting Languages Lecture 5 Slide L5 – 17
Regular expressions (2) Binding operator
Revision
Read
• Chapter 7: In the World of Regular Expressions
• Chapter 8: Matching with Regular Expressions
of
R. L. Schwartz, brian d foy, T. Phoenix:
Learning Perl.
O’Reilly, 2011.
• http://perldoc.perl.org/perlre.html
• http://perldoc.perl.org/perlretut.html
• http://www.perlfect.com/articles/regextutor.shtml
COMP284 Scripting Languages Lecture 5 Slide L5 – 18
http://perldoc.perl.org/perlre.html
http://perldoc.perl.org/perlretut.html
http://www.perlfect.com/articles/regextutor.shtml
COMP284 Scripting Languages
Lecture 6: Perl (Part 5)
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
11 Substitution
Binding operators
Capture variables
Modifiers
12 Subroutines
Introduction
Defining a subroutine
Parameters and Arguments
Calling a subroutine
Persistent variables
Nested subroutine definitions
COMP284 Scripting Languages Lecture 6 Slide L6 – 1
Substitution Binding operators
Substitutions
s/regexpr/replacement/
• Searches a variable for a match for regexpr, and if found,
replaces that match with a string specified by replacement
• In both scalar context and list context returns the number of
substitutions made (that is, 0 if no substitutions occur)
• If no variable is specified via one of the binding operators =∼ or !∼,
the special variable $_ is searched and modified
• The binding operator !∼ only negates the return value but does not
affect the manipulation of the text
The delimiter / can be replaced by some other paired or non-paired
character, for example:
s!regexpr!replacement! or s
COMP284 Scripting Languages Lecture 6 Slide L6 – 2
Substitution Binding operators
Substitutions
Example:
$text = “http ://www.myorg.co.uk/info/refund /../ vat.html”;
$text =∼ s!/[^\/]+/\.\.!!;
print “$text\n”;
Output:
http :// www.myorg.co.uk/info/vat.html
Example:
$_ = “Yabba dabba doo”;
s/bb/dd/;
print $_,”\n”;
Output:
Yadda dabba doo
Note: Only the first match is replaced
COMP284 Scripting Languages Lecture 6 Slide L6 – 3
Substitution Capture variables
Substitutions: Capture variables
s/regexpr/replacement/
• Perl treats replacement like a double-quoted string
; backslash escapes work as in a double-quoted string
\n Newline
\t Tab
\l Lower case next letter
\L Lower case all following letters until \E
\u Upper case next letter
\U Upper case all following letters until \E
; variable interpolation is applied, including capture variables
$N string matched by capture group N
(where N is a natural number)
$+{name} string matched by a named capture group
COMP284 Scripting Languages Lecture 6 Slide L6 – 4
Substitution Capture variables
Substitutions: Capture variables
Example:
$name = “Dr Ullrich Hustadt”;
$name =∼ s/(Mr|Ms|Mrs|Dr)?\s*(\w+)\s+(\w+)/\ U$3\E, $2/;
print “$name\n”;
$name = “Dave Shield”;
$name =∼ s/(Mr|Ms|Mrs|Dr)?\s*(\w+)\s+(\w+)/\ U$3\E, $2/;
print “$name\n”;
Output:
HUSTADT , Ullrich
SHIELD , Dave
COMP284 Scripting Languages Lecture 6 Slide L6 – 5
Substitution Modifiers
Substitutions: Modifiers
Modifiers for substitutions include the following:
s/ / /g Match and replace globally, that is, all occurrences
s/ / /i Case-insensitive pattern matching
s/ / /m Treat string as multiple lines
s/ / /s Treat string as single line
s/ / /e Evaluate the right side as an expression
Combinations of these modifiers are also allowed
Example:
$_ = “Yabba dabba doo”;
s/bb/dd/g;
print $_,”\n”;
Output:
Yadda dadda doo
COMP284 Scripting Languages Lecture 6 Slide L6 – 6
Substitution Modifiers
Substitutions: Modifiers
Modifiers for substitutions include the following:
s/ / /e Evaluate the right side as an expression
Example:
1 $text = “The temperature is 105 degrees Fahrenheit”;
2 $text =∼ s!(\d+) degrees Fahrenheit!
3 (($1 -32)*5/9).” degrees Celsius”!e;
4 print “$text\n”;
5 $text =∼ s!(\d+\.\d+)! sprintf(“%d”,$1 +0.5)!e;
6 print “$text\n”;
The temperature is 40.5555555555556 degrees Celsius
The temperature is 41 degrees Celsius
Better:
1 $text = “The temperature is 105 degrees Fahrenheit”;
2 $text =∼ s!(\d+) degrees Fahrenheit!
3 sprintf(“%d” ,(($1 -32)*5/9)+0.5).
4 ” degrees Celsius”!e;
COMP284 Scripting Languages Lecture 6 Slide L6 – 7
Substitution Modifiers
Regular Expressions and the Chomsky Hierarchy
• In Computer Science, formal
languages are categorised
according to the type of grammar
needed to generate them (or the
type of automaton needed to
recognise them)
• Perl regular expressions can
at least recognise all context-free
languages Chomsky Hiearchy of Formal Languages
• Howerver, this does not mean regular expression should be used for
parsing context-free languages
• Instead there are packages specifically for parsing context-free languages
or dealing with specific languages, e.g. HTML, CSV
COMP284 Scripting Languages Lecture 6 Slide L6 – 8
Subroutines Introduction
Java methods versus Perl subroutines
• Java uses methods as a means to encapsulate sequences of instructions
• In Java you are expected
• to declare the type of the return value of a method
• to provide a list of parameters, each with a distinct name,
and to declare the type of each parameter
public static int sum2( int f, int s) {
f = f+s;
return f;
}
public static void main(String [] args) {
System.out.println(“Sum of 3 and 4 is ” + sum2(3, 4));
}
• Instead of methods, Perl uses subroutines
COMP284 Scripting Languages Lecture 6 Slide L6 – 9
Subroutines Defining a subroutine
Subroutines
Subroutines are defined as follows in Perl:
sub identifier {
statements
}
• Subroutines can be placed anywhere in a Perl script but preferably they
should all be placed at start of the script (or at the end of the script)
• All subroutines have a return value (but no declaration of its type)
• The statement
return value
can be used to terminate the execution of a subroutine and
to make value the return value of the subroutine
• If the execution of a subroutine terminates without encountering
a return statement, then the value of the last evaluation of an expression
in the subroutine is returned
The return value does not have to be scalar value, but can be a list
COMP284 Scripting Languages Lecture 6 Slide L6 – 10
Subroutines Parameters and Arguments
Parameters and Arguments
Subroutines are defined as follows in Perl:
sub identifier {
statements
}
• In Perl there is no need to declare the parameters of a subroutine
(or their types)
; there is no pre-defined fixed number of parameters
• Arguments are passed to a subroutine via a special array @_
• Individual arguments are accessed using $_[0], $_[1] etc
• Is is up to the subroutine to process arguments as is appropriate
• The array @_ is private to the subroutine
; each nested subroutine call gets its own @_ array
COMP284 Scripting Languages Lecture 6 Slide L6 – 11
Subroutines Parameters and Arguments
Parameters and Arguments: Examples
• The Java method
public static int sum2( int f, int s) {
f = f+s;
return f;
}
could be defined as follows in Perl:
sub sum2 {
return $_[0] + $_[1];
}
• A more general solution, taking into account that a subroutine can be
given arbitrarily many arguments, is the following:
1 sub sum {
2 return undef if (@_ < 1);
3 $sum = shift(@_);
4 foreach (@_) { $sum += $_ }
5 return $sum;
6 }
COMP284 Scripting Languages Lecture 6 Slide L6 – 12
Subroutines Parameters and Arguments
Private variables
sub sum {
return undef if (@_ < 1);
$sum = shift(@_);
foreach (@_) { $sum += $_ }
return $sum;
}
The variable $sum in the example above is global:
$sum = 5;
print "Value of \$sum before call of sum: ",$sum ,"\n";
print "Return value of sum: ",&sum(5,4,3,2,1),"\n";
print "Value of \$sum after call of sum: ",$sum ,"\n";
produces the output
Value of $sum before call of sum: 5
Return value of sum: 15
Value of $sum after call of sum: 15
This use of global variables in subroutines is often undesirable
; we want $sum to be private/local to the subroutine
COMP284 Scripting Languages Lecture 6 Slide L6 – 13
Subroutines Parameters and Arguments
Private variables
• The operator my declares a variable or list of variables to be private:
my $variable;
my ($variable1 ,$variable2 );
my @array;
• Such a declaration can be combined with a (list) assignment:
my $variable = $_[0];
my ($variable1 ,$variable2) = @_;
my @array = @_;
• Each call of a subroutine will get its own copy of its private variables
Example:
sub sum {
return undef if (@_ < 1);
my $sum = shift(@_);
foreach (@_) { $sum += $_ }
return $sum;
}
COMP284 Scripting Languages Lecture 6 Slide L6 – 14
Subroutines Calling a subroutine
Calling a subroutine
A subroutine is called by using the subroutine name with an ampersand &
in front possibly followed by a list of arguments
The ampersand is optional if a list of arguments is present
sub identifier {
statements
}
... &identifier ...
... &identifier(arguments) ...
... identifier(arguments) ...
Examples:
print "sum0: " ,&sum ,"\n";
print "sum0: ",sum(),"\n";
print "sum1: ",sum(5),"\n";
print "sum2: ",sum(5,4),"\n";
print "sum5: " ,&sum(5,4,3,2,1),"\n";
$total = sum(9,8,7,6)+sum(5,4,3,2,1);
sum(1,2,3,4);
COMP284 Scripting Languages Lecture 6 Slide L6 – 15
Subroutines Persistent variables
Persistent variables
• Private variables within a subroutine are forgotten once a call of the
subroutine is completed
• In Perl 5.10 and later versions, we can make a variable
both private and persistent using the state operator
; the value of a persistent variable will be retained between
independent calls of a subroutine
Example:
use 5.010;
sub running_sum {
state $sum;
foreach (@_) { $sum += $_ }
return $sum;
}
COMP284 Scripting Languages Lecture 6 Slide L6 – 16
Subroutines Persistent variables
Persistent variables
Example:
1 use 5.010;
2
3 sub running_sum {
4 state $sum;
5 foreach (@_) { $sum += $_ }
6 return $sum;
7 }
8
9 print "running_sum ():\t\t", running_sum (), "\n";
10 print "running_sum (5):\t", running_sum (5), "\n";
11 print "running_sum (5 ,4):\t", running_sum (5,4), "\n";
12 print "running_sum (3,2 ,1):\t",running_sum (3,2,1),"\n";
Output:
running_sum ():
running_sum (5): 5
running_sum (5 ,4): 14
running_sum (3,2,1): 20
COMP284 Scripting Languages Lecture 6 Slide L6 – 17
Subroutines Nested subroutine definitions
Nested subroutine definitions
• Perl allows nested subroutine definitions (unlike C or Java)
sub outer_sub {
sub inner_sub { ... }
}
• Normally, nested subroutines are a means for information hiding
; the inner subroutine should only be visible and executable from
inside the outer subroutine
• However, Perl allows inner subroutines to be called from anywhere
(within the package in which they are defined)
sub outer_sub {
sub inner_sub { ... }
}
inner_sub ();
COMP284 Scripting Languages Lecture 6 Slide L6 – 18
Subroutines Nested subroutine definitions
Nested subroutine definitions
If an inner subroutine uses a local variable of an outer subroutine,
then it refers to the instance of that local variable created the first time
the outer subroutine was called
sub outer {
my $x = $_[0];
sub inner { return $x }
return inner (); # returns $_[0]?
}
print "1: ",outer (10),"\n";
print "2: ",outer (20),"\n";
1: 10
2: 10 # not 20!
; Do not refer to local variables of an outer subroutine,
pass information via arguments instead
COMP284 Scripting Languages Lecture 6 Slide L6 – 19
Subroutines Nested subroutine definitions
Nested subroutine definitions: Example
sub sqrt2 {
my $x = shift(@_);
my $precision = 0.001;
sub sqrtIter {
my ($guess ,$x) = @_;
if (isGoodEnough($guess ,$x)) {
return int($guess/$precision +0.5)* $precision;
} else { sqrtIter(improveGuess($guess , $x), $x) } }
sub improveGuess {
my ($guess ,$x) = @_;
return ($guess + $x / $guess) / 2; }
sub isGoodEnough {
my ($guess ,$x) = @_;
return (abs($guess * $guess - $x) < $precision ); }
return sqrtIter (1.0,$x);
}
COMP284 Scripting Languages Lecture 6 Slide L6 – 20
Subroutines Nested subroutine definitions
Revision
Read
• Chapter 9: Processing Text with Regular Expressions
• Chapter 4: Subroutines
of
R. L. Schwartz, brian d foy, T. Phoenix:
Learning Perl.
O’Reilly, 2011.
• http://perldoc.perl.org/perlsub.html
COMP284 Scripting Languages Lecture 6 Slide L6 – 21
http://perldoc.perl.org/perlsub.html
COMP284 Scripting Languages
Lecture 7: Perl (Part 6)
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
13 Input/Output
Filehandles
Open
Close
Read
Select
Print
Here documents
14 Arguments and Options
Invocation Arguments
Options
COMP284 Scripting Languages Lecture 7 Slide L7 – 1
Input/Output Filehandles
I/O Connections
• Perl programs interact with their environment via I/O connections
• A filehandle is the name in a Perl program for such an I/O connection,
given by a Perl identifier
Beware: Despite the terminology, no files might be involved
• There are six pre-defined filehandles
STDIN Standard Input, for user input, typically the keyboard
STDOUT Standard Output, for user output, typically the terminal
STDERR Standard Error, for error output,
typically defaults to the terminal
DATA Input from data stored after __END__ at the end of a
Perl program
ARGV Iterates over command-line filenames in @ARGV
ARGVOUT Points to the currently open output file when doing edit-
in-place processing with -i
perl -pi -e ’s/cat/dog/’ file
COMP284 Scripting Languages Lecture 7 Slide L7 – 2
Input/Output Filehandles
I/O Connections
Except for the six predefined I/O connections, all other I/O connections
• need to be opened before they can be used
open filehandle, mode, expr
• should be closed once no longer needed
close filehandle
• can be used to read from
• can be used to write to
print filehandle list
printf filehandle list
• can be selected as default output
select filehandle
COMP284 Scripting Languages Lecture 7 Slide L7 – 3
Input/Output Filehandles
I/O Connections
Example:
open INPUT , “<", "oldtext.txt" or die "Cannot open file"; open OUTPUT , ">“, “newtext.txt”;
while () {
s!(\d+) degrees Fahrenheit!
sprintf(“%d” ,(($1 -32)*5/9)+0.5).” degrees Celsius”!e;
print OUTPUT;
}
close(INPUT );
close(OUTPUT );
oldtext.txt:
105 degrees Fahrenheit is quite warm
newtext.txt:
41 degrees Celcius is quite warm
COMP284 Scripting Languages Lecture 7 Slide L7 – 4
Input/Output Open
Opening a filehandle
open filehandle, expr
open filehandle, mode, expr
• Opens an I/O connection specified by mode and expr and associates it
with filehandle
• expr specifies a file or command
• mode is one of the following
Mode Operation Create Truncate
< read file
> write file yes yes
>> append file yes
+< read/write file
+> read/write file yes yes
+>> read/append file yes
|- write to command yes
-! read from command yes
COMP284 Scripting Languages Lecture 7 Slide L7 – 5
Input/Output Close
Closing a filehandle
close
close filehandle
• Flushes the I/O buffer and closes the I/O connection associated with
filehandle
• Returns true if those operations succeed
• Closes the currently selected filehandle if the argument is omitted
COMP284 Scripting Languages Lecture 7 Slide L7 – 6
Input/Output Read
Reading
• In a scalar context, returns a string consisting of all characters from
filehandle up to the next occurrence of $/
(the input record separator)
• In a list context, returns a list of strings representing the whole content
of filehandle separated into string using $/ as a separator
(Default value of $/: newline \n)
1 open INPUT , “<", "oldtext.txt" or die "Cannot open file";
2 $first_line = ;
3 while ($other_line = ) { … }
4 close INPUT;
5
6 open LS, ” -|”, “ls -1”;
7 @files =
8 close LS;
9 foreach $file (@files) { … }
COMP284 Scripting Languages Lecture 7 Slide L7 – 7
Input/Output Select
Selecting a filehandle as default output
select
select filehandle
• If filehandle is supplied, sets the new current default filehandle for
output
; write or print without a filehandle default to filehandle
; References to variables related to output will refer to filehandle
• Returns the currently selected filehandle
COMP284 Scripting Languages Lecture 7 Slide L7 – 8
Input/Output Print
Printing
print filehandle list
print filehandle
print list
• Print a string or a list of strings to filehandle
• If filehandle is omitted, prints to the last selected filehandle
• If list is omitted, prints $_
• The current value of $, (if any) is printed between each list item
(Default: undef)
• The current value of $\ (if any) is printed after the entire list has
been printed
(Default: undef)
COMP284 Scripting Languages Lecture 7 Slide L7 – 9
Input/Output Print
Printing: Formatting
sprintf(format, list)
• Returns a string formatted by the usual printf conventions of the C
library function sprintf (but does not by itself print anything)
sprintf “(%10.3f)” 1234.5678
format a floating-point number with minimum width 10 and precision 3
and put the result in parentheses:
( 1234.568)
See http://perldoc.perl.org/functions/sprintf.html for
further details
COMP284 Scripting Languages Lecture 7 Slide L7 – 10
http://perldoc.perl.org/functions/sprintf.html
Input/Output Print
Printing: Formatting
printf filehandle format, list
printf format, list
• Equivalent to
print filehandle sprintf(format, list)
except that $\ (the output record separator) is not appended
COMP284 Scripting Languages Lecture 7 Slide L7 – 11
Input/Output Print
Printing: Formatting
Format strings can be stored in variables and can be constructed
on-the-fly:
@list = qw(wilma dino pebbles );
$format = “The items are:\n”. (“%10s\n” x @list );
printf $format , @list;
Output:
The items are:
wilma
dino
pebbles
(The code above uses the ‘quote word’ function qw()
to generate a list of words.
See http://perlmeme.org/howtos/perlfunc/qw_function.html
for details)
COMP284 Scripting Languages Lecture 7 Slide L7 – 12
http://perlmeme.org/howtos/perlfunc/qw_function.html
Input/Output Here documents
Here documents
• A here document is a way of specifying multi-line strings in a scripting
or programming language
• The basic syntax is
<
$title
Lots of HTML markup here
END
The double-quotes in “END”
indicate that everything be-
tween the opening “END” and
the closing END should be
treated like a double-quoted
string
Content -type: text/html
My HTML document
Lots of HTML markup here
COMP284 Scripting Languages Lecture 7 Slide L7 – 14
Input/Output Here documents
Here documents: Single-quotes
$title = “My HTML document”
print <<’END’; Content -type: text/html
END
The single-quotes in ’END’ indicate that everything between ’END’ and
END should be treated like a single-quoted string
; no variable interpolation is applied
; $title will not be expanded
Content -type: text/html
END
COMP284 Scripting Languages Lecture 7 Slide L7 – 15
Input/Output Here documents
Here documents: Backticks
$command = “ls”;
print <<‘END ‘; $command -1 END The backticks in ‘END‘ tell Perl to run the here document as a shell script (with the here document treated like a double-quoted string) handouts.aux handouts.log handouts.pdf handouts.tex COMP284 Scripting Languages Lecture 7 Slide L7 – 16 Input/Output Here documents Here documents: Variables Here documents can be assigned to variables and manipulated using string operations $header = <<"HEADER"; Content -type: text/html
HEADER
$body = <<"BODY";
$title
Lots of HTML markup here
BODY
$html = $header.$body;
print $html;
COMP284 Scripting Languages Lecture 7 Slide L7 – 17
Arguments and Options Invocation Arguments
Invocation Arguments
• Another way to provide input to a Perl program are
invocation arguments (command-line arguments)
./ perl_program arg1 arg2 arg3
• The invocation arguments given to a Perl program are stored in the
special array @ARGV
perl_program1:
print “Number of arguments: ” ,$#ARGV+1,”\n”;
for ($index =0; $index <= $#ARGV; $index ++) { print "Argument $index: $ARGV[$index],"\n"; } ./ perl_program1 ada ’bob’ 2 Output: Number of arguments: 3 Argument 0: ada Argument 1: bob Argument 2: 2 COMP284 Scripting Languages Lecture 7 Slide L7 – 18 Arguments and Options Options Options • There are various Perl modules that make it easier to process command-line options –scale=5 –debug –file=’image.png’ • One such module is Getopt::Long: http://perldoc.perl.org/Getopt/Long.html • The module provides the GetOptions function • GetOptions parses the command line arguments that are present in @ARGV according to an option specification • Arguments that do not fit to the option specification remain in @ARGV • GetOptions returns true if @ARGV can be processed successfully COMP284 Scripting Languages Lecture 7 Slide L7 – 19 http://perldoc.perl.org/Getopt/Long.html Arguments and Options Options Options: Example perl_program2: use Getopt ::Long; my $file = "photo.jpg"; my $scale = 2; my $debug = 0; $result = GetOptions ("debug" => \$debug , # flag
“scale=i” => \$scale , # numeric
“file=s” => \$file); # string
print “Debug: $debug; Scale: $scale; File: $file\n”;
print “Number of arguments: ” ,$#ARGV+1,”\n”;
print “Arguments: “,join(“,”,@ARGV), “\n”;
./ perl_program2 –scale =5 –file=’image.png’ arg1 arg2
Debug: 0; Scale: 5; File: image.png
Number of arguments: 2
Arguments: arg1 , arg2
COMP284 Scripting Languages Lecture 7 Slide L7 – 20
Arguments and Options Options
Revision
Read
• Chapter 5: Input and Output
of
R. L. Schwartz, brian d foy, T. Phoenix:
Learning Perl.
O’Reilly, 2011.
• http://perldoc.perl.org/perlop.html#I%2fO-Operators
• http://perldoc.perl.org/perlop.html#Quote-Like-Operators
• http://perldoc.perl.org/Getopt/Long.html
COMP284 Scripting Languages Lecture 7 Slide L7 – 21
http://perldoc.perl.org/perlop.html#I%2fO-Operators
http://perldoc.perl.org/perlop.html#Quote-Like-Operators
http://perldoc.perl.org/Getopt/Long.html
COMP284 Scripting Languages
Lecture 8: Perl (Part 7)
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
15 CGI
Overview
CGI I/O
16 The Perl module CGI.pm
Motivation
HTML shortcuts
Forms
COMP284 Scripting Languages Lecture 8 Slide L8 – 1
CGI Overview
Common Gateway Interface — CGI
The Common Gateway Interface (CGI) is a standard method
for web servers to use an external application, a CGI program,
to dynamically generate web pages
1 A web client generates a client request,
for example, from a HTML form, and sends it to a web server
2 The web server selects a CGI program to handle the request,
converts the client request to a CGI request, executes the program
3 The CGI program then processes the CGI request and
the server passes the program’s response back to the client
COMP284 Scripting Languages Lecture 8 Slide L8 – 2
CGI CGI I/O
Client requests
In the following we focus on client requests that are generated
using HTML forms
COMP284 Scripting Languages Lecture 8 Slide L8 – 3
CGI CGI I/O
Client requests
In the following we focus on client requests that are generated
using HTML forms
COMP284 Scripting Languages Lecture 8 Slide L8 – 4
CGI CGI I/O
Encoding of input data
• Input data from an HTML form is sent URL-encoded as sequence of
key-value pairs: key1=value1&key2=value2&…
Example:
username=dave&fullname=David%20Davidson
• All characters except A-Z, a-z, 0-9, -, _, ., ∼ (unreserved characters)
are encoded
• ASCII characters that are not unreserved characters are represented
using ASCII codes (preceded by %)
• A space is represented as %20 or +
• + is represented as %2B
• % is represented as %25
Examples:
username=cath&fullname=Catherine+O%27Donnell
COMP284 Scripting Languages Lecture 8 Slide L8 – 5
CGI CGI I/O
Request methods: GET versus POST
The two main request methods used with HTML forms
are GET and POST:
• GET:
• Form data is appended to the URI in the request
• Form data is accessed by the CGI program via environment variables
Example:
GET /cgi -bin/cgiwrap/ullrich/demo?username=dave&
fullname=David+Davidson HTTP /1.1
Host: cgi.csc.liv.ac.uk
COMP284 Scripting Languages Lecture 8 Slide L8 – 6
CGI CGI I/O
Request methods: GET versus POST
The two main request methods used with HTML forms are GET and POST:
• POST:
• Form data is appended to end of the request (after headers and blank line) • Form data can be accessed by the CGI program via standard input • Form data is not necessarily URL-encoded (but URL-encoding is the default)
Example:
POST /cgi -bin/cgiwrap/ullrich/demo HTTP /1.1
Host: cgi.csc.liv.ac.uk
username=dave&fullname=David+Davidson
COMP284 Scripting Languages Lecture 8 Slide L8 – 7
CGI CGI I/O
Environment variables: GET
Env variable Meaning
QUERY_STRING The query information passed to the program
REQUEST_METHOD The request method that was used
PATH_INFO Extra path information passed to a CGI program
PATH_TRANSLATED Translation of PATH_INFO from virtual to physical path
SCRIPT_NAME The relative virtual path of the CGI program
SCRIPT_FILENAME The physical path of the CGI program
Example (1): GET http ://cgi.csc.liv.ac.uk/cgi -bin/cgiwrap/ullrich/demo/more/dirs?
username=dave&fullname=David+Davidson
QUERY_STRING username=dave&fullname=David+Davidson
REQUEST_METHOD GET
PATH_INFO /more/dirs
PATH_TRANSLATED /users/www/external/docs/more/dirs
SCRIPT_NAME /cgi -bin/cgiwrap/ullrich/demo
SCRIPT_FILENAME /users/loco/ullrich/public_html/cgi -bin/demo
STDIN
# empty
COMP284 Scripting Languages Lecture 8 Slide L8 – 8
CGI CGI I/O
Environment variables: GET
Env variable Meaning
QUERY_STRING The query information passed to the program
REQUEST_METHOD The request method that was used
PATH_INFO Extra path information passed to a CGI program
PATH_TRANSLATED Translation of PATH_INFO from virtual to physical path
SCRIPT_NAME The relative virtual path of the CGI program
SCRIPT_FILENAME The physical path of the CGI program
Example (2): GET http ://cgi.csc.liv.ac.uk/cgi -bin/cgiwrap/ullrich/demo/more/dirs?
username =2%60n+d%2Bt+e+s%27t&fullname=Peter+Newton
QUERY_STRING username =2%60n+d%2Bt+e+s%27t&fullname=Peter+Newton
REQUEST_METHOD GET
PATH_INFO /more/dirs
PATH_TRANSLATED /users/www/external/docs/more/dirs
SCRIPT_NAME /cgi -bin/cgiwrap/ullrich/demo
SCRIPT_FILENAME /users/loco/ullrich/public_html/cgi -bin/demo
STDIN
# empty
COMP284 Scripting Languages Lecture 8 Slide L8 – 9
CGI CGI I/O
Environment variables: POST
Env variable Meaning
QUERY_STRING The query information passed to the program
REQUEST_METHOD The request method that was used
SCRIPT_NAME The relative virtual path of the CGI program
SCRIPT_FILENAME The physical path of the CGI program
Example: POST /cgi -bin/cgiwrap/ullrich/demo
Host: cgi.csc.liv.ac.uk
username =2%60n+d%2Bt+e+s%27t&fullname=Peter+Newton
QUERY_STRING
# empty
REQUEST_METHOD POST
SCRIPT_NAME /cgi -bin/cgiwrap/ullrich/demo
SCRIPT_FILENAME /users/loco/ullrich/public_html/cgi -bin/demo
STDIN username =2%60n+d%2Bt+e+s%27t&fullname=Peter+Newton
COMP284 Scripting Languages Lecture 8 Slide L8 – 10
CGI CGI I/O
More environment variables
Env variable Meaning
HTTP_ACCEPT A list of the MIME types that the client can accept
HTTP_REFERER The URL of the document that the client points to before accessing the CGI program
HTTP_USER_AGENT The browser the client is using to issue the request
REMOTE_ADDR The remote IP address of the user making the request
REMOTE_HOST The remote hostname of the user making the re- quest
SERVER_NAME The server’s hostname
SERVER_PORT The port number of the host on which the server is running
SERVER_SOFTWARE The name and version of the server software
COMP284 Scripting Languages Lecture 8 Slide L8 – 11
The Perl module CGI.pm Motivation
CGI programs and Perl
• CGI programs need to process input data from environment variables and STDIN, depending on the request method ; preferably, the input data would be accessible by the program
in a uniform way
• CGI programs need to process input data that is encoded ; preferably, the input data would be available in decoded form
• CGI programs need to produce HTML markup/documents as output ; preferably, there would be an easy way to produce HTML markup
In Perl all this can be achieved with the use of the CGI.pm module http://perldoc.perl.org/CGI.html
COMP284 Scripting Languages Lecture 8 Slide L8 – 12
http://perldoc.perl.org/CGI.html
The Perl module CGI.pm HTML shortcuts
CGI.pm HTML shortcuts
• CGI.pm provides so-called HTML shortcuts that create HTML tags
a address applet b body br center code
dd div dl dt em font form
h1 h2 h3 h4 h5 h6 head header
html hr img li ol p pre strong
sup table td th tr title tt ul
• HTML tags have attributes and contents
This is a paragraph
• HTML shortcuts are given • HTML attributes in the form of a hash reference as the first argument • the contents as any subsequent arguments
p({-align=>right},”This is a paragraph”)
COMP284 Scripting Languages Lecture 8 Slide L8 – 13
The Perl module CGI.pm HTML shortcuts
CGI.pm HTML shortcuts: Examples
Code: print p();
Output:
Code: print p(’’);
Output:
Code: print p({-align=>right},”Hello world!”);
Output:
Hello world!
Code: print p({-class=>right_para ,-id=>p1},”Text”);
Output:
Text
COMP284 Scripting Languages Lecture 8 Slide L8 – 14
The Perl module CGI.pm HTML shortcuts
CGI.pm HTML shortcuts: Nesting vs Start/End
• Nested HTML tags using nested HTML shortcuts
Code: print p(em(“Emphasised”).” Text”), “\n”;
Output:
Emphasised Text
• Nested HTML tags using start_tag and end_tag:
use CGI qw(-utf8 :all *em *p);
print start_p(), start_em(), “Emphasised”, end_em(),
” Text”, end_p(), “\n”;
Output:
Emphasised Text
The following start_tag/end_tag HTML shortcuts are generated automatically by CGI.pm:
start_html (), start_form (), start_multipart_form ()
end_html(), end_form () end_multipart_form ()
All others need to be requested by adding *tag to the CGI.pm import list COMP284 Scripting Languages Lecture 8 Slide L8 – 15
The Perl module CGI.pm Forms
CGI.pm Forms
• HTML forms are created using start_form and end_form print start_form ({-method=>request_method ,
-action=>uri});
form_elements
print end_form;
• HTML form elements are again created using HTML shortcuts
textfield textarea password_field
filefield hidden scrolling_list
popup_menu optgroup
image_button checkbox checkbox_group
radio_group reset submit
• optgroup creates an option group within a popup menu ; optgroup occurs nested inside popup_menu
• All other HTML shortcuts for HTML form elements will occur independently of each other within a form
COMP284 Scripting Languages Lecture 8 Slide L8 – 16
The Perl module CGI.pm Forms
CGI.pm Forms: Examples
print textfield ({-name=>’username ’,
-value=>’dave’,
-size=>100,
-maxlength = >500});
• -name specifies the name of the text field and is the only required argument of textfield
• -value specifies a default value that will be shown in the text field • -size is the size of the text field in characters • -maxlength is the maximum number of characters that the text field
will accept
Output:
COMP284 Scripting Languages Lecture 8 Slide L8 – 17
The Perl module CGI.pm Forms
CGI.pm Forms: Examples
print submit({-name=>’submit ’,
-label=>’Click for response ’});
• -name is an optional argument that allows to distinguish submit buttons from each other
• -label or -value is an optional argument that determines the label shown to the user and the value passed to the CGI program
Output:
COMP284 Scripting Languages Lecture 8 Slide L8 – 18
The Perl module CGI.pm Forms
CGI.pm Forms: Example
#!/usr/bin/perl
use CGI qw(-utf8 :all);
print header(-charset=>’utf -8’),
start_html ({-title=>’My HTML Form’,
-author=>’u.hustadt@liverpool.ac.uk’,
-style=>’style.css’});
print start_form ({-method=>”GET”,
-action=>”http :// cgi.csc.liv.ac.uk/”.
“cgi -bin/cgiwrap/ullrich/demo”});
print textfield ({-name=>’username ’,
-value=>’dave’,
-size = >100});
print br();
print textfield ({-name=>’fullname ’,
-value=>’Please enter your name’,
-size = >100});
print br();
print submit({-name=>’submit ’,
-value=>’Click for response ’});
print end_form , end_html;
COMP284 Scripting Languages Lecture 8 Slide L8 – 19
The Perl module CGI.pm Forms
Making it work
For CGI programs to work on our systems you must proceed as follows:
1 Your home directory must be ‘world executable’
2 You must have a directory $HOME/public_html/cgi-bin/
Your public_html and cgi-bin directory must be both readable and executable by everyone
3 Your CGI script must be placed in $HOME/public_html/cgi-bin/
and must be executable by everyone
4 The CGI script can then be accessed using the URL
http://cgi.csc.liv.ac.uk/cgi-bin/cgiwrap/
or http://cgi.csc.liv.ac.uk/cgi-bin/cgiwrapd/
where
for username, would redirect the browser to malware_site. • Check whether the data has the format expected
if (param(’username ’) !∼ /^[a-zA -Z0 -9]+$/s) {
print "Not a valid user name"
} else {
print "The value of username is ",param(’username ’),"\n";
}
or sanitise the input using the CGI.pm routine escapeHTML:
print "The value of username is ",
escapeHTML(param(’username ’)),"\n";
or even better, do both
COMP284 Scripting Languages Lecture 8 Slide L8 – 25
The Perl module CGI.pm Forms
CGI.pm Scripts: Example (Part 1)
use CGI qw(-utf -8 :all *table);
binmode(STDOUT , ":encoding(utf -8)");
print header(-charset=>’utf -8’), "\n",
start_html ({-title=>’Form Processing ’,
-author=>’u.hustadt@liverpool.ac.uk’});
if (! defined(param(’username ’))) {
# This branch is executed if the user first visits this page/script
print start_form ({-method=>"POST"});
print textfield ({-name=>’username ’, -value=>’dave’,
-size =>100}), "\n";
print br(), "\n";
print textfield ({-name=>’fullname ’,
-value=>’Please enter your name’,
-size =>100}), "\n";
print br(), "\n";
print submit({-name=>’submit ’,
-value=>’Click for response ’}), "\n";
print end_form;
} else {
# This branch is executed if the client request is generated
# by the form
COMP284 Scripting Languages Lecture 8 Slide L8 – 26
The Perl module CGI.pm Forms
CGI.pm Scripts: Example (Part 2)
# (We are in the else -branch now)
print start_table ({-border = >1});
print caption("Inputs");
foreach $key (param ()) {
print Tr(td(’PARAM ’),td($key),td(escapeHTML(param($key ))));
}
foreach $key (keys %ENV) {
print Tr(td(’ENV’),td($key),td(escapeHTML($ENV{$key })));
}
print end_table;
}
print end_html;
COMP284 Scripting Languages Lecture 8 Slide L8 – 27
The Perl module CGI.pm Forms
CGI.pm Scripts: Example (Part 3)
Page produced on the first visit
Page produced on submission of the form
COMP284 Scripting Languages Lecture 8 Slide L8 – 28
The Perl module CGI.pm Forms
Revision
Read
• Chapter 11: Perl Modules
of
R. L. Schwartz, brian d foy, T. Phoenix: Learning Perl. O’Reilly, 2011.
• http://perldoc.perl.org/CGI.html
COMP284 Scripting Languages Lecture 8 Slide L8 – 29
http://perldoc.perl.org/CGI.html
COMP284 Scripting Languages Lecture 9: PHP (Part 1)
Handouts
Ullrich Hustadt
Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
17 PHP Motivation
18 Overview Features Applications
19 Types and Variables Types Variables Type juggling and Type casting Comparisons
COMP284 Scripting Languages Lecture 9 Slide L9 – 1
PHP Motivation
Common Gateway Interface — CGI
The Common Gateway Interface (CGI) is a standard method for web servers to use external applications, a CGI program, to dynamically generate web pages
1 A web client generates a client request, for example, from a HTML form, and sends it to a web server
2 The web server selects a CGI program to handle the request, converts the client request to a CGI request, executes the program
3 The CGI program then processes the CGI request and the server passes the program’s response back to the client
COMP284 Scripting Languages Lecture 9 Slide L9 – 2
PHP Motivation
Disadvantages of CGI/Perl
• A distinction is made between static web pages and dynamic web pages created by an external program
• Using Perl scripting it is difficult to add ‘a little bit’ of dynamic content to a web page – can be alleviated to some extent by using here documents
• Use of an external program requires • starting a separate process every time an external program is requested • exchanging data between web server and external program
; resource-intensive
If our main interest is the creation of dynamic web pages, then the scripting language we use
• should integrate well with HTML • should not require a web server to execute an external program
COMP284 Scripting Languages Lecture 9 Slide L9 – 3
Overview Features
PHP
• PHP is (now) a recursive acronym for PHP: Hypertext Preprocessor • Development started in 1994 by Rasmus Lerdorf • Originally designed as a tool for tracking visitors at Lerdorf’s website • Developed into full-featured, scripting language for
server-side web programming
• Inherits a lot of the syntax and features from Perl • Easy-to-use interface to databases • Free, open-source • Probably the most widely used server-side web programming language • Negatives: Inconsistent, muddled API; no scalar objects
The departmental web server uses PHP 5.6.25 (released August 2014) PHP 7 was released in December 2015 (PHP 6 was never released)
COMP284 Scripting Languages Lecture 9 Slide L9 – 4
Overview Features
PHP processing
• Server plug-ins exist for various web servers ; avoids the need to execute an external program
• PHP code is embedded into HTML pages using tags ; static web pages can easily be turned into dynamic ones
PHP satisfies the criteria we had for a good web scripting language
Processing proceeds as follows:
1 The web server receives a client request
2 The web server recognizes that the client request is for a HTML page containing PHP code
3 The server executes the PHP code, substitutes output into the HTML page, the resulting page is then send to the client
As in the case of Perl, the client never sees the PHP code, only the HTML web page that is produced
COMP284 Scripting Languages Lecture 9 Slide L9 – 5
Overview Applications
PHP: Applications
• Applications written using PHP • activeCollab – Project Collaboration Software http://www.activecollab.com/
• Drupal – Content Management System (CMS) http://drupal.org/home
• Magento – eCommerce platform http://www.magentocommerce.com/
• MediaWiki – Wiki software http://www.mediawiki.org/wiki/MediaWiki
• Moodle – Virtual Learning Environment (VLE) http://moodle.org/
• Sugar – Customer Relationship Management (CRM) platform http://www.sugarcrm.com/crm/
• WordPress – Blogging tool and CMS http://wordpress.org/
COMP284 Scripting Languages Lecture 9 Slide L9 – 6
http://www.activecollab.com/ http://drupal.org/home http://www.magentocommerce.com/ http://www.mediawiki.org/wiki/MediaWiki http://moodle.org/ http://www.sugarcrm.com/crm/ http://wordpress.org/
Overview Applications
PHP: Websites
• Websites using PHP: • Delicious – social bookmarking http://delicious.com/
• Digg – social news website http://digg.com
• Facebook – social networking http://www.facebook.com
• Flickr – photo sharing http://www.flickr.com
• Frienster – social gaming http://www.frienster.com
• SourceForge – web-based source code repository http://sourceforge.net/
• Wikipedia – collaboratively built encyclopedia http://www.wikipedia.org
COMP284 Scripting Languages Lecture 9 Slide L9 – 7
http://delicious.com/ http://digg.com http://www.facebook.com http://www.flickr.com http://www.frienster.com http://sourceforge.net/ http://www.wikipedia.org
Overview Applications
Recommended texts
• R. Nixon: Learning PHP, MySQL, and JavaScript. O’Reilly, 2009.
Harold Cohen Library: 518.561.N73 or e-book (or later editions of this book)
• M. Achour, F. Betz, A. Dovgal, N. Lopes, H. Magnusson, G. Richter, D. Seguy, J. Vrana, et al.: PHP Manual. PHP Documentation Group, 2018.
http://www.php.net/manual/en/index.php
COMP284 Scripting Languages Lecture 9 Slide L9 – 8
http://www.php.net/manual/en/index.php
Overview Applications
PHP: Hello World!
1 2
Our first PHP script
5 Hello World!
\n"); 7 ?> 8
• PHP code is enclosed between • File must be stored in a directory accessible by the web server, for
example $HOME/public_html, and be readable by the web server
• File name must have the extension .php, e.g. hello_world.php
COMP284 Scripting Languages Lecture 9 Slide L9 – 9
Overview Applications
PHP: Hello World!
Since version 4.3.0, PHP also has a command line interface
1 #!/usr/bin/php 2
• PHP code still needs to be enclosed between • Code must be stored in an executable file • File name does not need to have any particular format
; PHP can be used as scripting language outside a web programming context
Output:
Hello World!
COMP284 Scripting Languages Lecture 9 Slide L9 – 10
Overview Applications
PHP: Hello World!
Our first PHP script
Hello World!
\n");
?>
• Can also ‘executed’ using php filename
• File does not need to exectuable, only readable for the user
Output:
Our first PHP script
Hello World!
COMP284 Scripting Languages Lecture 9 Slide L9 – 11
Overview Applications
PHP scripts
• PHP scripts are typically embedded into HTML documents and are enclosed between tags
• A PHP script consists of one or more statements and comments ; there is no need for a main function (or classes) • Statements end in a semi-colon • Whitespace before and in between statements is irrelevant
(This does not mean its irrelevant to someone reading your code)
• One-line comments start with // or # and run to the end of the line or ?> • Multi-line comments are enclosed in /* and */
COMP284 Scripting Languages Lecture 9 Slide L9 – 12
Types and Variables Types
Types
PHP has eight primitive types
• Four scalar types: • bool – booleans • int – integers • float – floating-point numbers • string – strings
• Two compound types: • array – arrays • object – objects
• Two special types: • resource • NULL
• Integers, floating-point numbers, and strings do not differ significantly from the corresponding Perl scalars, including the pecularities of single-quoted versus double-quoted strings
• In contrast to Perl, PHP does distinguish between different types including between the four scalar types
COMP284 Scripting Languages Lecture 9 Slide L9 – 13
Types and Variables Variables
Variables
• All PHP variable names start with $ followed by a PHP identifier • A PHP identifier consists of letters, digits, and underscores,
but cannot start with a digit PHP identifiers are case sensitive
• In PHP, a variable does not have to be declared before it can be used
• A variable also does not have to be initialised before it can be used, although initialisation is a good idea
• Uninitialized variables have a default value of their type depending on the context in which they are used
Type Default Type Default
bool FALSE string empty string
int/float 0 array empty array
If there is no context, then the default value is NULL
COMP284 Scripting Languages Lecture 9 Slide L9 – 14
Types and Variables Variables
Assignments
• Just like Java and Perl, PHP uses the equality sign = for assignments $student_id = 200846369;
As in Perl, this is an assignment expression • The value of an assignment expression is the value assigned
$b = ($a = 0) + 1;
// $a has value 0
// $b has value 1
COMP284 Scripting Languages Lecture 9 Slide L9 – 15
Types and Variables Variables
Binary assignments
PHP also supports the standard binary assignment operators:
Binary assignment Equivalent assignment
$a += $b $a = $a + $b
$a -= $b $a = $a - $b
$a *= $b $a = $a * $b
$a /= $b $a = $a / $b
$a %= $b $a = $a % $b
$a **= $b $a = $a ** $b
$a .= $b $a = $a . $b
Example:
// Convert Fahrenheit to Celsius:
// Subtract 32, then multiply by 5, then divide by 9
$temperature = 105; // temperature in Fahrenheit
$temperature -= 32;
$temperature *= 5/9; // converted to Celsius
COMP284 Scripting Languages Lecture 9 Slide L9 – 16
Types and Variables Variables
Constants
• bool define(string, expr [, case_insensitive]) • defines a constant that is globally accessible within a script • string should be a string consisting of a PHP identifier
(preferably all upper-case) The PHP identifier is the name of the constant
• expr is an expression that should evaluate to a scalar value • case_insensitive is an optional boolean argument, indicating
whether the name of the constant is case-insensitive (default is FALSE)
• returns TRUE on success or FALSE on failure
define("PI" ,3.14159);
define("SPEED_OF_LIGHT" ,299792458 , true);
COMP284 Scripting Languages Lecture 9 Slide L9 – 17
Types and Variables Variables
Constants
• To use a constant we simply use its name define("PI" ,3.14159);
define("SPEED_OF_LIGHT" ,299792458 , true);
$circumfence = PI * $diameter;
$distance = speed_of_light * $time;
• Caveat: PHP does not resolve constants within double-quoted strings (or here documents)
print "1 - Value of PI: PI\n";
print "2 - Value of PI: ".PI."\n";
1 - Value of PI: PI
2 - Value of PI: 3.14159
COMP284 Scripting Languages Lecture 9 Slide L9 – 18
Types and Variables Variables
Values, Variables and Types
PHP provides several functions that explore the type of an expression:
string gettype(expr) returns the type of expr as string
bool is_type(expr) checks whether expr is of type type
void var_dump(expr) displays structured information about expr that includes its type and value
Type of 23: integer
Type of 23.0: double
Type of "23": string
23 is an integer
COMP284 Scripting Languages Lecture 9 Slide L9 – 19
Types and Variables Type juggling and Type casting
Type juggling and Type casting
• PHP automatically converts a value to the appropriate type as required by the operation applied to the value (type juggling)
2 . " worlds" ; "2 worlds" "2" * 3 ; 6 "1.23e2" + 0 ; 123 "hello" * 3 ; 0 "10hello5" + 5 ; 15
• PHP also supports explicit type casting via (type) (int) "12" ; 12 (bool) "0" ; FALSE (int) "1.23e2" ; 1 (bool) "foo" ; TRUE (int) ("1.23e2" + 0) ; 123 (float) "1.23e2" ; 123 (int) "10hello5" ; 10 (int) 10.5 ; 10 (array) "foo" ; array(0 => "foo")
COMP284 Scripting Languages Lecture 9 Slide L9 – 20
Types and Variables Comparisons
Comparison operators
Type juggling also plays a role in the way PHP comparison operators work:
expr1 == expr2 Equal TRUE iff expr1 is equal to expr2 after type juggling
expr1 != expr2 Not equal TRUE iff expr1 is not equal to expr2 after type juggling
expr1 <> expr2 Not equal TRUE iff expr1 is not equal to expr2 after type juggling
expr1 === expr2 Identical TRUE iff expr1 is equal to expr2, and they are of the same type
expr1 !== expr2 Not identical TRUE iff expr1 is not equal to expr2, or they are not of the same type
Note: For ==, !=, and <>, numerical strings are converted to numbers and compared numerically
"123" == 123 ; TRUE "123" === 123 ; FALSE "123" != 123 ; FALSE "123" !== 123 ; TRUE "1.23e2" == 123 ; TRUE 1.23e2 === 123 ; FALSE "1.23e2" == "12.3e1" ; TRUE "1.23e2" === "12.3e1" ; FALSE 5 == TRUE ; TRUE 5 === TRUE ; FALSE
COMP284 Scripting Languages Lecture 9 Slide L9 – 21
Types and Variables Comparisons
Comparison operators
Type juggling also plays a role in the way PHP comparison operators work:
expr1 < expr2 Less than TRUE iff expr1 is strictly less than expr2 after type juggling expr1 > expr2 Greater than TRUE iff expr1 is strictly greater than expr2 after type juggling
expr1 <= expr2 Less than or equal to TRUE iff expr1 is less than or equal to expr2 after type juggling expr1 >= expr2 Greater than or equal to
TRUE iff expr1 is greater than or equal to expr2 after type juggling
’35.5’ > 35 ; TRUE ’35.5’ >= 35 ; TRUE ’ABD’ > ’ABC’ ; TRUE ’ABD’ >= ’ABC’ ; TRUE ’1.23e2’ > ’12.3e1’ ; FALSE ’1.23e2’ >= ’12.3e1’ ; TRUE "F1" < "G0" ; TRUE "F1" <= "G0" ; TRUE TRUE > FALSE ; TRUE TRUE >= FALSE ; TRUE 5 > TRUE ; FALSE 5 >= TRUE ; TRUE
COMP284 Scripting Languages Lecture 9 Slide L9 – 22
Types and Variables Comparisons
Revision
Read
• Chapter 3: Introduction to PHP
of
R. Nixon: Learning PHP, MySQL, and JavaScript. O’Reilly, 2009.
Also read
• http://uk.php.net/manual/en/language.types.intro.php • http://uk.php.net/manual/en/language.types.type-juggling.php • http://uk.php.net/manual/en/language.operators.comparison.php • http://uk.php.net/manual/en/types.comparisons.php
COMP284 Scripting Languages Lecture 9 Slide L9 – 23
http://uk.php.net/manual/en/language.types.intro.php http://uk.php.net/manual/en/language.types.type-juggling.php http://uk.php.net/manual/en/language.operators.comparison.php http://uk.php.net/manual/en/types.comparisons.php
COMP284 Scripting Languages Lecture 10: PHP (Part 2)
Handouts
Ullrich Hustadt
Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
20 Scalar types Integers and Floating-point numbers Exceptions and error handling Booleans Strings
21 Compound types Arrays Foreach-loops Array functions
22 Printing
COMP284 Scripting Languages Lecture 10 Slide L10 – 1
Scalar types Integers and Floating-point numbers
Integers and Floating-point numbers
• PHP distinguishes between • integer numbers 0 2012 -40 1263978 • floating-point numbers 1.25 256.0 -12e19 2.4e-10
• PHP supports a wide range of pre-defined mathematical functions abs(number) absolute value ceil(number) round fractions up floor(number) round fractions down round(number [,prec,mode]) round fractions log(number [,base]) logarithm rand(min,max) generate an integer random number sqrt(number) square root
• PHP provides a range of pre-defined number constants including M_PI 3.14159265358979323846 NAN ‘not a number’ INF ‘infinity’
COMP284 Scripting Languages Lecture 10 Slide L10 – 2
Scalar types Integers and Floating-point numbers
Integers and Floating-point numbers: NAN and INF
The constants NAN and INF are used as return values for some applications of mathematical functions that do not return a number
• log(0) returns -INF (negative ‘infinity’) • sqrt(-1) returns NAN (‘not a number’)
In contrast
• 1/0 returns FALSE and produces an error message • 0/0 returns FALSE and produces an error message and execution of the script continues!
In PHP 7
• 1/0 returns INF and produces an error message • 0/0 returns NAN and produces an error message and execution of the script continues!
COMP284 Scripting Languages Lecture 10 Slide L10 – 3
Scalar types Integers and Floating-point numbers
Integers and Floating-point numbers: NAN and INF
NAN and INF can be compared with each other and other numbers using equality and comparison operators:
NAN == NAN ; FALSE NAN === NAN ; FALSE NAN == 1 ; FALSE
INF == INF ; FALSE INF === INF ; TRUE INF == 1 ; FALSE
NAN < NAN ; TRUE INF < INF ; TRUE 1 < INF ; TRUE
NAN < INF ; TRUE INF < NAN ; TRUE INF < 1 ; FALSE
NAN < 1 ; TRUE 1 < NAN ; TRUE
In PHP 5.3 and earlier versions, INF == INF returns FALSE
In PHP 5.4 and later versions, INF == INF returns TRUE
COMP284 Scripting Languages Lecture 10 Slide L10 – 4
Scalar types Integers and Floating-point numbers
Integers and Floating-point numbers: NAN and INF
• PHP provides three functions to test whether a value is or is not NAN,
INF or -INF:
• bool is_nan(value)
returns TRUE iff value is NAN
• bool is_infinite(value)
returns TRUE iff value is INF or -INF
• bool is_finite(value)
returns TRUE iff value is neither NAN nor INF/-INF
• In conversion to a boolean value,
both NAN and INF are converted to TRUE
• In conversion to a string,
NAN converts to ’NAN’ and INF converts to ’INF’
COMP284 Scripting Languages Lecture 10 Slide L10 – 5
Scalar types Exceptions and error handling
Exceptions and error handling
PHP distinguishes between exceptions and errors
• A possible way to perform exception handling in PHP is as follows:
try { ... run code here ... // try
} catch (Exception $e) {
... handle the exception here using $e // catch
}
• Errors must be dealt with by an error handling function
(‘Division by zero’ produces an error not an exception)
One possible approach is to let the error handling function
turn errors into exceptions
function exception_error_handler($errno , $errstr ,
$errfile , $errline ) {
throw new ErrorException($errstr , $errno ,
0, $errfile , $errline ); }
set_error_handler("exception_error_handler");
http://www.php.net/manual/en/class.errorexception.php
COMP284 Scripting Languages Lecture 10 Slide L10 – 6
http://www.php.net/manual/en/class.errorexception.php
Scalar types Booleans
Booleans
• Unlike Perl, PHP does have a boolean datatype
with constants TRUE and FALSE (case insensitive)
• PHP offers the same short-circuit boolean operators as Java and Perl:
&& (conjunction) || (disjunction) ! (negation)
• Alternatively, and and or can be used instead of && and ||, respectively
• However, not is not a PHP operator
• The truth tables for these operators are the same as for Perl
• Remember that && and || are not commutative, that is,
(A && B) is not the same as (B && A)
(A || B) is not the same as (B || A)
COMP284 Scripting Languages Lecture 10 Slide L10 – 7
Scalar types Booleans
Type conversion to boolean
When converting to boolean, the following values are considered FALSE:
• the boolean FALSE itself
• the integer 0 (zero)
• the float 0.0 (zero)
• the empty string, and the string ’0’
• an array with zero elements
• an object with zero member variables (PHP 4 only)
• the special type NULL (including unset variables)
• SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource)
COMP284 Scripting Languages Lecture 10 Slide L10 – 8
Scalar types Strings
Strings
• PHP supports both single-quoted and double-quoted strings
• PHP also supports heredocs as a means to specify multi-line strings
The only difference to Perl is the use of <<< instead of << in their
definition:
<<
print <<