Anda di halaman 1dari 24

Perl

Practical Extraction Report


Language

Perl is a programming language which can


be used for a large variety of tasks.
A typical simple use of Perl would be for
extracting information from a text file and
printing out a report or for converting a text
file into another form.
Perl is implemented as an interpreted (not
compiled) language.

Writing Perl Programs


To check Perl Installation, perl v
First, you create a text file to hold the
Perl program.
Then you run or execute the Perl
program file.

Leading spaces on a line are ignored. You


can start a Perl statement anywhere you
want: at the beginning of the line.
Statements are terminated with a
semicolon.
Anything after a hash sign (#) is ignored
except in strings. Use this fact to pepper
your code with useful comments.
Here's Our first uninspired Perl
statementhello1.pl:
print("Hello World\n");
TO execute, perl hello1.pl

Comments in Your Program


Comments are placed inside a
program file using the # character.
Everything after the # is ignored. For
examplecomment.pl:
# This whole line is ignored.
print("Perl is easy.\n");
# Here's a half-line comment.

PERL Strings
String Literalsare groups of
characters surrounded by quotes so
that they can be used as a single
data.
They are frequently used in programs
to identify filenames, display
messages, and prompt for input.
In Perl you can use single quotes
('), double quotes("), and back
quotes (`).

Single-Quoted Strings
A single-quoted string is pretty simple. Just
surround the text that you'd like to use with
single quotes,
E.g. Hello
But what if you wanted to use a single
quote inside the literal?
Perl uses the backslash (\) character to
indicate that the normal function of the
single quote-ending a literal-should be
ignored for a moment.

Double-Quoted Strings
Onemajor differencebetween
double- and single-quoted strings is
that double-quoted strings have
some specialescape sequencesthat
can be used.
Escape sequences represent
characters that are not easily
entered using the keyboard or that
are difficult to see inside an editor
window.

Back-Quoted Strings
Itmight be argued that back-quoted strings are
not really a data type.
That's because 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.
Let's see how to use the back-quoted string to
display a directory listing of all text files in the
perl5 directory:
print `dir *.txt`;

Variables
Perl has three types of variables:
Scalar-- denoted by a$symbol
prefix. A scalar variable can be either
a number or a string.
Array-- denoted by a@symbol
prefix. Arrays are indexed by
numbers.
Associative Array-- denoted by
a%symbol prefix. Arrays are
indexed by strings. You can look up

Scalar Variables
Scalar variables can be either a
number or a string
You can use variable types almost
interchangeably. Numbers first then
strings later
Numbers are numbers -- there is no
integer typeper se. Perl regards all
numbers a floating point numbers for
calculationsetc.

Defining Scalar Variables


You define scalar variables by assigning a value
(number or string) to it.
It is a good idea to declare all variables together
near the top of the program.
The following are simple examples (var1.pl) of
variable declarations:
$first_name = "David";
$last_name = "Marshall";
$number = 3;
$another_number = 1.25;
$sci_number = 7.25e25;

String Scalar Variables


Strings are a sequence of characters. Perl has two types
of string:
Single-quoted strings-- denoted by '....'. All
characters are regarded as being literal characters.
That is to say special format characters like\nare
regarded as being two characters\andnwith no special
meaning.

Two exceptions:To get a single-quote character do\'


To get a backslash character do\\
Double-quoted strings-- Special format characters
now have a special meaning.Some special format
characters include:

\n newline
\r carriage return
\t tab
\b backspace
\\ backslash character
\" double-quote character
\l lower case next letter
\L lower case all letters until \E
\u upper case next letter
\U upper case all letters until \E
\E Terminate \L or \E

Arrays
We can also have two forms of array:
1. Arrays - An array is a series of
numbers and strings handled as a
unit. You can also think of an array
as a list.
2. Associative Arrays - This is the most
complicated data type. Think of it as
a list in which every value has an
associated lookup item.

What is an Array?
Perl usesarrays-or lists-to store a series of
items. You could use an array to hold all of the
lines in a file, to help sort a list of addresses, or
to store a variety of items.
An array, in Perl, is an ordered list of scalar
data.
Each element of an array is an separate scalar
variable with a independent scalar value Arrays
can therefore havemixedelements, for
example
(1,"fred", 3.5) is perfectly valid.

Literal Arrays
Arrays can be defined literally in Perl
code by simply enclosing the array
elements in parentheses and
separating each array element with a
comma.
For example
(1, 2, 3) ("fred", "albert") () # empty
array (zero elements) (1..5) #
shorthand for (1,2,3,4,5)

Indexed Arrays
You declare an ordinary indexed array by giving it a name and prefixing it
with a@
Values are assigned to arrays in the usual fashion:
@array = (1,2,3);
@copy_array = @array;
@one_element = 1;
for example:
@array1 = (4,5,6);
@array2 = (1,2,3, @array1, 7,8); results in the elements
ofarray1being inserted in the appropriate parts of the list.
Therefore after the above operations
@array2 = (1,2,3,4,5,6,7,8) This means Lists cannot contain other lists
elements only scalars allowed.
Elements of arrays are indexed by index:
$array1[1] = $array2[3]; Assign ``third'' element ofarray2to ``first''
element ofarray1.
Each array element is a scalar so we reference each array element wth$.

Array indexing starts from0in Perl So


@array = (1,2,3,4,5,6,7,8);
The index$array[0]refers to 1
and$array[5]refers to 6.
If you assign a scalar to an array the scalar
gets assigned the length of the array,i.e:
@array2 = (1,2,3,4,5,6,7,8);
$length = @array2; # length = 8
$length = $array2[3]; # length gets ``third''
value # in array i.e 4

Useful Array Functions


push()andpop()
One common use of an array is as a
stack.
push()andpop()add or remove an
item from the right hand side of an
array.
push(@mystack,$newvalue); # add
new value to stack
$off_value = pop(@mystack); # take
last element off array

shift()andunshift()
Likepush()andpop()except put
values on and take values off the left
side of an array.
Theshift()operation 'pops' the
element from the left side of the
array andunshift()'pushes' to the
left side of an array.

Associative Arrays
Associative arrays are a very useful and
commonly used feature of Perl.
Associative arrays basically storetablesof
information where the lookup is the right
handkey(usually a string) to an associated
scalar value. Again scalar values can be mixed
``types''.
Associative arrays are denoted by a verb| When
you declare an associative array the key and
associated values are listed in consecutive
pairs.

We would declare a Perl associative array to


perform this lookup as follows:
%lookup = ("dave", 1234, "peter", 3456,
"andrew", 6789); The reference a particular
value you do:
$lookup{"dave"} You can create new
elements by assignments to newkeys.E.g.
$lookup{"adam"} = 3845; You do new
assignments to old keys also:
# change dave's code $lookup{"dave"} =
7634;

Associative Array Operators


keys()
Thekeys(%arrayname)lists all the key names in a
specified associative array. The answer is returned as an
ordinary index array.
E.g.
@names = keys(%lookup);
values()This operator returns the values of the
specified associative array.
E.g.
@codes = values(%lookup);
deletedeletes an associated key and value by key
reference,
e.g.
# scrub adam from code list delete $lookup("adam");

Anda mungkin juga menyukai