Short Perl tutorial Adapted from slides by Rada Mihalcea.

43
Short Perl tutorial Adapted from slides by Rada Mihalcea
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    1

Transcript of Short Perl tutorial Adapted from slides by Rada Mihalcea.

Page 1: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Short Perl tutorial

Adapted from slides by Rada Mihalcea

Page 2: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 2

About Perl: Practical Extraction and Report Language1987

Larry Wall Develops PERL

1989October 18 Perl 3.0 is

released under the GNU Protection License

1991March 21 Perl 4.0 is

released under the GPL and the new Perl Artistic License

Now Perl 6

PERL is not officially a Programming Language per se.

Wall’s original intent was to develop a scripting language more powerful than Unix Shell Scripting, but not as tedious as C.

PERL is an interpreted language. That means that there is no explicitly separate compilation step.

Rather, the processor reads the whole file, converts it to an internal form and executes it immediately.

Page 3: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 3

Can you say Cryptic?

Page 4: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 4

Variables

A variable is a name of a place where some information is stored. For example:

$yearOfBirth = 1976; $currentYear = 2000; $age = $currentYear-$yearOfBirth; print $age; Same name can store strings: $yearOfBirth = ‘None of your business’;

The variables in the example program can be identified as such because their names start with a dollar ($). Perl uses different prefix characters for structure names in programs. Here is an overview:

• $: variable containing scalar values such as a number or a string • @: variable containing a list with numeric keys • %: variable containing a list with strings as keys • &: subroutine

Page 5: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 5

Operations on numbers

Perl contains the following arithmetic operators: • +: sum • -: subtraction • *: product • /: division • %: modulo division • **: exponent

Apart from these operators, Perl contains some built-in arithmetic functions. Some of these are mentioned in the following list:

• abs($x): absolute value • int($x): integer part • rand(): random number between 0 and 1 • sqrt($x): square root

Page 6: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 6

Input and output

print "Please enter your birth year "; $yearOfBirth = <>; chomp($yearOfBirth); print "Your age is ",2004-$yearOfBirth,".\n"; $yearOfBirth = ‘none of your business’; # note no typing

print "What file do you want to open?";$myfile = <>;

# count the number of lines in a fileopen INPUTFILE, "<$myfile";chomp($myfile); #without the chomp, the \n is part of the filename

# count the number of lines in a fileopen (INPUTFILE, “<$myfile”);(-r INPUTFILE) || die “Could not open the file $myfile\n”;

$count = 0;

while($line = <INPUTFILE>) { $count++;}

print “$count lines in file $myfile\n”;

# open for writingopen OUTPUTFILE, “>$myfile”;

Page 7: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 7

Conditional structures

# determine whether number is odd or even#notice the elsif (not a typo)#<> reads a value from a file

print "Enter number: "; $number = <>; chomp($number); if ($number%2 == 0) { print "$number is even\n"; } elsif (abs($number%2) == 1) { print "$number is odd\n"; } else { print "Something strange has happened!\n"; }

Page 8: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 8

Running the program

On a Unix system, after you've entered and saved the program, make sure the file is executable by using the command

 chmod u+x progname at the UNIX prompt, where progname is the filename of the program.

This is not required for our Windows version of Perl.  Now to run the program, just type any of the following at the prompt.

perl progname  // This is sufficient for our Windows version of Perl.

./progname

progname

If something goes wrong then you may get error messages, or you may get nothing. You can always run the program with warnings using the command

 perl -w progname at the prompt.

This will display warnings and helpful messages before it tries to execute the program. To run the program with a debugger use the command

 perl -d progname  

Use the help menu to find out what to do next

Page 9: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 9

Numeric test operators

An overview of the numeric test operators: • ==: equal • !=: not equal • <: less than • <=: less than or equal to • >: greater than • >=: greater than or equal to

Truth expressions

three logical operators: • and: and (alternative: &&) • or: or (alternative: ||) • not: not (alternative: !)

Page 10: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 10

Iterative structures

#print numbers 1-10 in three different ways $i = 1; while ($i<=10) {print "$i\n"; $i++; }

for ($i=1;$i<=10;$i++) { print "$i\n"; }

foreach $i (1,2,13,8,19,100) { print "$i\n"; }

Stop a loop, or force continuation:last; # C breaknext; # C continue;

Exercise: Read ten numbers, print the largest and a count representing how many of them are divisable by three.

Watch typos – as variables need not be declared

Page 11: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 11

while($i < 10){ $i++; $number = <>;

#without the chomp the carriage return is part of $number

# this doesn’t affect the math, but does affect the printing

chomp($number);

if (not (defined($largest)) or $number > $largest) { $largest = $number; }

if ($number%3==0) { $count3++; } }#note interpolation inside double quotes!print "largest is $largest and those divisible by

three are $count3\n";

Page 12: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 12

