1 an introduction to perl. 2 perl: practical extraction and report language developed in 1986 by...

76
1 An Introduction to Perl

Upload: jewel-waters

Post on 05-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

1

An Introduction to Perl

Page 2: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

2

PERL: Practical Extraction and Report Language

Developed in 1986 by Larry Wall Perl is very portable, very friendly to the operating system Perl has gained wide distribution and is readily available. Perl programs syntax are taken from shells scripts, awk and C programs.

Page 3: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

3

PERL: syntax• A bunch of statements and declarations like in a shell

script (no main( ) like in C);

• Each statement ends with a semi-colon (;)

• #!/usr/bin/perl -w (w for warnings) at the beginning of your program.

(like sed and awk) PERL, completely parses and compiles the program into an internal format before executing any of it.

• (unlike sed and awk) PERL executes sequences of statements exactly once.

Page 4: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

4

PERL: Features

• rich collection of built-in operators / functions

• regular expressions capabilities

• no arbitrary limits on – array sizes– length or content of strings

• comes with a symbolic debugger

Page 5: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

5

PERL Features (cont’)• at first used as pattern -scanning and report generating

language• now used for

– system administration– client-server programming– CGI programming for WWW– database access– other tasks

• many modules available: examples– CGI, LWP, DBI, Tk

Page 6: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

6

PERL: Example:#!/usr/bin/perl -w # -w issues `warnings

print “What is your favorite color? “;

$color = <STDIN>;

chomp($color);

if ($color eq ‘blue’) {

print “That is my favorite! \n”;

} elsif ($color =~ /black/) {

print “ I do like $color for my shoes\n”;

} else {

print “$color is a nice choice \n”;

}

Page 7: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

7

Running Perl in UNIX• Explicit invocation of a program

$perl options program arguments

• Using the #! Directive in program$program arguments

• Debugging mode$perl -d program args

• Interactive debugging to test statements$perl -de 0

Page 8: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

8

Perl Data Structures

• Three data structures– scalars

– arrays

– associative arrays, known as “hashes”

• arrays are indexed by numbers, starting with 0

• hashes are indexed by string

Page 9: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

9

Perl Data structures

• Scalars:– $str = “The world is round” ; #string var– $num = 134.99; #numeric val

• Arrays– @students =(“Mike, “Lisa”, “John”);– print $students[0]; #print Mike– $students[3]= “Julie”; #add a new element– @students = (); #empty array

Page 10: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

10

Perl data structures (cont’)

• Hashes– %emp =(“Julie”, “President”, “Mary”, “VP”);– print $emp {“Julie”}; #print

president– print $emp{Julie}– $emp{John} = “controller”;– %emp =(); # empty hash

Page 11: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

11

Scalars: STRINGS and NUMBERS

• Perl supports strings and numbers as the simplest kind of data.

A scalar: is either a number or a string of characters.

A scalar value can be acted upon with operators (like + or concatenation) yielding a scalar result.

Page 12: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

12

Perl data structuresreference: Perl data structures (MIT)

• Scalar values are named with $, even when referring to a scalar that is part of an array

• it works like the English word “the”$days #simple scalar value days

$days[28] #the 29th element of array @days

$days{‘Feb’} # the ‘Feb’ value from hash %days

$#days #the last index of array @days

Page 13: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

13

Perl data structuresreference http://www.mit.edu/perl/perldata.htm

• Entire array and array slice are denoted by ‘@’, which works like “these” or “those”

@days #$days[0], $days[1],…$days[n])

@days[3,4,5] #same as @days[3..5]

@days{‘a’,’c’} #same as ($days{‘a’},$days{‘b’})

Entire hashes are denoted by %%days # (key1, val1, key2, val2 …)

Page 14: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

14

Perl: namespace reference: Perl data structures (MIT)

• Each variable type has its own namespace– you can use, with fear of conflict, same name for a

scalar variable, array, hash, subroutine.

