From: Ameya Prakash Usgaonkar
PERL : Practical Extraction & Report Language
Perl is an interpreted language. The interpreter can be invoked from the
command prompt as follows :
$ perl <script file> [arguments]
$ perl -e ' <set of commands> ' [arguments]
where the command line arguments are optional. Alternatively, it can also
be from the file by making the first line of the file as
#! /usr/bin/perl -w
Where the option -w stands for "Warning" mode.
Data types in Perl
------------------
1. Scalars: These are simplest types of data types. This type of data type
is accessed, created or initialised as "$ ".
Scalar data types can be inetegers, reals, strings.
2. Arrays: The array data type in Perl is also called "Lists". That is,
such a data type contains a collection of data values. For
eg., a list can have values such as "Jan, Feb, Mar, Apr, ..."
A list is denoted by the symbol "@". However, individual
elements of the list can be accessed as "$my_list[index]"
where mylist is a list & index refers to the index of data.
3. Hash: This type of data structure is also called "Associative
arrays". They are characterized by the fact that each entry in
the hash has an associated "key" value. A hash is defined by
using the symbol "%"
Files in PERL
-------------
The STDIN STDOUT & STDERR are available to every program of Perl. The
standard input can be accessed as <> or explicitly by saying where
the angle brackets take FILE HANDLE as its parameter.
In the similar way, files can opened using the standard "open" command of
Perl as follows :
open(, "") where if the file of
is opened properly, then is
associated with it. The second parameter in the "open"
call may not always be the file. It can also be any valid
Unix command with its output redirected to a file or some
form of output filter is also accepted.
Command line parameters:
-----------------------
The commad line parameters supplied to the program can be accessed as
follows:
$ARGV[index] where ARGV is a list & index refers to the index of
the parameter in the list ARGV. The size of the list can be accessed as
$#ARGV.
Operators:
----------
Arithmetic operators : +, -, *, /, =
Relational Operators : <, >, !=, <=, >=, <=>
Logical Operators : &&, ||, !, and, or, not
Control structures:
------------------
1. IF stmt.
Syntax : if (condition) {
stmts.; stmts;....;
}
else {
stmts; stmts;.....;
}
2. While looping construct
Syntax : while (condition is true) {
stmts;
stmts; ...... ;
}
3. Until looping construct
Syntaax : until (condition is false) {
stmts;.....;
}
4. For looping construct
Syntax : for(index variable; condition; increment) {
stmt;....stmt;
}
Escape Sequences:
----------------
\b blank / space
\d digit
\D not digit
\s space
\S not space
\w [A-Za-z0-9]
String operations:
-----------------
1. Concatenation
Strings can be concateneted using the ',' operator.
2. Multiplication
Strings can be repeated using the 'x' operator
3. Splitting operation
Strings can be split using the 'split(how to, what) operator,
where 'how to' refers to the way 'what' is to be split.
4. Substitution
The substitute operator is defined as
's///
5. Translation
The translate operation is defined as
tr /[a-z]/A-Z] which converts loweercase to uppercase
-------------------------------------------------------------------------------