PERL philosophy(ies)

There is more than one way to do it; TIMTOWTDI

If you want to shoot yourself in the foot, who am I to stop you?

DO write comments in your Perl programs!

Personal comment: some of books are terrible!

See my tutorial on the web (Vicki Allan cs4700)

for an easy intro

Page 13: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 13

Basic string operations

- strings are stored in the same variables we used for storing numbers

- string values can be specified between double or single quotes

Comparison operators for strings eq: equal - ne: not equal - lt: less than - le: less than or equal to - gt: greater than - ge: greater than or equal to

Examples:

if ($a eq $b) { ….}

Page 14: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 14

String operations

$a = $b . $c;  # Concatenate $b and $c$a = $b x $c;  # $b repeated $c times

To assign values Perl includes  $a = $b;       # Assign $b to $a$a += $b;      # Add $b to $a$a -= $b;      # Subtract $b from $a$a .= $b;      # Append $b onto $a

Note that when Perl assigns a value with $a = $b, it does a deep copy:

it makes a copy of $b and then assigns that to $a.

Therefore the next time you change $b it will not alter $a.

Page 15: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 15

String substitution and string matching

The power of Perl! If you like vi, you will like pattern matching!

The s/// operator modifies sequences of characters SubstituteThe tr/// operator changes individual characters. TranslationThe m// operator checks for matching (or in short //) Matching

- the first part between the first two slashes contains a search pattern - the second part between the final two slashes contains the

replacement.- behind final slash are characters to modify the behavior of the

commands.

By default s/// only replaces the first occurrence of the search pattern - append a g to the operator to replace every occurrence. - append an i to the operator, to have the search case insensitive The tr/// operator allows the modification characters - c (replace the complement of the search class, anything not in first spec is replaced by second, uses last character of string)- s (squeeze sequences of identical replaced characters to one

character)

Page 16: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 16

Perl's regex often look like this:

$name=~/piper/

That is saying "If 'piper' is inside $name, then True."

=~ assigns target of search

$try = 'What do you think of this? HUHHHHHHHHHH?';

$try =~ s/H/M/ig; # replaces all Hh’s with Mprint "results in $try\n";

if ($try =~ /you/) {print "Found you in $try";}

Page 17: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 17

Test your understanding

$text =~ s/bug/feature/;

$text =~ s/bug/feature/g;

$text =~ tr/[A-Z]/[a-z]/;

$text =~ tr/AEIOUaeiou//d;

$text =~ tr/[0-9]/x/cs;

$text =~ s/[A-Z]/CAPS/g;

Page 18: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 18

Examples

# replace first occurrence of "bug" $text =~ s/bug/feature/;

# replace all occurrences of "bug" $text =~ s/bug/feature/g;

# convert to lower case $text =~ tr/[A-Z]/[a-z]/;

# delete vowels $text =~ tr/AEIOUaeiou//d;

# replace nonnumber sequences with a single x $text =~ tr/[0-9]/x/cs;

# replace each capital character by CAPS $text =~ s/[A-Z]/CAPS/g;

Page 19: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 19

Regular expressions

• \b: word boundaries • \d: digits • \n: newline • \r: carriage return • \s: white space characters • \t: tab • \w: alphanumeric characters • ^: beginning of string • $: end of string • .: any character • [bdkp]: characters b, d, k and p • [a-f]: characters a to f • [^a-f]: all characters except a to f • abc|def: string abc or string def

• *: zero or more times • +: one or more times • ?: zero or one time • {p,q}: at least p times and at most q times • {p,}: at least p times • {p}: exactly p times

Examples:1. Clean an HTML formatted text

2. Grab URLs from a Web page

3. Transform all lines from a file into lower case

Page 20: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 20

Lists and arrays

@a = (); # empty list

@b = (1,2,3); # three numbers

@c = ("Jan","Piet","Marie"); # three strings

@d = ("Dirk",1.92,46,"20-03-1977"); # a mixed list

Variables and sublists are interpolated in a list@b = ($a,$a+1,$a+2); # variable interpolation @c = ("Jan",("Piet","Marie")); # list interpolation @d = ("Dirk",1.92,46,(),"20-03-1977"); # empty list # don’t get lists containing lists – just a simple

list@e = ( @b, @c ); # same as

(1,2,3,"Jan","Piet","Marie")

Page 21: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 21

Lists and arrays

Practical construction operators•($x..$y) @x = (1..6); # same as (1, 2, 3, 4, 5, 6)

@z = (2..5,8,11..13); # same as (2,3,4,5,8,11,12,13)

•qw() "quote word" function

qw(Jan Piet Marie) is a shorter notation for ("Jan","Piet","Marie").

Page 22: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 22

Split

It takes a regular expression and a string, and splits the string into a list, breaking it into pieces at places where the regular expression matches. 

