PerlIntro.pdf

(152 KB) Pobierz
A Quick, Painless Introduction to the Perl Scripting Language
Norman Matloff
University of California, Davis
c 2002-2007, N. Matloff
May 15, 2007
Contents
1
2
3
4
What Are Scripting Languages?
Goals of This Tutorial
A 1-Minute Introductory Example
Variables
4.1
4.2
4.3
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scalars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3.1
4.3.2
4.4
4.5
4.6
4.7
4.8
5
6
Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
3
3
4
4
4
5
5
6
6
7
7
8
8
9
10
Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Anonymous Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Declaration of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scope of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Conditionals, Etc.
Subroutines
6.1
6.2
Arguments, Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Some Remarks on Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1
6.3
7
8
9
Passing Subroutines As Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
12
13
14
16
Confusing Defaults
String Manipulation in Perl
Perl Packages/Modules
10 OOP in Perl
10.1 General Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
10.2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
10.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
11 Debugging in Perl
18
11.1 Perl’s Own Built-in Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
11.2 GUI Debuggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
12 To Learn More
A The Tie Operation
B Networking in Perl
20
21
24
2
1
What Are Scripting Languages?
Languages like C and C++ allow a programmer to write code at a very detailed level which has good
execution speed. But in many applications one would prefer to write at a higher level. For example, for text-
manipulation applications, the basic unit in C/C++ is a character, while for languages like Perl and Python
the basic units are lines of text and words within lines. One can work with lines and words in C/C++, but
one must go to greater effort to accomplish the same thing. C/C++ might give better speed, but if speed is
not an issue, the convenience of a scripting language is very attractive.
The term
scripting language
has never been formally defined, but here are the typical characteristics:
Used often for system administration and “rapid prototyping.”
Very casual with regard to typing of variables, e.g. no distinction between integer, floating-point or
string variables. Functions can return nonscalars, e.g. arrays, nonscalars can be used as loop indexes,
etc.
Lots of high-level operations intrinsic to the language, e.g. stack push/pop.
Interpreted, rather than being compiled to the instruction set of the host machine.
Today many people, including me, strongly prefer Python, as it is much cleaner and more elegant.
Our introduction here assumes knowledge of C/C++ programming. There will be a couple of places in
which we describe things briefly in a Unix context, so some Unix knowledge would be helpful.
1
2
Goals of This Tutorial
Perl is a very feature-rich language, which clearly cannot be discussed in full detail here. Instead, our
goals here are to (a) enable the reader to quickly become proficient at writing simple Perl programs and (b)
prepare the reader to consult full Perl books (or Perl tutorials on the Web) for further details of whatever
Perl constructs he/she needs for a particular application.
Our approach here is different from that of most Perl books, or even most Perl Web tutorials. The usual
approach is to painfully go over all details from the beginning. For example, the usual approach would be
to state all possible forms that a Perl literal can take on.
We avoid this here. Again, the aim is to enable the reader to quickly acquire a Perl foundation. He/she should
then be able to delve directly into some special topic, with little or not further learning of foundations.
3
A 1-Minute Introductory Example
This program reads a text file and prints out the number of lines and words in the file:
1
But certainly not required. Again, Perl is used on Windows and Macintosh platforms too, not just Unix.
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# ome.pl, introductory example
# comments begin with the sharp sign
# open the file whose name is given in the first argument on the command
# line, assigning to a file handle INFILE (it is customary to choose
# all-caps names for file handles in Perl); file handles do not have any
# prefixing punctuation
open(INFILE,$ARGV[0]);
# names of scalar variables must begin with $
$line_count = 0;
$word_count = 0;
# <> construct means read one line; undefined response signals EOF
while ($line = <INFILE>) {
$line_count++;
# break $line into an array of tokens separated by " ", using split()
# (array names must begin with @)
@words_on_this_line = split(" ",$line);
# scalar() gives the length of any array
$word_count += scalar(@words_on_this_line);
}
print "the file contains ",$line_count," lines and ",
$word_count, " words\n";
We’d run this program, say on the file
x,
by typing
perl ome.pl x
Note that as in C, statements in Perl end in semicolons, and blocks are defined via braces.
4
4.1
Variables
Types
Type is not declared in Perl, but rather is inferred from a variable’s name (see below), and is only loosely
adhered to.
Note that a possible value of a variable is
undef
(i.e. undefined), which may be tested for, using a call to
defined().
Here are the main types:
4.2
Scalars
Names of
scalar
variables begin with $.
Scalars are integers, floating-point numbers and strings. For the most part, no distinction is made between
these.
There are various exceptions, though. One class of exceptions involves tests of equality or inequality. For
example, use
eq
to test equality of strings but use
==
for numbers.
4
The ‘.’ is used to concatenate strings. For example,
$x = $x . "abc"
would add the string “abc” to
$x.
4.3
4.3.1
Arrays
Structure
Array names begin with @. Indices are integers beginning at 0.
Array elements can only be scalars, and not for instance other arrays. For example
@wt = (1,(2,3),4);
would have the same effect as
@wt = (1,2,3,4);
Since array elements are scalars, their names begin with $, not @, e.g.
@wt = (1,2,3,4);
print $wt[2]; # prints 3
Arrays are referenced for the most part as in C, but in a more flexible manner. Their lengths are not declared,
and they grow or shrink dynamically, without “warning,” i.e. the programmer does not “ask for permission”
in growing an array. For example, if the array
x
currently has 7 elements, i.e. ends at
$x[6],
then the
statement
$x[7] = 12;
changes the array length to 8. For that matter, we could have assigned to element 99 instead of to element
7, resulting in an array length of 100.
The programmer can treat an array as a queue data structure, using the Perl operations
push
and
shift
(usage
of the latter is especially common in the Perl idiom), or treat it as a stack by using
push
and
pop.
We’ll see
the details below.
An array without a name is called a
list.
For example, in
@x = (88,12,"abc");
we assign the array name
@x
to the list (88,12,”abc”). We will then have
$x[0]
= 88, etc.
One of the big uses of lists and arrays is in loops, e.g.:
2
2
C-style
for
loops can be done too.
5
Zgłoś jeśli naruszono regulamin