124
1 Perl Tutorial 林林林

Perl Tutorial 林光龍. 22 The Goal of this Course To know what is Perl programming language To know how to design program using Perl

Embed Size (px)

Citation preview

1

Perl Tutorial

林光龍

22

The Goal of this Course

• To know what is Perl programming language

• To know how to design program using Perl

33

Course Outline

• Perl Introduction

• How to work with perl on your PC

• Fundamental programming skill

• Advanced programming skill

4

Perl Introduction

• What is Perl?

• Why Perl?

4

5

What is Perl?• Perl is acronym for Practical Extraction and Report

Language • High level scripting language • Originally developed by Larry Wall in 1987 for UNIX

platform (it has been developed ever since as Open Source project)

• Now Perl available for almost all platform • It has syntax like C and shell scripting • Useful to write CGI scripts (Web/Internet applications),

database applications, network programming, biological data analysis and automating system administration task etc.

5

6

Why Perl?

• Easy to use and efficient scripting language • It is portable • It is free and runs on wide range of platform

(UNIX/Linux/BSD/Windows etc) • Ready to use libraries available (called as

modules) for different tasks such as network or database programming

• Read Ten Perl Myths by Simon Cozens

6

7

How to work with perl on your PC

• Unix-like Platform

• Windows Platform

7

8

Unix-like Platform

• Find out the pathname of the Perl file which would be executed in the your environment with which command

8

$ which perl/usr/bin/perl

9

Windows Platform (1/2)

• To download and install the latest ActivePerl (ActivePerl-5.10.0.1005-MSWin32-x86-290470.msi) from http://www.activestate.com/activeperl/

• To download and install the PSPad, a freeware programmer’s editor, from http://www.pspad.com/en/

10

Windows Platform (1/2)

1111

Fundamental Programming Skill

• Your First Perl Program• Comments• Literal & Variable• String

– Interpolated String– Non-interpolated String

• Operation• Statement• Control Statement

1212

Advanced Programming Skill

• Special Variable

• Subroutine

• Modules (CPAN)

• Regular Expression

• File Operation

13

Your First Perl Program (1/4)

13

#!/usr/bin/perl# Your first Perl Program to display message# Program to display knowledge is power on screenprint "Knowledge is power \n";

14

Your First Perl Program (2/4)

15

Your First Perl Program (3/4)#!/usr/bin/perluse strict; # important pragmause warnings; # another important pragmaprint "What is your username? "; # print out the questionmy $username; # "declare" the variable$username = <STDIN>; # ask for the usernamechomp($username); # remove "new line"print "Hello, $username.\n"; # print out the greeting

16

Your First Perl Program (4/4)

17

Comments (1/2)

• The comments are advantageous in the sense that they make the programmer feel convenient to grasp the logic of the program.

• The comments are ignored by the Perl interpreter, they are included in the program for the convenience of the programmer to understand the code

18

Comments(2/2)

• Comment’s notations:– Single line comment: opening each line with #– Multi-line comments: begin with =head and

ending with =cut

# Everything after the # is ignored=headIt is very important to place comments into your Perl programs. Comments will enable you to figure out the intent behind the mechanics of your program. =cutprint "Knowledge is power \n";

19

Literal

• A literal is a value that is represented "as is" or hard-coded in your source code.

• 45.5 refers to forty-five and a half

• Perl uses four types of literals:– Numbers– Strings– Arrays– Associative Arrays

20

Numeric Literals (Integer Value)

Base Value Example Meaning

10 123 print 123;

8 0173 print 0173;

16 0x7B print 0x7B;

21

Numeric Literals (Float Value)

• Scientific notation

print 0.000034;print "\n";print 3.4e-5;print "\n";print 3.4e+5;print "\n";

22

String Literal

• String Literals are groups of characters surrounded by quotes.

