Perl

24
Perl Practical Extraction Report Language

description

PERL

Transcript of Perl

Page 1: Perl

Perl

Practical Extraction Report Language

Page 2: Perl

• 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.

Page 3: Perl

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.

Page 4: Perl

• 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 statement hello1.pl:print("Hello World\n");

TO execute, perl hello1.pl

Page 5: Perl

Comments in Your Program

• Comments are placed inside a program file using the # character.

• Everything after the # is ignored. For example comment.pl:

# This whole line is ignored. print("Perl is easy.\n"); # Here's a half-line comment.

Page 6: Perl

PERL Strings

• String Literals are 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 (`).

Page 7: Perl

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.

Page 8: Perl

Double-Quoted Strings

• One major difference between double- and single-quoted strings is that double-quoted strings have some special escape sequences that 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.

Page 9: Perl

Back-Quoted Strings

• It might 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`;

Page 10: Perl

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 items by name.

Page 11: Perl

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 type per se. Perl regards all numbers a floating point numbers for calculations etc.

Page 12: Perl

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;

Page 13: Perl

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 \n are regarded as being two characters \ and n with 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:

Page 14: Perl

• \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

Page 15: Perl

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.

Page 16: Perl

What is an Array?

• Perl uses arrays-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 have mixed elements, for example

• (1,"fred", 3.5) is perfectly valid.

Page 17: Perl

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)

Page 18: Perl

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 of array1 being 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 of array2 to ``first'' element

of array1.• Each array element is a scalar so we reference each array element wth $.

Page 19: Perl

• Array indexing starts from 0 in 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

Page 20: Perl

Useful Array Functions

• push() and pop()• One common use of an array is as a stack.• push() and pop() 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

Page 21: Perl

• shift() and unshift()• Like push() and pop() except put values on and

take values off the left side of an array.The shift() operation 'pops' the element from the left side of the array and unshift() 'pushes' to the left side of an array.

Page 22: Perl

Associative Arrays

• Associative arrays are a very useful and commonly used feature of Perl.

• Associative arrays basically store tables of information where the lookup is the right hand key (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.

Page 23: Perl

• 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 new keys. E.g.

• $lookup{"adam"} = 3845; You do new assignments to old keys also:

• # change dave's code $lookup{"dave"} = 7634;

Page 24: Perl

Associative Array Operators• keys()

The keys(%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); delete deletes an associated key and value by key reference, e.g.# scrub adam from code list delete $lookup("adam");