Академический Документы
Профессиональный Документы
Культура Документы
PERL – Part I
1
Introduction
• What is PERL?
¾Practical Report and Extraction Language.
¾It is an interpreted language optimized for
scanning arbitrary text files, extracting
information from them, and printing
reports based on that information.
¾Very powerful string handling features.
¾Available on all platforms.
Main Advantages
• Speed of development
¾You can enter the program in a text file,
and just run it. It is an interpretive
language; no compiler is needed.
• It is powerful
¾The regular expressions of Perl are
extremely powerful.
¾Uses sophisticated pattern matching
techniques to scan large amounts of data
very quickly.
2
• Portability
¾Perl is a standard language and is
available on all platforms.
¾Free versions are available on the Internet.
• Editing Perl programs
¾No sophisticated editing tool is needed.
¾Any simple text editor like Notepad or vi
will do.
• Flexibility
¾Perl does not limit the size of your data.
¾If memory is available, Perl can handle the
whole file as a single string.
¾Allows one to write simple programs to
perform complex tasks.
3
How to run Perl?
• Recommended steps:
¾Create a directory/folder where you will be
storing the Perl files.
¾Using any text editor, create a file “test.pl”
with the following content:
print “Good day\n”;
print “This is my first Perl program\n”;
4
• On Unix/Linux, an additional line has
to be given at the beginning of every
Perl program.
#!/usr/bin/perl
print “Good day\n”;
print “This is my first Perl program \n”;
Variables
• Scalar variables
¾A scalar variable holds a single value.
¾Other variable types are also available
(array and associative array) – to be
discussed later.
¾A ‘$’ is used before the name of a
variable to indicate that it is a scalar
variable.
$xyz = 20;
5
• Some examples:
$a = 10;
$name=“Indranil Sen Gupta”;
$average = 28.37;
¾Variables do not have any fixed types.
¾Variables can be printed as:
print “My name is $name, the average
temperature is $average\n”;
• Data types:
¾Perl does not specify the types of
variables.
It is a loosely typed language.
Languages like C or java are strongly
typed.
6
Variable Interpolation
• A powerful feature
¾Variable names are automatically
replaced by values when they appear in
double-quoted strings.
• An example:
$stud = “Rupak”;
$marks = 75;
print “Marks obtained by $stud is $marks\n”;
print ‘Marks obtained by $stud is $marks\n’;
¾What do we see:
If we need to do variable interpolation,
use double quotes; otherwise, use
single quotes.
7
• Another example:
$Expense = ‘$100’;
print “The expenditure is $Expense.\n”;
8
• Operations on strings:
¾Concatenation: the dot (.) is used.
$a = “Good”;
$b = “ day”;
$c = “\n”;
$total = $a.$b.$c; # concatenate the strings
9
¾String repetition operator (x).
$a = $b x3;
will concatenate three copies of $b
and assign it to $a.
String as a Number
10
Escaping
$num = 20;
print “Value of \$num is $num\n”;
11
• Another example:
print “<HTML>\n”;
print “<HEAD><TITLE>Test page
</TITLE></HEAD>\n”;
print “<BODY>\n”;
print “<H2>This is a test document.<H2>\n”;
print “</BODY></HTML>”;
12
Lists and Arrays
Basic Difference
13
List Literal
• Examples:
(10, 20, 50, 100)
(‘red', “blue", “green")
(“a", 1, 2, 3, ‘b')
($a, 12)
() # empty list
(10..20) # list constructor function
(‘A’..’Z’) # same, for lettere\s
14
Initializing an Array
• Two ways:
¾Specify values, separated by commas.
@color = (‘red’, ‘green’, “blue”, “black”);
¾Use the quote words (qw) function, that
uses space as the delimiter:
@color = qw (red green blue black);
Array Assignment
15
¾Some other examples:
@xyz = (2..5);
Multiple Assignments
16
Number of Elements in Array
• Two ways:
$size = scalar @colors;
$size = @colors;
Accessing Elements
$first = $list[0];
$fourth = $list[3];
17
• The $# is the index of the last
element of the array.
@value = (1, 2, 3, 4, 5);
18
• Example:
@color = qw (red, blue, green, black);
19
• Example:
@color = qw (red, blue, green, black);
Reversing an Array
20
Printing an Array
• Example:
print @colors;
# prints without spaces – redgreenblue
print “@colors”;
# prints with spaces – red green blue
21
SOLUTIONS TO QUIZ
QUESTIONS ON
LECTURE 20
22
Quiz Solutions on Lecture 20
23
Quiz Solutions on Lecture 20
<html>
<head><title>Login Validation</title></head>
<body>
<% YourName = Request.Form(“name") %>
<% Password = Request.Form(“passwd") %>
</body>
</html>
24
QUIZ QUESTIONS ON
LECTURE 21
25
Quiz Questions on Lecture 21
26