• In Perl you can use single quotes ('), double quotes("), and back quotes (`).

#!/usr/bin/perluse strict;use warnings;print 'Date\n';print "Date\n";print `Date`;

23

Variables

• literals - values that don't change while your program runs because you represent them in your source code exactly as they should be used

• variable - a name for a container that holds one or more values. The name of the variable stays the same throughout the program, but the value or values contained in that variable typically change repeatedly throughout the execution of the program.

$a

2

$a = 2;

3

$a = 2;$a = 3;

23

$a

24

Variable Types (1/2)Variable Type (Data Type)

Description Example

ScalarsHolds one number or string value at a time. Scalar variable names always begin with a $.

$first_name = "Jacob";

$total_widgets = 43;

$price = 12.99;

Arrays (List)

Holds a list of values. The values can be numbers, strings, or even another array. Array variable names always begin with a @.

@names = ( “Bob", "Jim", "Ken" );

@salaries = ( 54000, 22500, 45000 );

@prices = ( 14.99, 19.99, 29.99, 39.99, 59.99 );

Associative Arrays (Hashes)

Uses any value as an index into an array. Associative array variable names always begin with a %.

%pet = (bob => "Alex", jim => “Judy", ken => “Tom");

%colour_purple = ( r => 255, g => 0, b => 255 );

%prices = ( "Super Widget" => 39.99,

"Wonder Widget" => 49.99,);

Reference

A reference is a scalar value that points to a memory location that holds some type of data. To create a reference is to add the backslash(\) to the front of a value or variable.

$refScalar = \$scalar;

$refArray = \@array;

$refHash = \%hash;

25

Variable Types (2/2)• Perl is a dynamically typed language

– There is no integer, string, character, or pointer type in Perl. Strings, characters, and numbers are all treated roughly interchangeably because they can be converted from one to the other internally by the Perl interpreter during runtime.

– All of the numeric data types in C, pointers, and even strings, all correspond to a single data-type in Perl: the scalar.

– Unlike C, which is statically typed, Perl is dynamically typed. What this means is that the datatype of any particular scalar variable is not known at compile-time.

26

Pragma (1/2)

• Perl tends to be a pretty permissive language. But maybe you want Perl to impose a little discipline; that can be arranged with the use strict pragma.

• A pragma is a hint to a compiler, telling it something about the code.

• The 'use strict' and 'use warnings' pragma tells Perl's internal compiler that it should enforce some good programming rules for the rest of this block or source file.

27

Pragma (1/2)$bamm_bamm = 3; # Perl creates that variable automatically$bammbamm += 1; # Oops!print $bamm_bamm;

use strict; # Enforce some good programming rulesuse warnings; # To warn as much as possible when you write code that might be questionablemy $bamm_bamm = 3; # New lexical variable$bammbamm += 1; # No such variable: Compile time errors!print $bamm_bamm;

$something = "Hello World"; # Create a variable out of thin airprint $somehting; # automatically create a variable & be initialized to an empty string.

use strict; use warnings;

my $something = "Hello World"; # New lexical variableprint $somehting; # No such variable: Compile time errors!

28

Scalar Variables

• Scalar variables are used to track single pieces of information.

• All scalars begin with a $

# Assign a value of 23 to a variable called $numberOfRooms. $numberOfRooms = 23; #Add 5 to the $numberOfRooms variable.$numberOfRooms = $numberOfRooms + 5;# Assign a value of Perl by Example to a variable called $bookTitle.$bookTitle = "Perl by Example";

23

$a

"Perl"

$b

29

Array Variables

• Arrays are ordered, integer-indexed collections of any object.

• Array indexing starts at 0.• Array variable names always begin with a @ character.

@a = (1, 2, 3);

3$a[2]

21$a[1]

$a[0] @a

($a, $b) = (1, 2);1

$a

2$b

30

@a = (100, "Cat");

Cat

$a[1]

100

$a[0] @a

@a = ();@a

@a = (1, 2, 3);($a1, $a2) = @a;

2$a2 1

$a1

3$a[2]

21$a[1]

$a[0] @a

31

$a4 3$a3

@a = (1, 2, 3);($a1, $a2, $a3, $a4) = @a;

2$a2 1

$a1

3$a[2]

21$a[1]

$a[0] @a

@a = (1, 2, 3);

3$a[2]

21$a[1]

$a[0] @a$a[-1] $a[-

2] $a[-3]

32

3$b

@a = (1, 2, 3);$b = @a;

3$a[2]

21$a[1]

$a[0] @a

@a = (1, 2, 3);$b = $#a; 2

$b

3$a[2]

21$a[1]

$a[0] @a

33

11$b

@a = (1, 2, 3, 4);$a[10] = 11;$b = @a;print $b;

3$a[2]

21$a[1]

$a[0] @a

11$a[10] …

8$a[2]

76$a[1]

$a[0] @a

@a = (1, 2, 3, 4, 5);@a = (6, 7, 8);

34

@emptyArray = ();@numberArray = (12, 014, 0x0c, 34.34, 23.3E-3);@stringArray = ("This", "is", 'an', "array", 'of', "strings");@mixedArray = ("This", 30, "is", 'a', "mixed array", 'of', 0x08, "items");print "Here is an empty array:" . @emptyArray . "<-- Nothing there!\n";print @numberArray; print "\n";print @stringArray; print "\n";print @mixedArray; print "\n";

@a = ();print "Number of elements:";print @a . "\n";$a[5] = 10;print "Number of elements:";print @a . "\n";@a = (1, 2, 3);print "Number of elements:";print @a . "\n";

35

@smallArrayOne = (5..10);@smallArrayTwo = (1..5);@largeArray = (@smallArrayOne, @smallArrayTwo);print @largeArray;

#Create an array with five elements. @array = (1..5);#Print the array. print @array; print "\n";#Print each element of the array.print $array[0]; print "\n";print $array[1]; print "\n";print $array[2]; print "\n";print $array[3]; print "\n";print $array[4]; print "\n";

$index = 2;@array = (1..5);print $array[$index]; print "\n";

36

Interpolated String (1/5)

• In Perl, any string that is built with double quotes will be interpolated. That is, any variable or escaped char that appears within the string will be replaced with the value of that variable.

37

Interpolated String (2/5)

#!/usr/bin/perluse strict;use warnings;

my @friends = ('Margaret', 'Richard', 'Carolyn', 'Rohan', 'Cathy', 'Yukiko');print "Friends: @friends\n";

#!/usr/bin/perluse strict;use warnings;my $apples = 4;print "I have $apples apples\n";

38

Interpolated String (3/5)

• The function qq() works just like double quotes, but makes it easy to put double quotes in your string:

#!/usr/bin/perluse strict;use warnings;

my $apples = 4;print qq(I "have" $apples apples\n);

39

Interpolated String (4/5)• Perl offers a convenient way of printing multiple

lines of output through an interesting feature known as a "here" document.

• A here document works like this:– The first line of your command will include the two

characters << followed by a "special" identifier string, followed by a semi-colon.

– Next, just enter all of the lines of output that you want to print.

– When you are ready to terminate the output, put your special identifier string on a line by itself to end the output.

40

Interpolated String (5/5)

my $a = "Hello";my $str = "There you go.";my $true = "False";

print <<"END";The value of \$a is: "$a"The value of \$str is: "$str"The value of true is: "$true"END

41

Escape Character

• There are some characters that can't be written directly in a string. The backslash ('\') character precedes any of these special characters.

• For example, if a string contains a double quotes, put a backslash ('\') in front of each internal double quote, eg "abc\"def\"ghi".

42

Table of Escape Sequences

• Escape sequence is a character constant of special characters.

\n new line

\r return

\t tab

\f Form Feed

\b Backspace

\a Bell

\e Escape

\0ddd Any octal ASCII value (here, 007 = bell)

\0xddd Any hex ASCII value (here, 7f = delete)

\cC Any "control" character (here, CTRL-C)

\\ Backslash

\" Double quote

\l Lowercase next letter

\L Lowercase all following letters until \E

\u Uppercase next letter

\U Uppercase all following letters until \E

\Q Backslash-quote all nonalphanumerics until \E

\E Terminate \L , \U, or \Q

43

Non-interpolated String (1/3)

• Single quoted strings do not interpolate variables or most escaped values, such as \n, (however \' is the exception). Single quotes are helpful when you want to include a $ or a % or any other special character in your output.

#!/usr/bin/perluse strict;use warnings;print 'Those strawberries cost $2.50';

44

Non-interpolated String (2/3)

• The function q() works the same as single quotes, except it makes it easier to include a single quote in your data.

#!/usr/bin/perluse strict;use warnings;print q(Bob's strawberries cost $2.50);

45

Non-interpolated String (3/3)

• Variables in here documents, where the end token is surrounded with single quotes, are not interpolated.

my $apples = 4;my $oranges = 7;my $pears = 3;print <<'EOT';My fruit list: $apples apples $oranges oranges $pears pearsEOT

46

Back-Quoted Strings

• Perl uses back-quoted strings to execute system commands. When Perl sees a back-quoted string, it passes the contents to Windows, UNIX, or whatever operating system you are using.

print `dir *.txt`;

47

Type of Operation

• Bitwise operation• Arithmetic operation• String operation• Range operation• Assignment operation• Comparison operation• Logical operation• File test operation

48

Operators of Bitwise Operation

Operator Action Example Meaning

& Bitwise AND

| Bitwise OR

^ Bitwise XOR

~ Bitwise one's complement

>> Bitwise right shift

<< Bitwise left shift

49

Operators of Arithmetic Operation

Operator Action Example Meaning

+ Addition

- Subtraction

* Multiplication

** Exponentiation

/ Division

% Modulus

= Assignment

50

Operators of Increment/Decrement

Operator Action Example Meaning

++

Prefix Increment

Postfix Increment

--

Prefix Decrement

Postfix Decrement

51

Operators of Assignment Operation

Operator Action Example Meaning

= Normal Assignment

+= Add and Assign

-= Subtract and Assign

*= Multiply and Assign

/= Divide and Assign

%= Modulus and Assign

**= Exponent and Assign

52

Operators of String Operation

Operator Action Example Meaning

. Concatenate Strings

.= Concatenate and Assign

++

Prefix String Increment

Postfix String Increment

x String Repeatto repeat any string a given number of times

53

Range Operation

• The range operator is used to create a range of elements in arrays. It can also be used in a scalar context.

#Create an array with ten elements that include 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.@array = (1..10);#Create an array with ten elements that include A, B, C, D, E, F, G, H, I , and J.@array = ("A".."J");#Create an array that includes AAA, 1, 2, 3, 4, 5, A, B, C, D, and ZZZ.@array = ("AAA", 1..5, "A".."D", "ZZZ");#Create an array with ten elements that include the strings 01, 02, 03, 04, 05, 06, 07, 08, 09, and 10.@array = ("01".."10");#Create an array that includes the strings aa, ab, ac, ad, ae, and af.@array = ("aa" .. "af");#Create an array that includes the strings ay, az, ba, bb, bc, bd, be, and bf.@array = ("ay" .. "bf");

54

Comparison Operation

• Perl has different operators (relational and equality operators) for comparing numbers and strings.

• Comparison operators compare the values of two variables or statements and return the result of the comparison as either true or false.

55

Numeric Comparison (1/2)

Operator Action Example Meaning

== Equal To

!= Not Equal To

< Great Than

<= Great Than or Equal To

> Less Than

>= Less Than or Equal To

<=> Comparison

56

Numeric Comparison (1/2)

$a = 10;$b = 20;print '$a = ' . $a . ', $b =' . "$b\n";print '$a < $b : ' . ($a < $b) . "\n";print '$a > $b : ' . ($a > $b) . "\n";print '$a == $b : ' . ($a == $b) . "\n";print '$a <=> $b : ' . ($a <=> $b) . "\n";print '$a = $b : ' . ($a = $b) . "\n";

57

The Standard ASCII Characters

58

String Comparison

Operator Action Example Meaning

eq Equal To

ne Not Equal To

gt Great Than

ge Great Than or Equal To

lt Less Than

le Less Than or Equal To

cmp Comparison

59

Logical Operation

Operator Action Example Meaning

&&

andLogical AND

||

orLogical OR

xor Exclusive OR

!

notLogical negation

60

Conditional Operator (1/2)• The conditional operator (? :) is a ternary operator (it

takes three operands). The conditional operator works as follows: – The first operand is implicitly converted to boolean. It is

evaluated and all side effects are completed before continuing. – If the first operand evaluates to true, the second operand is

evaluated. – If the first operand evaluates to false, the third operand is

evaluated.

use strict; use warnings;print "How many camels do you have?";my $n = <STDIN>; chomp($n); # remove "new line"printf "You have %d camel%s.\n", $n, $n == 1 ? "" : "s";

61

Conditional Operator (2/2)

use strict; use warnings;print "Input year?";my $year = <STDIN>; chomp($year); # remove "new line"my $leapyear = $year % 4 == 0 ? $year % 100 == 0 ? $year % 400 == 0 ? 1 : 0 : 1 : 0;printf "%d is %sleap year.\n", $year, $leapyear == 1 ? "" : "not ";

62

Short-Circuit Boolean Operation (1/2)

• The expression boolean1 is evaluated. If it is false, the result of the overall expression is false, the expression boolean2 not being evaluated. If it is true, the expression boolean2 is evaluated. If it is true, the result of the overall expression is true. Otherwise, the result of the overall expression is false.

• The expression boolean1 is evaluated. If it is true, the result of the overall expression is true, the expression boolean2 not being evaluated. If it is false, the expression boolean2 is evaluated. If it is true, the result of the overall expression is true. Otherwise, the result of the overall expression is false.

ConditionIf the left condition is true, this statement is executed&&

ConditionIf the left condition is false,

this statement is executed||

63

Short-Circuit Boolean Operation (2/2)

$a = 4;($a < 10) || (print "\$a greant than 10.\n");($a < 10) && (print "\$a less than 10.\n");$a = ($a != 0) && ($a * 2);($a == 10) && (print "\$a is 10.\n");($a != 8) || (print "\$a is 8.\n");

64

Precedence and Associativity of Operators (1/2)

1 -> left

2 ++ -- non

3 ** right

4 ! ~ \ + - rightLogical not, bitwise not, reference , unary +, unary -

5 =~ ! ~ left Pattern matching

6 * / % x left

7 + - . left

8 << >> left

9unary operators

non

10< > <= >= lt gt le ge

non

11== != <=> eq ne cmp

non

12 & left

13 | ^ left

14 && left

15 || left

16 .. non

17 ? : right

18= += -= *= /=

right

19 , => left

20list operators

non

21 not right

22 and left

23 or xor left

65

Precedence and Associativity of Operators (1/2)

$xyz = $x || $y || $z;$xyz = $x or $y or $z; # WRONG$xyz = ( $x or $y or $z );

$x = $a = 1;$x = ++$a;print $x, $a;print "\n";$x = $a = 1;$x = $a++;print $x, $a;

$x = "A";$x = ++$x;

$x = "aaa";$x = ++$x;

$x = 4;$y = 3;$z = 2;$x = $x ** $y ** $z;

66

Statements• A Perl script consists of a series of statements

and comments. Each statement is a command that is recognized by the Perl interpreter and executed.

• Statements are terminated by the semicolon character (;). They are also usually separated by a newline character to enhance readability.

• In Perl, semicolons are statement separators, not statement terminators, so a semicolon isn't required after the very last statement in a block.

• Statement Types:

67

Statement Types

Description Example

No-action statements

These statements evaluate a value but perform no actions.

10+20;

Action statements

These statements perform some action. $a++;

Assignment statements

These statements assign a value to one or more variables.

$a = $b = $c;

Decision statements

These statements allow you to test a condition and choose among one or more actions.

Control Statements

Jump statements

These statements let you unconditionally change the program flow to another point in your code.

Control Statements

Loop statements

These statements let you perform a series of statements repeatedly while some condition is true or until some condition is true.

Control Statements

Modified Statements

These statements let you use the if, unless, until, and while keywords to change the behavior of a statement.

$t-=12 if ($t > 12);$t -= 12 unless ($t <= 12);

68

Statement Block

• A statement block is a group of statements surrounded by curly braces ({ }).

• If a bare block is unlabelled, we call it an anonymous block.

{ statement; ... statement; statement}

69

Statement Blocks and Local Variables

• It's a good idea to place all of your variable initialization at the top of a program or function. However, if you are maintaining some existing code, you may want to use a statement block and local variables to minimize the impact of your changes on the rest of the code.

• You can use the my() function to create variables whose scope is limited to the statement block. This technique is very useful for temporary variables that won't be needed elsewhere in your program.

$firstVar = 10;{ my($firstVar) = "A"; print $firstVar x 5 . "\n";}print("firstVar = $firstVar\n");

70

Control Statements

• We can divide the control statements in two main categories: – conditional statements - which use an

expression to check if a condition is met– loop statements - which checks a condition

and executes the statements included in curly braces until that condition is evaluated either true or false

71

Conditional Statements

• The expressions in a conditional statement are evaluated in a boolean context.

• We can divide the conditional statements in three main categories:– if/elsif/else – which will execute a block only when a

conditional expression is evaluated as true– unless/elsif/else – which will execute the block when a

condition expression is evaluated as false – it is the reverse of the "if" statement

– switch/case/else, given/when/default

72

Loop Statements (1/2)

• If you need to execute a code block more than once, process known as iteration, you must use this type of Perl statements.

• All the basic Perl loop statements imply the execution of three steps:1. evaluate an expression condition in a boolean

context

2. if the expression is evaluated as true/false, Perl will execute the next block of statements

3. resume the code with the step 1

73

Loop Statements (2/2)• We can divide the loop statements in six main categories, you

can choose the one which is most appropriate for your task:– for – executes a given set of statements for a certain number of times; in

the case of the "for" statement, the conditional expression consists of three parts (initialization, condition, increment), delimitated by a semicolon symbol

– while – executes the block repeatedly as long as a conditional expression is true; the expression is tested before the first iteration of the loop

– do-while – you can use this loop statement when you need to execute a piece of code at least once and then iterate consecutively while the condition is true

– until – is like while, except that it executes the block only if the expression is false; the expression will be tested before the first iteration, too

– do-until – operates similar to "do-while", except that it will iterate only if the conditional expression is evaluated false

– foreach – operates over a list of values or array by setting consecutively a control variable to each element of the list

74

if/elsif/else Statements

• if (EXPR) BLOCK• if (EXPR) BLOCK else BLOCK• if (EXPR) BLOCK elsif (EXPR) BLOCK ...

else BLOCK

• STATEMENT if (EXPR)

75

$a = -1;

if($a < 0) {

$a += 2;

}

print "a = $a\n";

$a = 15;

$b = 27;

if($a > $b) {

print "max($a, $b) = $a\n";

} else {

print "max($a, $b) = $b\n";

}

$a = 15; $b = 27;

print "max($a, $b) = ", $a > $b ? $a : $b,"\n";

76

chomp ($line = <STDIN>);

$line = lc($line);

if($line eq "index"){

print "index function\n";

} elsif ($line eq "chomp") {

print "chomp function\n";

} elsif ($line eq "hex") {

print "hex function\n";

} elsif ($line eq "substr") {

print "substr function\n";

} else {

print "$line: unknown function\n";

}

$v = "An example with the if modifier";

print "\$v contains the word \'example\'\n" if ($v =~ /example/);

77

unless/elsif/else Statements

• unless (EXPR) BLOCK• unless (EXPR) BLOCK else BLOCK• unless (EXPR) BLOCK elsif (EXPR)

BLOCK ... else BLOCK

• STATEMENT unless (EXPR)

78

$b = 0;

unless($b != 0) {

$b += 1;

}

print "b = $b\n"; # it outputs b = 1

$a = 35;

$b = 22;

unless($a > $b) {

print "min($a, $b) = $a\n";

} else {

print "min($a, $b) = $b\n";

}

# it prints: min(35, 22) = 22

79

$a = 12;

unless($a >= 0) {

print "a is negative\n";

} elsif ($a == 0) {

print "a is equal to 0\n";

} else {

print "a is positive\n";

}

# it prints: a is positive

# define an array@colors = qw(blue green brown cyan white);print "@colors\n" unless scalar(@colors) < 5;# prints: blue green brown cyan white

80

switch/case/else Statements

chomp($var=<STDIN>);SWITCH: { $var == 1 && do { print "\$var = 1\n"; last SWITCH; }; $var == 2 && do { print "\$var = 2\n"; last SWITCH; }; $var == 3 && do { print "\$var = 3\n"; last SWITCH; }; print "\$var is not equal with 1 or 2 or 3\n";}

• Perl Switch module was made available beginning with the 5.8.x version of Perl

use Switch;chomp($var=<STDIN>);switch ($var){ case(1) { $i = "One"; } case(2) { $i = "Two"; } case(3) { $i = "Three"; } else { $i = "Other"; }}print "case: $i\n";

81

given/when/default Statements

• In Perl 6 the builtin Perl switch statement is spelled given, and case is pronounced when.

chomp($var=<STDIN>);given ($var){ when(1) { $i = "One"; } when(2) { $i = "Two"; } when(3) { $i = "Three"; } default { $i = "Other"; }}

82

for Statements

• LABEL for(initialization; test; re-initialization) BLOCK– The Perl for statement argument section (enclosed in

parenthesis) is composed of three parts:• initialization – where you can initialize some counter variables; this

part is executed only once at the beginning of the for statement

• test – represents a conditional expression that will be evaluated and if it is true, the block will be executed, otherwise the for loop will be terminated

• re-initialization – in this part the test expression will be reinitialized before the next iteration of the loop statement (like incremented or decremented a test variable in the most simple case)

– The Perl for statement is preceded by a LABEL which can be very useful when you want to alter the normal flow within the block by using the loop controls (next, last or redo).

83

for($i = 5; $i >= -5; $i--) { print "$i "}print "\n";# expected: 5 4 3 2 1 0 -1 -2 -3 -4 -5

print "$_ " for(-5..5);print "\n";

84

Jump StatementOperator Description Example

lastJumps out of the current statement block.

10+20;

nextSkips the rest of the statement block and continues with the next iteration of the loop.

$a++;

redo Restarts the statement block. $a = $b = $c;

goto

Jumps to a specified label. If you write a program that you feel needs a goto in order to run, then use it - but first, try to restructure the program to avoid it.

Control Statements

85

@array = ("A".."Z");for ($index = 0; $index < @array; $index++) { if ($array[$index] eq "T") { last; }}print("$index\n");

for ($index = 0; $index < 10; $index++) { if ($index == 5) { last; } print("loop: index = $index\n");}print("index = $index\n");

86

OUTER_LOOP: for ($index = 0; $index < 10; $index++) { if ($index == 5) { last; } while ($index < 10) { if ($index == 4) { last OUTER_LOOP; } print("inner: index = $index\n"); $index++; } print("outer: index = $index\n");}print("index = $index\n");

87

OUTER_LOOP: for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { print("inner: $row,$col\n"); if ($row == 1) { next OUTER_LOOP; } } print("outer: $row,$col\n\n"); }

@array = (0..9);print("@array\n");for ($index = 0; $index < @array; $index++) { if ($index == 3 || $index == 5) { next; } $array[$index] = "*";}print("@array\n");

88

{ print("What is your name? "); $name = <STDIN>; chop($name); if (! length($name)) { print("Msg: Zero length input. Please try again\n"); redo; } print("Thank you, " . uc($name) . "\n");}

do { print("What is your name? "); $name = <STDIN>; chomp($name);

if (! length($name)) { print("Msg: Zero length input. Please try again\n"); }} until (length($name));print("Thank you, " . uc($name) . "\n");

89

while Statements

• LABEL while (EXPR) BLOCK continue BLOCK• STATEMENT while (EXPR)

– EXPR represents a boolean conditional expression that it will be evaluated before the execution of the block. The Perl while statement will repeatedly execute the block as long as EXPR is true.

– The continue block is executed before the successive reevaluations of the test condition, excepting the case when the main block is exited by the last loop control. It provides a block of statements to be executed after the current iteration is terminated.

90

$a = 10;while ($a > 1) { if ($a == 5) { next; } $a--; print "$a\n";}continue { print "continue: $a\n";}

$k = 0;while(1) { last if $k == 1000; $k++;}print "k = $k\n";

91

# initialize a hash (associative array)%numbers = (1 => 'one ', 2 => 'two ', 3 => ' three', 4 => ' four');while(my $key = each %numbers) { $numbers{$key} = $key * 10;;}continue { print "Key: $key, Value: $numbers{$key}\n";}

$count = 0;print $count, " " while ++$count <= 10;print "\n"; # it displays 1 2 3 4 5 6 7 8 9 10

92

do-while Statements

• do BLOCK while (EXPR);

93

$count = 0;do { print "Password: "; chomp($psw = <STDIN>);} while ++$count < 3 && $psw ne "1qaz"; if($psw eq "1qaz") { print "Password OK ";} else { print "Wrong password\n";}

94

until Statements

• LABEL until (EXPR) BLOCK continue BLOCK

• STATEMENT until (EXPR)– The Perl until looping statement represents

the opposite of the while loop, i.e. it executes repeatedly a block of code until a test condition becomes true, or in other words it executes the block of code as long as the test condition remains false.

95

do-until Statements

• do BLOCK until (EXPR);– a do-until statement is repeatedly executed as long as

the test condition remains false

my $command;do { # ask for the input command print "Command: "; # get the input from STDIN and # chomp off the trailing newline chomp($command = <STDIN>); # convert $command to lowercase lc($command); # convert the first character in uppercase $command = ucfirst $command;}until($command eq "Stop");

96

# initialize a hash (associative array)%birds = (hen => 12, ducks => 15, geese => 5);# put the bird names in an array@birdNames = keys(%birds);

# initialize a count scalar variable$count = 0;do { # get the first element of the array $name = shift(@birdNames); # add to count the number of the current bird $count += $birds{$name};} until (!@birdNames);

# displays the total amount of birdsprint "Total: $count\n";# displays Total: 32

97

foreach Statements

• LABEL foreach VAR (LIST) BLOCK continue BLOCK

• STATEMENT foreach (LIST)

98

my $min = 999;foreach $item (1, 2, 3, 4, 5){ $min = $item if $min > $item;}print "Min = $min\n"; # expects Min = 1

my $max = -999;# initialize an array@numbers = (1, 2, 3, 4, 5);foreach $item (@numbers){ $max = $item if $max < $item;}print "Max = $max\n"; # expects Max = 5

99

# initialize an array@sentence = ("This ", "is ", "a ", "line ", "of ", "text ", "\n");print foreach @sentence;# it prints: This is a line of text

my $sum = 0;@numbers = (1, 2, 3, 4, 5);

foreach (@numbers){ $sum += $_;}print "Sum = $sum\n"; # expects Sum = 15

100

@lines = <DATA>;foreach (@lines) { print("$_");}__END__Line oneLine twoLine three

# define a hash%time = (hour => 12, min => 25, sec => 32, msec =>75);

# foreach loopforeach $key (sort keys %time){ print "$key: $time{$key}\n";}

101

# define a hash%v = (v1 => 75, v2 => 251, v3 => 3, v4 => 12);# sort by value and put the keys in an array @keys = sort {$v{$b} <=> $v{$a}} keys %v;# loop through array to print the hash pairs orderedforeach $key (@keys){ print "$key: $v{$key}\n";}

# define a hash%cds = ("Dr. Hook" => "Sylvias Mother", "Andrea Bocelli" => "Romanza", "Bonnie Tyler" => "Hide your heart"); %cdsReversed = reverse %cds;# we’ll use Data::Dumper module to see # what it is in the hashuse Data::Dumper;print Dumper(%cdsReversed);

102102

Advanced Programming Skill

• Special Variable

• Functions (Subroutine)

• Modules (CPAN)

• Regular Expression

• File Operation

103

Variable Name

Description

Variables That Affect Arrays

$" The separator used between list elements when an array variable is interpolated into a double-quoted string. Normally, its value is a space character.

$[ Holds the base array index. Normally, set to 0.

$; Holds the subscript separator for multi-dimensional array emulation.

Special Variable

104

Variables Used with Files

$. This variable holds the current record or line number of the file handle last read. It is read-only and will be reset to 0 when the file handle is closed.

$/ This variable holds the input record separator. The record separator is usually the newline character. However, if $/ is set to an empty string, two or more newlines in the input file will be treated as one.

$| This variable, if nonzero, will flush the output buffer after every write() or print() function. Normally, it is set to 0.

$^F This variable holds the value of the maximum system file description. Normally, it's set to 2. The use of this variable is beyond the scope of this book.

$ARGV This variable holds the name of the current file being read when using the diamond operator (<>).

_ This file handle (the underscore) can be used when testing files. If used, the information about the last file tested will be used to evaluate the latest test.

105

Variables Used with Files

DATA This file handle refers to any data following __END__.

STDERR This file handle is used to send output to the standard error file. Normally, this is connected to the display, but it can be redirected if needed.

STDIN This file handle is used to read input from the standard input file. Normally, this is connected to the keyboard, but it can be changed.

STDOUT This file handle is used to send output to the standard output file. Normally, this is the display, but it can be changed.

106

Variables Used with Patterns

$& This variable holds the string that was matched by the last successful pattern match.

$` This variable holds the string that preceded whatever was matched by the last successful pattern match.

$' This variable holds the string that followed whatever was matched by the last successful pattern match.

$+ This variable holds the string matched by the last bracket in the last successful pattern match. For example, the statement /Fieldname: (.*)|Fldname: (.*)/ && ($fName = $+); will find the name of a field even if you don't know which of the two possible spellings will be used.

$* This variable changes the interpretation of the ^ and $ pattern anchors. Setting $* to 1 is the same as using the /m option with the regular expression matching and substitution operators. Normally, $* is equal to 0.

$<number> This group of variables ($1, $2, $3, and so on) holds the regular expression pattern memory.

107

Variables Used with Printing

$, This variable is the output separator for the print() function. Normally, this variable is an empty string. However, setting $, to a newline might be useful if you need to print each element in the parameter list on a separate line.

$\ The variable is added as an invisible last element to the parameter list passed to the print() function. Normally, it's an empty string, but if you want to add a newline or some other suffix to everything that is printed, you can assign the suffix to $\.

$# This variable is the default format for printed numbers.

108

Variables Used with Processes

$$ This UNIX-based variable holds the process number of the process running the Perl interpreter.

$? This variable holds the status of the last pipe close, back-quote string, or system() function.

$0 This variable holds the name of the file containing the Perl script being executed.

$] This variable holds a string that identifies which version of Perl you are using. When used in a numeric context, it will be equal to the version number plus the patch level divided by 1000.

$! This variable, when used in a numeric context, holds the current value of errno. If used in a string context, it will hold the error string associated with errno.

$@ This variable holds the syntax error message, if any, from the last eval() function call.

$< This UNIX-based variable holds the read uid of the current process.

109

Variables Used with Processes

$> This UNIX-based variable holds the effective uid of the current process.

$) This UNIX-based variable holds the read gid of the current process. If the process belongs to multiple groups, then $) will hold a string consisting of the group names separated by spaces.

$^T This variable holds the time, in seconds, at which the script begins running.

$^X This variable holds the full path name of the Perl interpreter being used to run the current script.

%ENV This hash variable contains entries for your current environment variables. Changing or adding an entry will affect only the current process or a child process, never the parent process. See the section "Example: Using the %ENV Variable" later in this chapter.

%SIG This hash variable contains entries for signal handlers.

110

Variables Used with Reports

$% This variable holds the current page number for the default file handle. If you use select() to change the default file handle, $% will change to reflect the page number of the newly selected file handle.

$= This variable holds the current page length for the default file handle. Changing the default file handle will change $= to reflect the page length of the new file handle.

$- This variable holds the number of lines left to print for the default file handle. Changing the default file handle will change $- to reflect the number of lines left to print for the new file handle.

$~ This variable holds the name of the default line format for the default file handle. Normally, it is equal to the file handle's name.

111

Variables Used with Reports

$^ This variable holds the name of the default heading format for the default file handle. Normally, it is equal to the file handle's name with _TOP appended to it.

$: This variable holds a string that consists of the characters that can be used to end a word when word-wrapping is performed by the ^ report formatting character. Normally, the string consists of the space, newline, and dash characters.

$^L This variable holds the string used to eject a page for report printing.

112

Miscellaneous Variables

$_ This variable is used as the default parameter for a lot of functions.

$^D This variable holds the current value of the debugging flags.

$^I This variable holds the file extension used to create a backup file for the in-place editing specified by the -i command line option. For example, it could be equal to ".bak."

$^P This variable is an internal flag that the debugger clears so that it will not debug itself.

$^W This variable holds the current value of the -w command line option.

@ARGV This array variable holds a list of the command line arguments. You can use $#ARGV to determine the number of arguments minus one.

113

Miscellaneous Variables

@F This array variable holds the list returned from autosplit mode. Autosplit mode is associated with the -a command line option.

@INC This array variable holds a list of directories where Perl can look for scripts to execute. The list is used mainly by the require statement.

%INC This hash variable has entries for each filename included by do or require statements. The key of the hash entries are the filenames and the values are the paths where the files were found.

114

Function (Subroutine)

• Functions(subroutine) are blocks of codes that are given names so that you can use them as needed. (The difference being that a function returns a value and a subroutine does not. Perl makes no such distinctions.)

• User-defined function• Built-in function

addsum

1 2

3

115

Built-in Perl Functions• The built-in Perl functions are grouped in the following categories:

– Functions for scalar or strings– Regular expressions and pattern matching– Numeric functions– Functions for real @arrays– Functions for list data– Functions for real %hashes– Input/output functions– Functions for fixed length data or record– Functions for filehandles, files or directories– Keywords related to the control flow– Keyword related to scoping– Miscellaneous functions– Functions for processes and process groups– Keywords related to Perl modules– Keywords related to classes– Low level socket functions– Time related functions

116

Perl String Functions (1/3)• We’ll shortly describe below some of the most

frequently used:– chop – removes and returns the last character from a

string– chomp – removes trailing newlines and returns the

number of characters removed– chr - converts ASCII or Unicode values into their

corresponding characters – crypt – allows you to store passwords or other

sensitive data using ASCII characters as encrypted strings

– hex – converts a hex string into the numerical corresponding value

117

Perl String Functions (2/3)• We’ll shortly describe below some of the most

frequently used:– index – returns the position of the first occurrence of a

substring in a string– lc – converts all the characters of a string in

lowercases and returns the new string– lcfirst – converts the first character of a string in

lowercase and returns the new string– length – returns the number of characters in a string– oct – converts an octal string into the numerical

corresponding value– ord – converts a character into its ASCII/Unicode

corresponding value

118

Perl String Functions (3/3)• We’ll shortly describe below some of the most frequently

used:– pack – converts a list into a string, according to a user-defined

template (ex. a binary representation of a list) – reverse – is used to reverse the order of elements in a list– rindex – is similar to index, but returns the position of the last

occurrence of a substring in a string– sprintf – emulates the C sprintf function– substr – extracts a substring from a string and returns it– uc - converts all the characters of a string in uppercases and

returns the new string

119

Modules (CPAN)

• A module is a piece of code that others have already written, code that you can use and integrate in your own Perl script in order to save a lot of time.

• CPAN (Comprehensive Perl Archive Network), is a large collection of Perl software and documentation. – http://www.cpan.org/– http://www.perl.com/CPAN/ – http://www.cpan.org/SITES.html

120

PPM

• PPM is the package management utility for ActivePerl. It simplifies the task of locating, installing, upgrading and removing Perl packages. The PPM client accesses PPM repositories (collections of packages) on the internet or on a local network. It is also used to update previously installed packages with the latest versions and to remove unused packages from your system.

• To launch PPM's graphical user interface, run ppm without any command line arguments:

121

122

Regular Expression

• A regular expression is a pattern describing a certain amount of text

123123

Reference• Perl Tutorial: Start,

– http://www.comp.leeds.ac.uk/Perl/start.html– http://www.cyut.edu.tw/~hcchen/perl/Perl%20tutorial%20Start.ht

m (Big5 Version)

• The perl.ogr Online Library, http://www.perl.org/books/library.html

• Beginning Perl – perl.org http://www.perl.org/books/beginning-perl/

• Perl For CGI tutorialshttp://www.developingwebs.net/perl/

124

• A. D. Marshall, Practical Perl Programming, 1999-2005, http://www.cs.cf.ac.uk/Dave/PERL/perl_caller.html

• Simon Cozens, Ten Perl Myths, February 23, 2000, http://www.perl.com/pub/a/2000/01/10PerlMyths.html

• 簡信昌 , Perl 學習手札全文 , http://perl.hcchien.org/toc.html

• 朱孝國 , Perl 筆記 , http://irw.ncut.edu.tw/peterju/perl.html

• Perl的安全性監測 , http://www.linuxuser.com.tw/skill_detail.php?cid=869

• Programmer's File Editor, http://www.lancs.ac.uk/staff/steveb/cpaap/pfe/default.htm

• The CPAN Search Site, http://search.cpan.org/• Regular Expression Tutorial,

http://www.regular-expressions.info/tutorial.html

124