$string = "Jan Piet\nMarie \tDirk";@list = split /\s+/, $string; # yields ( "Jan","Piet","Marie","Dirk" ) # remember \s is a white space

$string = " Jan Piet\nMarie \tDirk\n"; # empty string at begin and end!!!@list = split /\s+/, $string; # yields ( "", "Jan","Piet","Marie","Dirk", "" )

$string = "Jan:Piet;Marie---Dirk"; # use any regular expression... @list = split /[:;]|---/, $string; # yields ( "Jan","Piet","Marie","Dirk" )

$string = "Jan Piet"; # use an empty regular expression to split on letters @letters= split //, $string; # yields ( "J","a","n"," ","P","i","e","t")

Page 23: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 23

More about arrays

@array = ("an","bert","cindy","dirk");

$length = @array; # $length now has the value 4

print $length; # prints 4

print $#array; # prints 3, last valid subscript

print $array[$#array] # prints "dirk"

print scalar(@array) # prints 4

Page 24: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 24

Working with lists

Subscripts convert lists to strings@array = ("an","bert","cindy","dirk"); print "The array contains $array[0] $array[1] $array[2] $array[3]";

# interpolateprint "The array contains @array";

function join STRING LIST. $string = join ":", @array; # $string now has the value "an:bert:cindy:dirk"

Iteration over listsfor( $i=0 ; $i<=$#array; $i++){ $item = $array[$i]; $item =~ tr/a-z/A-Z/; print "$item "; }

foreach $item (@array){ $item =~ tr/a-z/A-Z/; print "$item "; # prints a capitalized version of each item }

Page 25: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 25

More about arrays – multiple value assignments

($a, $b) = ("one","two"); ($onething, @manythings) = (1,2,3,4,5,6) # now $onething equals 1 # and @manythings = (2,3,4,5,6) ($array[0],$array[1]) = ($array[1],$array[0]); # swap the first two

Pay attention to the fact that assignment to a variable first evaluates the right hand-side of the expression, and then makes a copy of the result

@array = ("an","bert","cindy","dirk"); @copyarray = @array; # makes a deep copy $copyarray[2] = "XXXXX";

Page 26: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 26

Manipulating lists and their elementsPUSH•push ARRAY LIST

appends the list to the end of the array. if the second argument is a scalar rather than a

list, it appends it as the last item of the array.

@array = ("an","bert","cindy","dirk"); @brray = ("eve","frank");

push @array, @brray; # @array is

("an","bert","cindy","dirk","eve","frank")

push @brray, "gerben"; # @brray is ("eve","frank","gerben")

Page 27: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 27

Manipulating lists and their elementsPOP•pop ARRAY does the opposite of push. it removes

the last item of its argument list and returns it. •If the list is empty it returns undef. @array = ("an","bert","cindy","dirk"); $item = pop @array;

# $item is "dirk" and @array is ( "an","bert","cindy")

•shift @array removes the first element - works on the left end of the list, but is otherwise the same as pop.

•unshift (@array, @newStuff) puts stuff on the left side of the list, just as push does for the right side.

Page 28: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 28

Grep

•grep CONDITION LIST

returns a list of all items from list that satisfy some condition.

For example:

@large = grep $_ > 10, (1,2,4,8,16,25); # returns (16,25)

@i_names = grep /i/, @array; # returns

("cindy","dirk")

Page 29: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 29

map

•map OPERATION LIST is an extension of grep, and performs an

arbitrary operation on each element of a list.

For example:

@array = ("an","bert","cindy","dirk"); @more = map $_ + 3, (1,2,4,8,16,25); # returns (4,5,7,11,19,28)

@initials = map substr($_,0,1), @array; # returns ("a","b","c","d")

Page 30: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 30

Hashes (Associative Arrays)

- associate keys with values – named with %

- allows for almost instantaneous lookup of a value that is associated with some particular key

Examplesif %wordfrequency is the hash table,$wordfrequency{"the"} = 12731; # creates key "the", value 12731$phonenumber{"An De Wilde"} = "+31-20-6777871"; $index{$word} = $nwords; $occurrences{$a}++; # if this is the first reference, # the value associated with $a will # be increased from 0 to 1

Page 31: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 31

Hash Operations

%birthdays = ("An","25-02-1975","Bert","12-10-1953","Cindy","23-05-1969","Dirk","01-04-1961");

# fill the hash

%birthdays = (An => "25-02-1975", Bert => "12-10-1953", Cindy => "23-05-1969", Dirk => "01-04-1961" );

# fill the hash; the same as above, but more explicit

@list = %birthdays; # make a list of the key/value pairs

%copy_of_bdays = %birthdays; # copy a hash

Page 32: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 32

Hashes (What if not there?)

Existing, Defined and true.

- If the value for a key does not exist in the hash, the access to it returns the undef value.

- special test function exists(HASHENTRY), which returns true if the hash key exists in the hash

- if($hash{$key}){...}, or if(defined($hash{$key})){...} return false if the key $key has no associated value

print "Exists\n" if exists $array{$key};

Page 33: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 33

Operations on Hashes

- keys HASH returns a list with only the keys in the hash. As with any list, using it in a scalar context returns the number of keys in that list.

- values HASH returns a list with only the values in the hash, in the same order as the keys returned by keys.

foreach $key (sort keys %hash ){ push @sortedlist, ($key , $hash{$key} ); print "Key $key has value $hash{$key}\n"; }

Page 34: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 34

Operations on Hashes

reverse the direction of the mapping, i.e. construct a hash with keys and values swapped:

%backwards = reverse %forward; (if %forward has two identical values associated with different keys,

those will end up as only a single element in %backwards)

- hash slice – assign to a section specified by name@birthdays{"An","Bert","Cindy","Dirk"} = ("25-02-1975","12-10-

1953","23-05-1969","01-04-1961");

- each( HASH ) – traverse a hash while (($name,$date) = each(%birthdays)) { print "$name's birthday is $date\n"; }

# alternative: foreach $key (keys %birthdays)…

Page 35: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 35

Want an interesting use of a hash slice?

The simplest is when you have two arrays of values and you want a hash to convert a value from one array to another.

@foo_array = qw( abc def ghi ) ;

@bar_array = qw( jkl mno pqr ) ; @foo_to_bar{ @foo_array } = @bar_array

Now you can easily convert from foo values to bar values like this:

$foo_value = 'def' ; $bar_value = $foo_to_bar{ $foo_value } ; $bar_value now is 'mno'

You can even convert a whole array of foo values in one statement: @bar_values = @foo_to_bar{ @foo_values } ;

Page 36: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 36

Basics about Subroutines

Calls to subroutines can be recognized because subroutine names often start with the special character &.

sub askForInput { print "Please enter something: "; }

# function call &askForInput();

Page 37: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 37

Variables Scope

A variable $a is used both in the subroutine and in the main part program of the program.

$a = 60; print "$a\n";

sub changeA { $a = 1; }

print "$a\n"; &changeA(); print "$a\n";

The value of $a is printed three times. Can you guess what values are printed?

- $a is a global variable (so all references are to same variable).

Page 38: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 38

Variables Scope

Hide variables from the rest of the program using “my”.

my $a = 60; print "$a\n"; sub changeA { my $a = 1; } print "$a\n"; &changeA(); print "$a\n";

What values are printed now?

Page 39: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 39

Parameters

&doSomething(2,"a",$abc); - Perl converts all arguments to a flat list. This

means that &doSomething((2,"a"),$abc) will result in the same list of arguments as the earlier example.

Access the argument values inside the procedure with the special list @_.

E.g. my($number, $letter, $string) = @_; # reads the parameters from @_

- A tricky problem is passing two or more lists as arguments of a subroutine. &sub(@a,@b) the subroutine receives the two lists as one big one and will be unable to determine where the first ends and where the second starts.

Page 40: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 40

- Subroutines also use a list as output.

# the return statement from a subroutine return(1,2); # or simply (1,2)

# get the return values from the subroutine ($a,$b) = &subr().

- Read the main program arguments using $ARGC and @ARGV (same as in C)

Page 41: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 41

Just Try it!

Page 42: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 42

More about file management

• open(INFILE,"myfile"): reading • open(OUTFILE,">myfile"): writing • open(OUTFILE,">>myfile"): appending • open(INFILE,"someprogram |"): reading from program • open(OUTFILE,"| someprogram"): writing to program • opendir(DIR,"mydirectory"): open directo

Operations on an open file handle• $a = <INFILE>: read a line from INFILE into $a • @a = <INFILE>: read all lines from INFILE into @a • $a = readdir(DIR): read a filename from DIR into $a • @a = readdir(DIR): read all filenames from DIR into @a • read(INFILE,$a,$length): read $length characters from INFILE

into $a • print OUTFILE "text": write some text in OUTFILE

Close files / directories• close(FILE): close a file • closedir(DIR): close a directory

Page 43: Short Perl tutorial Adapted from slides by Rada Mihalcea.

Slide 43

Other file management commands

• binmode(HANDLE): change file mode from text to binary • unlink("myfile"): delete file myfile • rename("file1","file2"): change name of file file1 to file2 • mkdir("mydir"): create directory mydir • rmdir("mydir"): delete directory mydir • chdir("mydir"): change the current directory to mydir • system("command"): execute command command • die("message"): exit program with message message • warn("message"): warn user about problem message

Exampleopen(INFILE,"myfile") or die("cannot open myfile!");

Other

About $_

Holds the content of the current variable Examples:while(<INFILE>) # $_ contains the current line readforeach (@array) # $_ contains the current element in @array