An Introduction to Perl

76
1 An Introduction to Perl

description

An Introduction to Perl. PERL: P ractical E xtraction and R eport L anguage. 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. - PowerPoint PPT Presentation

Transcript of An Introduction to Perl

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 the operating system Perl has gained wide distribution and is readily available. Perl programs syntax are taken from shells scripts, awk and C programs.

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.

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

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

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”;

}

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

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

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

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

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.

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

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 …)

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

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

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"

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

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

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;

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/;

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

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>);

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);

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 ;

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

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.

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.

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);

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);

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")

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`;

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”;

}

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.

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”;

}

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}

36

HASH: Examples

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

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

• $mycolor is now "yellow"

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

37

More Hash examples

• Using the => operator– %carcolor = {

buick => ‘yellow’

ge => ‘red’

nissan => ‘blue’

}

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

39

Hash Keys

• Order of keys is unpredictable

• special %ENV hash stores environment variables

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

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

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";

}

42

Hash function: delete

• removes hash elements, takes a hash reference as argument

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

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}) {..}

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) {..}

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

46

Foreach example

• Example:

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

foreach $one (@numbers ) {

$one*=3;

}

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

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.

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 $_;

}

49

Output to STDOUT

• print for normal output

• printf for formatted output

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? ";

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

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")

53

join function

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

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

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.

55

Calling a function/subroutine

• $var = myroutine( paramters);– or

• @array = myroutine( parameters);

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.

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

}

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.

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

60

Functions:arguments

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

• passing references to arrays

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

• passing references to hashes

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;

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.

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";

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

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”);

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”;

}

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

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:

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";

}

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

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.

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);

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;

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

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

76

End of Lecture