Example: $foo and @foo are two different variables

• Context:– scalar and list

– certain operations return list values in contexts wanting a list, and scalar otherwise

Page 15: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

15

PERL: Operators for numbers

Addition, subtraction, multiplication , division, etc..

For integers, a range .. operator to specify a range of integers.

– Example : print 1. . 9"\n" – 1 2 3 4 5 6 7 8 9

Page 16: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

16

PERL: Operators for strings:

Concatenation operator "."– Example: print "hello" . "world" ;

helloworld

String repetitor operator x– string x num makes as many

concatenated copies of the string as indicated by num

– "fred" x 3 # is "fredfredfred"

Page 17: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

17

PERL: Numeric and String comparison operators

Comparison NumericString

• equal = = eq• not equal != ne• less than < lt• greater than > gt• less than or equal to <= le• greater than or equal to >= ge

Page 18: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

18

PERL:SCALAR VARIABLES Scalar variables are like those of the shell: A scalar variable holds a single scalar value; Variables can be assigned any type of scalar

value and Perl keeps track of the type for you.

$ is always used to denote the variable and also to expand the value of the variable

Page 19: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

19

PERL: Scalar variables(see Perl mit)

• $var = value; = is the assignment operator

• print "$var \n"; prints the value of var

• $car = "buick";• $count = 1;

Page 20: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

20

More on scalars

• Not required to declare or initialize

• pattern matching– if ($var =~ m/pattern/) {….}– if ($var !~ m/pattern/) { …}

• substitution– $var1 =~ s/pattern/replacement/;

Page 21: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

21

FUNCTIONS ON SCALAR VARIABLES chop($var) takes a single argument, a scalar

variable, and removes the last character from the string value of that variable.– $doc = "the Perl document"– chop($doc); $doc is now "the Perl documen"

chomp($var) removes only a newline character at the end if there is one:– $a = "hello world\n";– chomp($a); # is now "hello world"– chomp($a); # nothing happens

Page 22: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

22

<STDIN> as a scalar value• when <STDIN> is used where a scalar

value is expected,• Perl reads the next complete text line from

standard input (up to the first newline) and uses that string as the value of <STDIN>

$a = <STDIN>; # get the text

chomp($a); # get rid of that pesky newline

same as: chomp($a = <STDIN>);

Page 23: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

23

PERL: ARRAYS

A list is an ordered scalar data.

• An array is a variable that holds that list. arrays are dynamically allocated An array is denoted by an "@" sign:

– example: – @arr = (1, 2 , 3, 4, 5);

Page 24: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

24

PERL: ARRAY OPERATORS AND

FUNCTIONS

Assignment:@fred = (1, 2, 3);

@barney = @fred; #copies @fred in @barney

if an array variable is assigned to a scalar variable, the number assigned is the length of the array (i.e., number of elements) – $length = @arr ;

Page 25: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

25

PERL: ARRAY OPERATORS AND FUNCTIONS

access a single element with index number in brackets. Note that the @ on the array name becomes a $ on the element reference

• example:

print $arr[0], "\n"; # prints first element of @arr which is 1

Page 26: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

26

