Perl Training
Perl Training
1 Introduction
• Regular Expressions
Unlike shell and C, Perl has built-in regular expression
A regex is a way of describing a set of strings without
having to list all the strings in your set
1
Introduction To Perl Programming
1.3.2 Interpreter
• The first line contains the character sequence #!
followed by the full pathname of the Perl interpreter
#!/usr/intel/bin/perl
1.3.3 Comments
• Uses # for comments
print("hello,world\n");
2
Introduction To Perl Programming
LAB 1
1. Using a text editor such as vi or emacs, input the line of
statement below and then save the file as hello.pl
print("hello,world\n");
#!/usr/intel/bin/perl
3
Introduction To Perl Programming
2 Literals
2.1 Numbers
• Numerical values can be specified in 4 types
▪ Decimal –> Decimal number is base 10
Example: 20
▪ Hexadecimal – > Data prefixed with 0x is base 16
Example: 0x14
▪ Octal –> Data prefixed with a single 0 is base 8
Example: 024
▪ Scientific –> Values in the form of xE+y
Example: 2E1
2.2 Strings
• Strings are stored in either single or double quotes
$x = "hello";
$x = hello;
$x = "hello";
print "$x,world\n"; # display: hello,world
print $x,world\n; # display: $x,world\n
4
Introduction To Perl Programming
++ Increment Operator
-- Decrement Operator
** Exponentiation
% Modulus (3%2 returns 1, the remainder)
* / Multiplication and Division
+ - Addition and Subtraction
5
Introduction To Perl Programming
3 Scalar Data
3.1 General
• All references to scalar variables (both assigning and
retrieving) are prefixed with the $ character
• Can be a number, string of characters or boolean
• Default to undef, null string or 0
print $name;
6
Introduction To Perl Programming
$scalar = "12345";
chop($scalar); # scalar is 1234
7
Introduction To Perl Programming
4 Basic I/O
4.1 STDIN
• Read in the next line from standard input
$name = chop($name);
print "Hello $name, $lastchar";
chop($name);
print "Hello $name";
chop($name);
print "Hello $name";
$name = <STDIN>;
$name = <>;
8
Introduction To Perl Programming
LAB 2
1. Write a program that reads a string and a number, and
prints the string the numbers of times indicated by the
number on separated lines.
(Hint: use the "x" operator)
Expected output:
$> lab2.pl
Enter string: test
Enter times: 3
test
test
test
9
Introduction To Perl Programming
5 Arrays
5.1 Definition
• Arrays are groups of scalar variables, with each
member indexed by a number
• Arrays can have any number of elements
• Elements can store anything from a number, a string to
a pointer to another array
• Denoted by initial character “ @“
@name = (martin,alan,dave,andy);
@number = (1, 2, 3, 4, 5, 6);
@newname = (mike,@name);# newname is (mike,
#martin, alan, dave,
#andy)
10
Introduction To Perl Programming
push(ARRAY,LIST)
appends LIST to the end of ARRAY
pop(ARRAY)
removes the last element of ARRAY, returns element pop off
Examples:
11
Introduction To Perl Programming
@name = (martin,alan,dave,andy);
@sortName = sort(@name);
# @sortName is alan, andy, dave, martin
12
Introduction To Perl Programming
LAB 3
1. Write a program that read 3 different strings and store
in into an array list.
(Hint: use <STDIN>)
print @ARRAY;
print “@ARRAY”;
print @ARRAY.””;
print $#ARRAY;
print $”;
Note:
srand; #initialize the random generator
rand(@array); #return a random value between 0 and
#one less than the length of @array
13
Introduction To Perl Programming
6 Control Structures
if (test_expression) {
statements;
...
}
• Type II
if (test_expression) {
statements;
...
} else {
statements;
...
}
• Type III
if (test_expression) {
statement;
...
} elsif (test_expression) {
statement;
...
} else {
statement;
...
}
14
Introduction To Perl Programming
while (test_expression) {
statement;
...
}
15
Introduction To Perl Programming
while (test_expression) {
statement1;
if (condition) {
statement2;
last;
}
statement3;
}
# last statement reach here
while (test_expression) {
statement1;
if (condition) {
statement2;
next;
}
statement3;
# next statement reach here
}
16
Introduction To Perl Programming
while (test_expression) {
# redo statement come here
statement1;
if (condition) {
statement2;
redo;
}
statement3;
}
17
Introduction To Perl Programming
LAB 4
1. Write a program that prints a table of numbers, x, and
their squares, x 2, from 0 to 16 using while loop.
18
Introduction To Perl Programming
7 Associative Arrays
7.1 Introduction
• Associative arrays also called hash
• Very similar to array, except that instead of being
indexed by a number with a particular order, they are
indexed by a string
• Denoted by “%”
• Each element of the array is a separate scalar variable,
accessed by the scalar index called the key
%hash = ();
$hash{"a"}{fruit} = "apple";
$hash{"a"}{people} = "boy";
19
Introduction To Perl Programming
20
Introduction To Perl Programming
close (FILEHANDLE);
21
Introduction To Perl Programming
• die() operator
used to display a list and exits the
program when a error occurred
22
Introduction To Perl Programming
LAB 5
1. Write a program that reads a series of words with one
word per line until end of file. The print a summary of
how many times each word was seen.
(Hint: use word as key, counter as value)
23
Introduction To Perl Programming
9 Directory Access
9.1 Basic
• To change the current directory of a process
chdir ("/etc");# change to the /etc directory
chdir (); # change to home directory
24
Introduction To Perl Programming
25
Introduction To Perl Programming
$now = time;
utime ($now, $now, "file1"); # touch file1
26
Introduction To Perl Programming
LAB 6
1. Write a program that works like ‘mv’, renaming the first
command-line argument to the second command-line
argument.
(Hint: use @ARGV to get argument)
27
Introduction To Perl Programming
11 Regular Expressions
11.1 Definition
• Regex is a pattern to be matched against a string
• $_ is matched by default
• =~ used to match variable strings other than $_
11.2 Patterns
• Any single character matches itself
/bob/ # matches word "bob"
• The "." matches any character
/b.b/ # matches word "bxb", "beb"…
• The "*" matches 0 or more of the preceeding character
/b.*b/ # matches word "bb", "bxb", "baaab" …
• The [ ] matches a single character within the bracket
/[acz098]/ # matches any single digit
28
Introduction To Perl Programming
11.3 Usage
• Search and replace
s/PATTERNS/REPLACEMENT/;
s/a/b; # match a and replace it with b
• Example:
$_ = "thiiiiiiiis x a test";
s/i+/i/; # $_ = "this x a test"
s/x/is/; # $_ = "this is a test"
$_ = "red:blue:green";
($color1,$color2,$color3) = split(/:/);
($color1,$rest) = split(/:/,$_,2);
# $rest = blue:green
29
Introduction To Perl Programming
LAB 7
1. Write a program that read the file /etc/passwd, printing
the login name and real name of each user.
(Hint: use split to break up the line up into fields, use
s/// to get rid of the parts of the comment field that are
after the first comma.)
30
Introduction To Perl Programming
12 Functions
12.1 Definition
• Functions (also called subroutines) are logical sections
of code that can be called repeatedly
• A subroutine is defined using the sub keyword, as in
sub subroutine_name {
statement1;
...
}
sub twice
{
($x) = @_;
return ($x * 2);
}
31
Introduction To Perl Programming
sub square
{
local($x);
($x) = @_;
return ($x * $x);
}
32
Introduction To Perl Programming
LAB 8
1. Write a subroutine that calculate the factorial function,
x! , in math. factor(5) = 5 x 4 x 3 x 2 x 1 = 120;
(Hint: Think recursively…)
33