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 employers 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 Universitys 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. Youll 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:
Bachelors 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
([email protected])
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.
OReilly, 2009. OReilly, 2014.
Harold Cohen Library: 518.561.N73 or e-book
R. L. Schwartz, brian d foy, T. Phoenix:
Learning Perl. Learning Perl, 7th edition.
OReilly, 2011. OReilly, 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 < 59COMP284 Scripting Languages Lecture 1 Slide L1 21COMP284 AssessmentAcademic Integrity Plagiarism occurs when a student misrepresents, as his/her own work,the work, written or otherwise, of any other person (including anotherstudent) or of any institution Collusion occurs where there is unauthorised co-operation between astudent and another person in the preparation and production of workwhich is presented as the students own Fabrication of data occurs when a student enhances, exaggerates, orfabricates data in order to conceal a lack of legitimate dataIf you are found to have plagiarised work, colluded with others, orfabricated data, then you may fail COMP284Serious offenders may be excluded from the UniversityDo not try to take a shortcutYou must do the work yourself!COMP284 Scripting Languages Lecture 1 Slide L1 22COMP284 AssessmentAcademic Integrity: Lab rules Do not ask another student to see any part of their code for aCOMP284 assignment; contravention of this leads to collusion Do not show or make available any part of your code relating for aCOMP284 assignment to any other student; contravention of this leads to collusion Do not share (links to) on-line material that might help with aCOMP284 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 COMP284assignment, you must add comments to your code indicating its originby a proper academic reference; contravention of this is plagiarism; acknowledged code re-use may still result in a lower markCOMP284 Scripting Languages Lecture 1 Slide L1 23COMP284 Scripting LanguagesLecture 2: Perl (Part 1)HandoutsUllrich HustadtDepartment of Computer ScienceSchool of Electrical Engineering, Electronics, and Computer ScienceUniversity of LiverpoolContents3 Perl: OverviewHistoryApplicationsJava vs Perl4 ScalarsDefinitionIntegers and Floating-point numbersStringsBooleansComparisons5 Variables, Constants, and AssignmentsVariablesConstantsAssignmentsVariable interpolationCOMP284 Scripting Languages Lecture 2 Slide L2 1Perl: Overview HistoryPerl Originally developed by Larry Wall in 1987Perl 6 was released in December 2015 Borrows features from Cimperative language with variables, expressions, assignment statements,blocks of statements, control structures, and procedures / functions Lisplists, 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/// Shelluse of sigils to indicate type ($ scalar, @ array, % hash, & procedure) Object-oriented programming languagesclasses/packages, inheritance, methodsCOMP284 Scripting Languages Lecture 2 Slide L2 2Perl: Overview ApplicationsPerl: 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 assuranceCOMP284 Scripting Languages Lecture 2 Slide L2 3Perl: Overview ApplicationsPerl: Applications Applications written in Perl Movable Type web publishing platformhttp://www.movabletype.org/ Request Tracker issue tracking systemhttp://bestpractical.com/rt/ Slash database-driven web application serverhttp://sourceforge.net/projects/slashcode/COMP284 Scripting Languages Lecture 2 Slide L2 4http://www.movabletype.org/http://bestpractical.com/rt/http://sourceforge.net/projects/slashcode/Perl: Overview ApplicationsPerl: Applications Organisations using Perl Amazon online retailerhttp://www.amazon.co.uk BBC TV/Radio/Online entertainment and journalismhttp://www.bbc.co.uk Booking.com hotel bookingshttp://www.booking.com craigslist classified adshttp://www.craigslist.org IMDb movie databasehttp://www.imdb.com Monsanto agriculture/biotechhttp://www.monsanto.co.uk/ Slashdot technology related newshttp://slashdot.orgCOMP284 Scripting Languages Lecture 2 Slide L2 5http://www.amazon.co.ukhttp://www.bbc.co.ukhttp://www.booking.comhttp://www.craigslist.orghttp://www.imdb.comhttp://www.monsanto.co.uk/http://slashdot.orgPerl: Overview Java vs PerlJava versus Perl: Java1 /* Author: Clare Dixon2 * The HelloWorld class implements an application3 * 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.java2 Compile using javac HelloWorld.java3 Run using java HelloWorldCOMP284 Scripting Languages Lecture 2 Slide L2 6Perl: Overview Java vs PerlJava versus Perl: Perl1 #!/usr/bin/perl2 # Author: Ullrich Hustadt3 # The HelloWorld script implements an application4 # that prints out “Hello World”.56 print “Hello World
“;Edit-run cycle:1 Edit and save as HelloWorld2 Run using perl HelloWorld1 Edit and save as HelloWorld2 Make it executable chmod u+x HelloWorldThis only needs to be done once!3 Run using ./HelloWorldCOMP284 Scripting Languages Lecture 2 Slide L2 7Perl: Overview Java vs PerlPerl Perl borrows features from a wide range of programming languagesincluding 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 importantCOMP284 Scripting Languages Lecture 2 Slide L2 8Perl: Overview Java vs PerlPerl 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
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):548.
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: Cant take log of 0
sqrt(-1) produces an error message: Cant 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
dont ; dont
hello ; hello
backslash\ ; backslash
glass\table ; glasstable
glasstable ; glasstable
hello ; hello
dont ; dont
hello ; hello
backslash\ ; backslash
glass\table ; glasstable
glasstable ; 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
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}
; # chinese
print x{062d}x{fef0}
; # 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 < ltGreater than > gt
Less than or equal to <= leGreater than or equal to >= ge
Examples
35 == 35.0 # true
35 eq 35.0 # false
35 == 35.0 # true
35 < 35.0 # false35 lt 35.0 # trueABC eq “Uabc” # trueCOMP284 Scripting Languages Lecture 2 Slide L2 23Variables, Constants, and Assignments VariablesScalar variables Scalar variables start with $ followed by a Perl identifier A Perl identifier consists of letters, digits, and underscores,but cannot start with a digitPerl 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 24Variables, Constants, and Assignments VariablesScalar 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 undefHowever, undef actslike 0 for numeric variables andlike for string variablesif 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,”
“;print $s1 defined:,(defined($s1)) ? TRUE:FALSE,”
“;print $s2 defined:,(defined($s2)) ? TRUE:FALSE,”
“;$s1 eq undef: TRUE$s1 defined: TRUE$s2 defined: FALSECOMP284 Scripting Languages Lecture 2 Slide L2 25Variables, Constants, and Assignments VariablesSpecial Variables Perl has a lot of pre-defined variables that have a particular meaningand serve a particular purposeVariable 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 seehttps://perldoc.perl.org/perlvar.html#SPECIAL-VARIABLESCOMP284 Scripting Languages Lecture 2 Slide L2 26https://perldoc.perl.org/perlvar.html#SPECIAL-VARIABLESVariables, Constants, and Assignments ConstantsConstantsPerl 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
;
print 2: ,$actor , won the ,$prize , in ,$year ,
;
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.
OReilly, 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 3Control structures Conditional statementsBlocks A sequence of statements in curly brackets is a block; an alternative definition of conditional statements isif (condition) blockelsif (condition) blockelse block Instatement if (condition );statement unless (condition );only a single statement is allowedbut do block counts as a single statement,so we can writedo block if (condition );do block unless (condition );COMP284 Scripting Languages Lecture 3 Slide L3 4Control structures Switch statementsControl structures: switch statement/expressionStarting with Perl 5.10 (released Dec 2007), the language includes aswitch statement and corresponding switch expressionBut these are considered experimental and need to be enabled explicitlyExample: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 neededCOMP284 Scripting Languages Lecture 3 Slide L3 5Control structures While- and Until-loopsControl structures: while- and until-loops Perl offers while-loops and until-loopswhile (condition) {statements}until (condition) {statements} A proper until-loop where the loop is executed at least oncecan be obtained as followsdo { statements } until (condition );The same construct also works for if, unless and whileIn case there is only a single statement it is also possible to writestatement until (condition );Again this also works for if, unless and whileCOMP284 Scripting Languages Lecture 3 Slide L3 6Control structures For-loopsControl structures: for-loops for-loops in Perl take the formfor (initialisation; test; increment) {statements}Again, the curly brackets are required even if the body of the loop onlyconsists 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 7Lists and Arrays IdentifiersLists 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@identifierAn array variable denotes the entire list stored in that variable Perl uses$identifier[index]to denote the element stored at position index in @identifierThe first array element has index 0 Note that$identifier@identifierare two unrelated variables (but this situation should be avoided)COMP284 Scripting Languages Lecture 3 Slide L3 8Lists and Arrays List literalsList literals A list can be specified by a list literal, a comma-separated list of valuesenclosed 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/lengthCOMP284 Scripting Languages Lecture 3 Slide L3 9Lists and Arrays List literalsSize 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 indicesin 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 @arrayCOMP284 Scripting Languages Lecture 3 Slide L3 10Lists and Arrays List literalsArray index out of bound Perl, in contrast to Java, allows you to access array indices that areout 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
“;print $array [5] = ,$array [5], , which ,(defined($array [5]) ? “IS NOT” : “IS”, “undef
“;$array [1] = , which IS undef$array [5] = , which IS undef The function exists can be used to determine whether an array indexis within bounds and has a value (including undef) associated with itprint $array [1] exists: ,exists($array [1]) ? “T”:”F”,”
“;print $array [5] exists: ,exists($array [5]) ? “T”:”F”,”
“;$array [1] exists: T$array [5] exists: FCOMP284 Scripting Languages Lecture 3 Slide L3 11Lists and Arrays ContextsScalar context versus list context Scalar contextwhen an expression is used as an argument of an operation that requiresa scalar value, the expression will be evaluated in a scalar contextExample:$arraySize = @array;; @array stores a list, but returns the number of elements of @arrayin a scalar context List contextwhen an expression is used as an argument of an operation that requiresa list value, the expression will be evaluated in a list contextExample:@sorted = sort 5;; A single scalar value is treated as a list with one element in alist contextCOMP284 Scripting Languages Lecture 3 Slide L3 12Lists and Arrays ContextsScalar context versus list contextExpressions behave differently in different contexts following these rules: Some operators and functions automatically return different values indifferent 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),
;
5 $last = pop(@planets );
6 print Array@2: , join( ,@planets),
;
7 $first = shift(@planets );
8 print Array@3: , join( ,@planets),
;
9 print @4: ,$first , ,$last ,
;
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 arrays 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,
;
print Size of $array: ,$#array+1,
;
delete($array [3]);
print $array [3] exists: ,exists($array [3])?T:F,
;
print Size of $array: ,$#array+1,
;
$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
);
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 ).
;
foreach $number (@my_list) {
$number ++;
}
print After:.join(, ,@my_list ).
;
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
);
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 $_!
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
,$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 }
;
print $hash2{b } = $hash2{b }
;
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
);
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.
OReilly, 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 companys
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
} else { print No match
}
Output:
Match
$_ = ababcbcdcde;
if (/dbd/) { print Match
} else { print No match
}
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]: $&
}
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
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, [adt
-\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,: $&
};
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,: $&
};
$_ = E00481370;
if (/[A-Z]0{2}(d+)/) {
print Match at positions $-[1] to ,$+[1]-1,: $1
};
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,: $&
};
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.
OReilly, 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
}
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
}
3 if (/( dog|cat|bird )/) { print Match 2: $1
}
4 if (/( dog|dogs )/) { print Match 3: $1
}
5 if (/( dogs|dog )/) { print Match 4: $1
}
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
z Match only at string end
$ Match only at string end modulo a preceding
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
played with fire
;
if (/firez/) { print fire at string end
}
if (/fireZ/) { print fire at string end modulo \n
}
fire at string end modulo
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
^ matches only at string start
$ matches only at string end modulo preceding
/ /s Treat string as a single long line
. matches any character including
^ matches only at string start
$ matches only at string end modulo preceding
/ /m Treat string as a set of multiple lines
. matches any character except
^ 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
^ matches at a line start
$ matches at a line end
/ /i perform a case-insensitive match
Example:
$_ = bill
Clinton;
if (/( Bill|Hillary ). Clinton )/smi) { print Match: $1
}
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]: $&
}
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]: $&
}
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),
;
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]: $&
}
if (/[a-z]+/g) { print Match starts at $-[0]: $&
}
if (/d+/g) { print Match starts at $-[0]: $&
}
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]: $&
}
3 if (/ab/g) { print 3: Match starts at $-[0]: $&
}
4 if (/d+/g) { print 4: Match starts at $-[0]: $&
}
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]: $&
}
3 if (/ab/gc) { print 3: Match starts at $-[0]: $&
}
4 if (/d+/g) { print 4: Match starts at $-[0]: $&
}
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,Lw+,t\w+);
while ($keyword = shift(@keywords )) {
print Match found for $keyword: $&
if /$keyword/i;
}
Output:
Match found for bart: Bart
Match found for lisa: Lisa
Match found for Lw+: Lisa
Match found for tw+: 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
}
$name = Dave Shield;
if ($name = /(Mr|Ms|Mrs|Dr)?s*(w+)/) {print Hello $2
}
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
;
$name = Dave Shield;
($t ,$f ,$l) = ($name = /(Mr|Ms|Mrs|Dr)?s*(w+)s+(w+)/);
print Name: $t, $f , $l
;
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 }
}
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.
OReilly, 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
;
Output:
http :// www.myorg.co.uk/info/vat.html
Example:
$_ = Yabba dabba doo;
s/bb/dd/;
print $_,
;
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
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$3E, $2/;
print $name
;
$name = Dave Shield;
$name = s/(Mr|Ms|Mrs|Dr)?s*(w+)s+(w+)/ U$3E, $2/;
print $name
;
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 $_,
;
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
;
5 $text = s!(d+.d+)! sprintf(%d,$1 +0.5)!e;
6 print $text
;
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 12Subroutines Parameters and ArgumentsPrivate variablessub 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 ,”
“;print “Return value of sum: “,&sum(5,4,3,2,1),”
“;print “Value of $sum aftercall of sum: “,$sum ,”
“;produces the outputValue of $sum before call of sum: 5Return value of sum: 15Value of $sum after call of sum: 15This use of global variables in subroutines is often undesirable; we want $sum to be private/local to the subroutineCOMP284 Scripting Languages Lecture 6 Slide L6 13Subroutines Parameters and ArgumentsPrivate 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 variablesExample:sub sum {return undef if (@_ < 1);my $sum = shift(@_);foreach (@_) { $sum += $_ }return $sum;}COMP284 Scripting Languages Lecture 6 Slide L6 14Subroutines Calling a subroutineCalling a subroutineA subroutine is called by using the subroutine name with an ampersand &in front possibly followed by a list of argumentsThe ampersand is optional if a list of arguments is presentsub identifier {statements}… &identifier …… &identifier(arguments) …… identifier(arguments) …Examples:print “sum0: ” ,&sum ,”
“;print “sum0: “,sum(),”
“;print “sum1: “,sum(5),”
“;print “sum2: “,sum(5,4),”
“;print “sum5: ” ,&sum(5,4,3,2,1),”
“;$total = sum(9,8,7,6)+sum(5,4,3,2,1);sum(1,2,3,4);COMP284 Scripting Languages Lecture 6 Slide L6 15Subroutines Persistent variablesPersistent variables Private variables within a subroutine are forgotten once a call of thesubroutine is completed In Perl 5.10 and later versions, we can make a variableboth private and persistent using the state operator; the value of a persistent variable will be retained betweenindependent calls of a subroutineExample:use 5.010;sub running_sum {state $sum;foreach (@_) { $sum += $_ }return $sum;}COMP284 Scripting Languages Lecture 6 Slide L6 16Subroutines Persistent variablesPersistent variablesExample:1 use 5.010;23 sub running_sum {4 state $sum;5 foreach (@_) { $sum += $_ }6 return $sum;7 }89 print “running_sum ():tt”, running_sum (), ”
“;10 print “running_sum (5):t”, running_sum (5), ”
“;11 print “running_sum (5 ,4):t”, running_sum (5,4), ”
“;12 print “running_sum (3,2 ,1):t”,running_sum (3,2,1),”
“;Output:running_sum ():running_sum (5): 5running_sum (5 ,4): 14running_sum (3,2,1): 20COMP284 Scripting Languages Lecture 6 Slide L6 17Subroutines Nested subroutine definitionsNested 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 frominside 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 18Subroutines Nested subroutine definitionsNested subroutine definitionsIf an inner subroutine uses a local variable of an outer subroutine,then it refers to the instance of that local variable created the first timethe outer subroutine was calledsub outer {my $x = $_[0];sub inner { return $x }return inner (); # returns $_[0]?}print “1: “,outer (10),”
“;print “2: “,outer (20),”
“;1: 102: 10 # not 20!; Do not refer to local variables of an outer subroutine,pass information via arguments insteadCOMP284 Scripting Languages Lecture 6 Slide L6 19Subroutines Nested subroutine definitionsNested subroutine definitions: Examplesub 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 20Subroutines Nested subroutine definitionsRevisionRead Chapter 9: Processing Text with Regular Expressions Chapter 4: SubroutinesofR. L. Schwartz, brian d foy, T. Phoenix:Learning Perl.OReilly, 2011. http://perldoc.perl.org/perlsub.htmlCOMP284 Scripting Languages Lecture 6 Slide L6 21http://perldoc.perl.org/perlsub.htmlCOMP284 Scripting LanguagesLecture 7: Perl (Part 6)HandoutsUllrich HustadtDepartment of Computer ScienceSchool of Electrical Engineering, Electronics, and Computer ScienceUniversity of LiverpoolContents13 Input/OutputFilehandlesOpenCloseReadSelectPrintHere documents14 Arguments and OptionsInvocation ArgumentsOptionsCOMP284 Scripting Languages Lecture 7 Slide L7 1Input/Output FilehandlesI/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 identifierBeware: Despite the terminology, no files might be involved There are six pre-defined filehandlesSTDIN Standard Input, for user input, typically the keyboardSTDOUT Standard Output, for user output, typically the terminalSTDERR Standard Error, for error output,typically defaults to the terminalDATA Input from data stored after __END__ at the end of aPerl programARGV Iterates over command-line filenames in @ARGVARGVOUT Points to the currently open output file when doing edit-in-place processing with -iperl -pi -e s/cat/dog/ fileCOMP284 Scripting Languages Lecture 7 Slide L7 2Input/Output FilehandlesI/O ConnectionsExcept for the six predefined I/O connections, all other I/O connections need to be opened before they can be usedopen filehandle, mode, expr should be closed once no longer neededclose 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
)
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:
. (%10s
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
Reviews
There are no reviews yet.