ARRAYS (cont')

undef is returned when accessing an array element beyond the end of the current array.

$#arrname is used to get the index of the last element of array @arrname.

Page 27: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

27

ARRAY FUNCTIONS push and pop functions• when arrays are used a stack of information, where new

values are added to and removed from the right hand side of the list.

• Example:push(@mylist, $newvalue); like

@mylist = (@mylist, $newvalue);

$oldvalue = pop(@mylist);

removes last element of my list.

Page 28: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

28

ARRAY FUNCTIONS (cont')

shift and unshift functions• Perform similar action to push and pop on the "left" side of a list.• Example:

@fred = (5, 6, 7);

unshift(@fred, $a) ; like @fred = ($a, @fred);

$x = shift(@fred); x gets 5,

@fred is now (6, 7)

reverse function

• reverses the order of the elements of its argument, returning the resulting list

• @a = (1, 2, 3);• @b = reverse(@a); # means @b = (3, 2, 1);

Page 29: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

29

ARRAY FUNCTIONS (CONT')

sort function

• returns a sorted list in ascending ASCII order without changing original list

reverse function

• reverses the order of the elements of its argument, returning the resulting list@a = (1, 2, 3);

@b = reverse(@a); # means @b = (3, 2, 1);

Page 30: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

30

ARRAY FUNCTIONS (CONT')

sort function

• returns a sorted list in ascending ASCII order without changing original list

chomp function

• @stuff = ("hello\n", "hi\n", "ola\n");

• chomp(@stuff); # @stuff is now ("hello", "hi", "ola")

Page 31: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

31

Filtering a list

• Grep function may be used to select from list– @toys =qw(car racecar truck bluecar train);– @cars = grep(/car/, @toys);– print “@cars\n”;– car racecar bluecar

• grep from any list– @daemons = grep /daemon/, `ps-ef`;

Page 32: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

32

Iterating through an array

• C-style for loopfor( $I = 0; $I < @myarray; $I++) {

print “$myarray[$I]\n”;

}

• foreach loopforeach $element (@myarray) {

print “$element\n”;

}

Page 33: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

33

<STDIN> as an array

• in a list context, <STDIN> returns all remaining lines up to the end of a file each line is returned as a separate element of the list.

• @a = <STDIN> # read standard input in a list context.

Page 34: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

34

@ARGV Array• @ARGV array contains command line

arguments

• @ARGV does not include program name,

• $0 contains program name

• use in scalar context to determine argument countif (@ARGV !=2) {

die: “usage: $0 infile argument\n”;

}

Page 35: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

35

PERL: HASHES• A hash is a collection of scalar data with

individual elements selected by some index. Index values are arbitrary scalars called keys;

They are used to retrieve values from the array.

Elements of a hash have no special order A hash is denoted by "%" sign Elements of a hash are referenced by

$hashname{key}

Page 36: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

36

HASH: Examples

• Example: %carcolor = ("buick", "yellow");

• $mycolor = $carcolor{ "buick"};

• $mycolor is now "yellow"

• %copy = %original; # copy from %original to %copy

Page 37: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

37

More Hash examples

• Using the => operator– %carcolor = {

buick => ‘yellow’

ge => ‘red’

nissan => ‘blue’

}

Page 38: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

38

HASH FUNCTIONS

• keys functions:

• keys(%hashname) yields a list of the current keys in the hash %hashname.

• Example: keys(%hashname) = keys %hashname;

# once for each key of % fred

foreach $key (keys (%fred)) {

print "at $key we have $fred{$key} \n";

# show key and value

Page 39: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

39

Hash Keys

• Order of keys is unpredictable

• special %ENV hash stores environment variables

• print $ENV{“SHELL”}– SHELL =/bin/csh

Page 40: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

40

HASH FUNCTIONS (CONT')

• values function:

• values(%hashname) yields a list of the current values of %hashname in the same order as keys returned by keys(%hashname)

• %lastname = ("barney", "flinstone", "gerry", "smith");

• @lastname = values(%lastname); #grab the values

Page 41: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

41

HASH FUNCTIONS (each) each function:

• each(%hashname) returns a key-value pair as a two element list.

• Used to iterate over an entire hash (examining every element of ).Example:

while (($first, $last)) = each(%lastname)) {

print "the last name of $first is $last\n";

}

Page 42: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

42

Hash function: delete

• removes hash elements, takes a hash reference as argument

• delete $lastname{"barney"}; #lastname has only one key-value pair now.

Page 43: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

43

Checking hash keys or values

• Checking for a true/false valueif ($emp{John}) { …}

• checking for an exsiting key

if (exists $emp{john}) {…}

• checking for an existing key and a defined valueif (defined $emp{John}) {..}

Page 44: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

44

Checking hash keys or values (cont’)

• Undefining a valueundef $emp{john};

• deleting a key-value pair from hashdelete $emp{john}

• checking to see if hash contains anythingif (%emp) {..}

Page 45: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

45

CONTROL STRUCTURES Perl supports "if", "for" while" similar than

those in C. "foreach" constructs is from the C shell• foreach example:

– If the list we are iterating over is made of real variables rather than some functions returning a list value, then the variable being used for iteration is in fact an alias for each variable in the list instead of being a merely copy of the values

Page 46: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

46

Foreach example

• Example:

@numbers = ( 3, 5, 7, 9)

foreach $one (@numbers ) {

$one*=3;

}

# @numbers is now (9,15,21,27)

Page 47: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

47

BASIC I/O Input from STDIN• Perl uses the variable $_ to contain the line read

from STDIN.• $a = <STDIN>; #reads the next line• @a = <STDIN>; # reads all lines until control ^D• typically

while (defined ($line = <STDIN>) {

# process $line here

}

when no more lines read. <STDIN> returns undef.

Page 48: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

48

using the diamond operator <>

<> operates like <STDIN>, but gets data from file or files

• specified on the command line that invoked the PERL program.

• <> looks at the @ARGV array#!/usr/bin/perl

while (<>) {

print $_;

}

Page 49: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

49

Output to STDOUT

• print for normal output

• printf for formatted output

Page 50: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

50

REGULAR EXPRESSIONS PERL supports the same regular expressions

as in SED. =~ match operator• It takes a regular expression operator on the right side

and changes the target of the operator to some value.• The target of the =~ operator can be any expression

that yields some scalar string value.

Example: if (<STDIN> = ~ /^[yY]/) {

print" what can I do for you? ";

Page 51: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

51

Regular expressions: split function

• split function takes a regular expression and a string, and looks for all occurrences of the regular expression within that string.

• Parts of the string that don't match the regular expression are returned in sequence as a list of values

Page 52: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

52

Example of split function

$line = "merlyn::118:120:Randal:/home/merlyn:/usr/bin/perl";

@fields = split(/:/, $line); # split $line, using : as delimiter

# now @fields is ("merlyn,"", "118", "120", "Randal",

# "/home/merlyn", "/usr/bin/perl")

Page 53: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

53

join function

• takes a list of values and glues them together with a glue string between each list element.

• Example: $outline = join(":", @fields);

Page 54: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

54

PERL :FUNCTIONS• Defining a user function

sub subname {

Statement_1;

Statement_2;

Statement_3;

} return value is the value of the return statement

or of the last expression evaluated in the subroutine.

Page 55: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

55

Calling a function/subroutine

• $var = myroutine( paramters);– or

• @array = myroutine( parameters);

Page 56: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

56

Function Arguments

• Subroutine invocation is followed by a list within parenthesis

• Causing the list to be automatically assigned to a special variable named @_ for the duration of the subroutine.

• $_[0] is the 1st element of the @_ array

• @_ variable is private to the subroutine.

Page 57: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

57

Function private variables

• using the my operatorsub add {

my ($sum); #make $sum a local variable

foreach $_ ( @_) {

$sum += $_; # add each element

}

return $sum #last expression evaluated

}

Page 58: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

58

Functions: Semi-private variables

semi-private variables using local

• local variables are visible to functions called from within the block in which those variables are declared.

Page 59: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

59

Requiring Variable Declarations

• Programmer may choose to require declarations – use strict or– use strict ‘vars’

• compiler will be strict in requiring that– variable must be declared private with my– otherwise, variable must be qualified with

package names

Page 60: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

60

Functions:arguments

• Passing arrays$score = average(@maths, @english, @french);

• passing references to arrays

• passing hashes– %checks = payroll(%emp);

• passing references to hashes

Page 61: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

61

Signal handling• Special hash %SIG used for signal handling• hash keys are the signal names

– example: SIGCHLD, HUP

• hash values are the signal decisions– ignoring signals:

$SIG{INT} = ‘IGNORE’;– resetting the default

$SIG{INT} = ‘DEFAULT’;– catching signals

$SIG{INT} = \&mycatch;

Page 62: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

62

FILEHANDLES

• Recommendation use all uppercase letters in your filehandles .

• 3 files handles,

• STDIN, for standard in

• STDOUT, for standard out

• STDERR for standard error.

Page 63: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

63

Filehandles (cont’)

• Opening a file for reading:open(FILEHANDLE, "filename");

• die: equivalent to "open that file or die.”• opening a file for writing (> or >> for append)open(FILEHANDLE,”>filename") | |

die "Sorry could not create filename\n";

Page 64: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

64

FileHandles (cont’)

• Writing to a file (note, no comma after file handle)print FILEHANDLE “hello world\n”;

• PERL provides -op file tests just like the shells

Page 65: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

65

Executing Unix commands from a PERL script

• To capture command output, use command substitution:– capturing output in a scalar:

$var = `who`;– capturing output in an array

@output = `who`;

• to execute command, without capturing output,use system

$status = system(“who”);

Page 66: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

66

Reading Directory files(references: Perl CookBook)

• Use opendir to set up a directory handle$dir =“/d/d1/test”;– opendir DH, $dir or die “cannot open $dir: $!”;

• use readdir in scalar context to read next entry• while (defined($filename = readdir DH)) {print $filename\n”;

}

Page 67: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

67

Reading Directory files(cont’) (references: Perl CookBook)

• use readdir in list context to read all entries

@files= readdir DH;• other directory routinesclosedir DH close directory handle

rewinddir DH #reposition to start

Page 68: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

68

USER DATABASE MANIPULATION

• Most UNIX systems have a standard library called DBM, which allows programs to store a collection of key-value pairs into a pair of disk files.

• In Perl, a hash may be associated with a DBM through a process similar to opening a file.

• dbmopen function associates a DBM database with a DBM array:

Page 69: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

69

Database interface example

dbmopen(%ARRAYNAME, "dbmfilename", $mode);

dbmopen(%FRED, ."mydatabase", 0644);

delete $FRED{"barney"}

while (($key, $value) = each(%FRED)) {

print "$key has value of $value\n";

}

Page 70: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

70

Perl debugging• perl -d• h print out a help message• T stack trace• s insgle step• n next• f finish• c continue• q quit• D delete all breakpoints

Page 71: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

71

SYSTEM CALLS

• Perl provides an interface to many UNIX system calls.

• Interface is via Perl functions, not directly through the system call library.

• The interface use is dependent on the implementation and version of Perl being used.

Page 72: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

72

Perl Modules(references: PERL Cookbook)

• Perl modules may export symbols to your namespace

• must use use to import these symbols– use CGI;

• may request only certain symbols be imported:– use CGI qw($ONE $TWO);

Page 73: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

73

Steps to making a Module(references: PERL Cookbook)

• Module names should not be all lower case

• module, file and package names should match

• module name filename package statement

• Modabc Modbc.pm package Modabc;

Page 74: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

74

Steps to making a module: (references: PERL Cookbook)

• Most modules should load in the standard Exporter module, and then create three special arrays:use Exporter;

@ISA = qw(….); #names of base classes

@EXPORT = qw(…); #default symbols to export

@EXPORT_OK =qw(…) #symbols to export on request

Page 75: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

75

Steps to making a module (references: PERL Cookbook)

• Next, comes any module initialization code

• function definitions

• file needs to return some true value to indicate success1; #last statement in file

Page 76: 1 An Introduction to Perl. 2 PERL: Practical Extraction and Report Language  Developed in 1986 by Larry Wall  Perl is very portable, very friendly to

76

End of Lecture