Getting Started With Perl Jonathan Worthington Scarborough Linux - - PowerPoint PPT Presentation

getting started with perl
SMART_READER_LITE
LIVE PREVIEW

Getting Started With Perl Jonathan Worthington Scarborough Linux - - PowerPoint PPT Presentation

Getting Started With Perl Jonathan Worthington Scarborough Linux User Group Getting Started With Perl What is Perl? A programming language Created by Larry Wall Perl 1 released in 1987 Current version is Perl 5.8.x Perl 6


slide-1
SLIDE 1

Getting Started With Perl

Jonathan Worthington Scarborough Linux User Group

slide-2
SLIDE 2

Getting Started With Perl

What is Perl?

A programming language Created by Larry Wall Perl 1 released in 1987 Current version is Perl 5.8.x Perl 6 under construction, looks very

exciting.

Probably shipped as part of every

Linux distribution.

slide-3
SLIDE 3

Getting Started With Perl

Where is Perl used?

Server side web programming (CGI

scripts, mod_perl Apache module)

System administration Bioinformatics Natural language processing Lots of situations where you want to

glue together things that don’t naturally fit.

slide-4
SLIDE 4

Getting Started With Perl

What does Perl mean? Practical Extraction (and) Reporting Language

slide-5
SLIDE 5

Getting Started With Perl

What does Perl mean? Practical

Focus on getting the job done without

getting in the programmer’s way

Language features map well to real

world tasks. Extraction (and) Reporting Language

slide-6
SLIDE 6

Getting Started With Perl

What does Perl mean? Practical Extraction (and)

Makes it easy to get hold of the data

we want to do stuff with.

Perl’s regexes are extremely powerful

and fast – engine widely used. Reporting Language

slide-7
SLIDE 7

Getting Started With Perl

What does Perl mean? Practical Extraction (and) Reporting

Easy to produce output from data in

the format we need to.

High performance file I/O, string

iterpolation, more later… Language

slide-8
SLIDE 8

Getting Started With Perl

What does Perl mean? Practical Extraction (and) Reporting Language

Designed by a linguist, borrowing from

natural languages so Perl feels natural to write.

Possible to write hard to read code.

slide-9
SLIDE 9

Getting Started With Perl

What does a Perl program look like?

  • “Shebang” gives path to Perl interpreter

Semicolon ends each statement Strings go in double quotes, \n means “new line”. “print” will send output to stdout by default.

slide-10
SLIDE 10

Getting Started With Perl

Scalars

A scalar is a container that can hold

  • ne thing.

When talking about a container holding

a single item of data, we use “$”.

The “S” in “$” is to remind you of the

“S” in “scalar”.

42 Jonathan 3.14159265

slide-11
SLIDE 11

Getting Started With Perl

Assignment

Assignment is about putting an item in

a container with a name.

Written with the “=“ sign. Do not think of this like equality in

maths – you’ll get horribly confused!

  • !"##$%&$

!"##$%&$ !"##$%&$ !"##$%&$

slide-12
SLIDE 12

Getting Started With Perl

Using scalars in output

Our previous “Hello world!” program

could be re-written as follows.

  • '

' ' '

  • '

' ' ' Placing a scalar where a string or number could be written will cause the value held in the scalar to be used in that place. Set the scalar $message to contain “Hello world!\n”

slide-13
SLIDE 13

Getting Started With Perl

Interpolation

Putting a scalar in the middle of some

  • utput should not be this painful:

So strings inside doubles quotes will

“interpolate” scalars, so we can do this:

() () () ()

  • ()

() () ()

slide-14
SLIDE 14

Getting Started With Perl

Interpolation

Interpolation can sometimes get in the

way.

Single quotes will not interpolate. Other characters that become special

with interpolation are “@”, “%” and “\”.

Be careful to use single quotes for

email addresses!

* * * *+ + + +, , , ,+ + + +"- "- "- "-

slide-15
SLIDE 15

Getting Started With Perl

Arithmetic

Works just as you would expect.

#. #. #. #. $ $ $ $ / / / /

  • 1

1 1 1 2 2 2 2 // // // //

  • 00

00 00 00

  • Addition; $c

will hold 15 Multiplication; $e will hold 50 Subtraction; $d will hold 5 Division; $f will hold 2 Short for $a = $a + 1; Short for $b = $b – 1;

slide-16
SLIDE 16

Getting Started With Perl

Getting input

We can read data in a line at a time

using the diamond operator.

For example, <STDIN> will read lines

from standard input.

Using it in an assignment to a scalar

will read a line and store the contents

  • f the line in the scalar.

345678 345678 345678 345678

slide-17
SLIDE 17

Getting Started With Perl

The greeting program

  • 9:);9

9:);9 9:);9 9:);9 345678 345678 345678 345678

  • 9

9 9 9

  • 9

9 9 9 We didn’t put a \n on this line so the cursor for input is left after this prompt. Looks nicer somehow. A line ends with a new line character and the diamond operator doesn’t strip it. chomp removes a new line character from the end if there is one. We read the users name into $name.

slide-18
SLIDE 18

Getting Started With Perl

  • loops

Looping is about doing the same thing

multiple times.

Often also called iteration. A while loop basically says “while some

condition C is true, do X.

In Perl, a while loop is written like this:

<=> <=> <=> <=> 22 22 22 22 ? ? ? ?

slide-19
SLIDE 19

Getting Started With Perl

Looping over all lines of input

Often we want to do something for

each line piped to standard in.

We can do this with the following loop: This works because when there is no

more to read, $line will be undefined.

An undefined value is always false.

<345678=> <345678=> <345678=> <345678=>

  • 022

22 22 22 ? ? ? ?

slide-20
SLIDE 20

Getting Started With Perl

The line counting program (“

  • ”)
  • .

. . . <345678=> <345678=> <345678=> <345678=> // // // // ? ? ? ?

  • $lines will store the number of lines read so far.

As we do not care about the contents

  • f the lines, we just want to count

them, we don’t assign what is read to a scalar. (Assigned to $_ - more later). Increment the counter for each line.

slide-21
SLIDE 21

Getting Started With Perl

Writing less code

We can do the same with less code. Need a careful trade-off between being

brief and being clear.

  • //38

//38 //38 //38

  • It is possible, when you are

doing a single thing per line, to use the post-fix form of while. Neater here – don’t overuse it. When the diamond

  • perator is empty,

it uses STDIN by default.

slide-22
SLIDE 22

Getting Started With Perl

Arrays

The second type of container in Perl. Contains many items indexed by the

integers starting from 0.

Think of slots numbered 0, 1, 2, 3… Written with the “@”, to remind you of

the “a” in “array”.

1 2 3 4 5 6 7

slide-23
SLIDE 23

Getting Started With Perl

Assigning to arrays

Can assign lots of values to an array at

  • nce.

The same could be achieved by

assigning to each element of the array.

Note use of $ rather than @ here, as

we are talking about a single value.

,<*7-@*A-@*4-= ,<*7-@*A-@*4-= ,<*7-@*A-@*4-= ,<*7-@*A-@*4-= B.C*7- B.C*7- B.C*7- B.C*7- B#C*A- B#C*A- B#C*A- B#C*A- BC*4- BC*4- BC*4- BC*4-

slide-24
SLIDE 24

Getting Started With Perl

Getting values from an array

We could reference each element by

its index, but it’s a lot of work.

If we do this, there’s no advantage over

using 3 scalars.

What makes arrays powerful is that we

can iterate (loop) over the values stored in them – even without knowing how many there are.

B.C B.C B.C B.C

  • B#C

B#C B#C B#C

  • BC

BC BC BC

slide-25
SLIDE 25

Getting Started With Perl

2 2 2 2 loops

Allow us to take each of the values in

an array (in order) and do something with that value.

A foreach loop looks like this: The code in the braces is executed

  • nce for each element in @array, the

current value being stored in $element.

2 2 2 2 <,)=> <,)=> <,)=> <,)=> 5' 5' 5' 5' ? ? ? ?

slide-26
SLIDE 26

Getting Started With Perl

A simple array example

  • ,<@#$@%@#=

,<@#$@%@#= ,<@#$@%@#= ,<@#$@%@#= . . . . 2 2 2 2<,=> <,=> <,=> <,=> / / / / ? ? ? ? ; ; ; ;

  • Assign some numbers to the array @numbers.

We add each element to the

  • total. Note that “$total += $n;” is

short for “$total = $total + $n;”. Each element of @numbers is placed in $n

slide-27
SLIDE 27

Getting Started With Perl

  • Often need to read input separated by

a certain character or characters.

For example, tab delimited files or

  • utput from “ps” where we have data in

columns separated by spaces.

The function allows a line of

input to be split up into an array each time a certain character or sequence of characters is encountered.

slide-28
SLIDE 28

Getting Started With Perl

  • example

We’ll take the output produced by

running “ps –ef” and total the times that processes have been running for.

The output of “ps –ef” looks like this: Need to ignore line 1 and get time from

column 7.

D65E65EE65F46(:G6(:F(5 D65E65EE65F46(:G6(:F(5 D65E65EE65F46(:G6(:F(5 D65E65EE65F46(:G6(:F(5 #.. .H..;..;$ #.. .H..;..;$ #.. .H..;..;$ #.. .H..;..;$ #. .H..;.#;.&B #. .H..;.#;.&B #. .H..;.#;.&B #. .H..;.#;.&BIJ IJ IJ IJC C C C !#. .H..;..;.B !#. .H..;..;.B !#. .H..;..;.B !#. .H..;..;.BI I I IC C C C """ """ """ """

slide-29
SLIDE 29

Getting Started With Perl

  • example, step 1

Start off with a simple skeleton script

that gives us a place to store the total and loops through the lines of input.

  • .6

.6 .6 .6 <345678=> <345678=> <345678=> <345678=> ? ? ? ? 9; 9; 9; 9;

  • 9

9 9 9

slide-30
SLIDE 30

Getting Started With Perl

  • example, step 2

Add logic to skip first line.

  • .6

.6 .6 .6 K. K. K. K. <345678=> <345678=> <345678=> <345678=> 7I#" 7I#" 7I#" 7I#" K// K// K// K// L2K# L2K# L2K# L2K# ? ? ? ? 9; 9; 9; 9;

  • 9

9 9 9 Skips to next line == means “is equal to”

slide-31
SLIDE 31

Getting Started With Perl

  • example, step 3

Using split, extract 7th column… …and the parts of the time.

M;((;442" M;((;442" M;((;442" M;((;442" ,2< ,2< ,2< ,2<

  • /@=

/@= /@= /@= )2B&C )2B&C )2B&C )2B&C “\s” means white space, “+” means one or more character FJ" FJ" FJ" FJ" <@ <@ <@ <@

  • @

@ @ @

  • =<;@=

=<;@= =<;@= =<;@= / / / /

  • /

/ / /

  • 1&./

1&./ 1&./ 1&./ 1!&.. 1!&.. 1!&.. 1!&..

slide-32
SLIDE 32

Getting Started With Perl

  • example in full
  • ).6

).6 ).6 ).6 )K. )K. )K. )K. <345678=> <345678=> <345678=> <345678=> 7I#" 7I#" 7I#" 7I#" K// K// K// K// L2K# L2K# L2K# L2K# M;((;442" M;((;442" M;((;442" M;((;442" ,2< ,2< ,2< ,2<

  • /@=

/@= /@= /@= )2B&C )2B&C )2B&C )2B&C FJ" FJ" FJ" FJ" <@ <@ <@ <@

  • @

@ @ @

  • =<;@=

=<;@= =<;@= =<;@= / / / /

  • /

/ / /

  • 1&./1!&..

1&./1!&.. 1&./1!&.. 1&./1!&.. ? ? ? ? 9; 9; 9; 9;

  • 9

9 9 9

slide-33
SLIDE 33

Getting Started With Perl

Hashes

The third type of container in Perl. Like an array, contains many values. Each value is indexed by a key, but

this time it can be any scalar.

Visualize it like a table. Written with a “%”.

Key 1 Value 1 Key 2 Value 2

slide-34
SLIDE 34

Getting Started With Perl

Assigning to hashes

We put entries into a hash specifying

the key and value.

We can add a single value to a hash –

note the use of $ for a single entry.

N'< N'< N'< N'< 78%$.@ 78%$.@ 78%$.@ 78%$.@ A8#O$@ A8#O$@ A8#O$@ A8#O$@ 48#O 48#O 48#O 48#O = = = = '>*6-?#P. '>*6-?#P. '>*6-?#P. '>*6-?#P.

slide-35
SLIDE 35

Getting Started With Perl

Getting values from a hash

Accessing a single value: It is possible to iterate over the keys in

the hash and print each key/value pair.

Can also iterate over just the values.

6J'>*6-? 6J'>*6-? 6J'>*6-? 6J'>*6-?

  • 2

2 2 2 <I)N'=> <I)N'=> <I)N'=> <I)N'=> J'>? J'>? J'>? J'>?

  • ?

? ? ? 2 2 2 2 '<JN'=> '<JN'=> '<JN'=> '<JN'=>

""" """ """ """

? ? ? ?

slide-36
SLIDE 36

Getting Started With Perl

Reading from files

First, open the file. Use diamond operator to read lines. When finished with the file, close it.

  • 2

2 2 2@32"L @32"L @32"L @32"L scalar stores file handle < means “read” <3 <3 <3 <32 2 2 28=> 8=> 8=> 8=> """ """ """ """ ? ? ? ?

  • 2

2 2 2

slide-37
SLIDE 37

Getting Started With Perl

Writing to files

First, open the file to write. Can use >> for append in place of >. Use print to write data to the file. When finished with the file, close it.

  • 2

2 2 2@82"L @82"L @82"L @82"L > means “write, replacing anything in the file”

  • 2

2 2 222 22 22 22

  • 2

2 2 2

slide-38
SLIDE 38

Getting Started With Perl

Regexes

One of the scarier looking things you’ll

find in Perl code.

Immensely powerful for extracting and

validating input.

Think of it in terms of pattern

matching.

A regex describes a pattern. We test if some text matches it or not.

slide-39
SLIDE 39

Getting Started With Perl

Regexes

Patterns are (usually) written between

slashes.

=~ operator attempts to match a

pattern to a scalar, giving true or false.

Alphabetic characters and numbers

match themselves.

2<LQ'=> 2<LQ'=> 2<LQ'=> 2<LQ'=> L' L' L' L'

  • ?

? ? ?

slide-40
SLIDE 40

Getting Started With Perl

Regexes – quantifiers

Quantifiers state how many of a

certain character is required.

? 0 or 1

+ 1 or more * 0 or more

Example: pattern H/

xxabxx matches xaxbbxx matches xxaxx doesn’t match

slide-41
SLIDE 41

Getting Started With Perl

Regexes – character classes

Used when any one of a set of

characters will do.

Written in square brackets For example, /[aeiou]/ will match a

string containing any vowel.

Can also provide ranges; to match any

lowercase letter use [a-z].

To put “-” in a character class, use “\-”.

slide-42
SLIDE 42

Getting Started With Perl

Regexes – built-in character classes

There are a number of character

classes already defined.

.

Matches any character but newline \w Matches any alphanumeric \d Matches any digit \s Matches white space \W Matches any non-alphanumeric \D Matches any non-digit \S Matches non-white space

slide-43
SLIDE 43

Getting Started With Perl

Word frequency example

The goal is to write a program that:

  • Reads in a text file.
  • Counts the number of times each

word appears in the file, being case insensitive.

  • Writes a file containing each word in

the input file, sorted alphabetically, and the number of times it appears.

slide-44
SLIDE 44

Getting Started With Perl

Word frequency example

We will use a hash to store the word

counts, the hash keys being the words and the values being the counters.

R222" R222" R222" R222"

  • 2

2 2 2@93L"L9 @93L"L9 @93L"L9 @93L"L9 N N N N

  • <=

<= <= <= <3 <3 <3 <32 2 2 28=> 8=> 8=> 8=> M M M M 62" 62" 62" 62" ? ? ? ?

  • 2

2 2 2

slide-45
SLIDE 45

Getting Started With Perl

Word frequency example

Extract words using split. Loop over words and update counters.

  • ,<BS

,<BS ,<BS ,<BS

  • T

T T T

  • 0C/@=

C/@= C/@= C/@= ^ at start of character class means “anything but…”. Letters, hyphens, and apostrophes. 2 2 2 2 <,=> <,=> <,=> <,=>

  • >

> > >

  • <=?//

<=?// <=?// <=?// ? ? ? ? lc function lowercases a string

slide-46
SLIDE 46

Getting Started With Perl

Word frequency example

Finally, need to write results file - a

sorted list of words and their counts.

U2" U2" U2" U2"

  • 2

2 2 2@98"L9 @98"L9 @98"L9 @98"L9 2 2 2 2 <I)N <I)N <I)N <I)N

  • =>

=> => =>

  • 2

2 2 2 9 9 9 9

  • >?

>? >? >?

  • 9

9 9 9 ? ? ? ?

  • 2

2 2 2

  • The sort keyword will sort

the elements in an array into ascending order. \t means “tab”

slide-47
SLIDE 47

Getting Started With Perl

Word frequency example

R222" R222" R222" R222"

  • 2

2 2 2@93L"L9 @93L"L9 @93L"L9 @93L"L9 N N N N

  • <=

<= <= <= <3 <3 <3 <32 2 2 28=> 8=> 8=> 8=> M" M" M" M"

  • ,<BS

,<BS ,<BS ,<BS

  • T

T T T

  • 0C/@=

C/@= C/@= C/@= 62" 62" 62" 62" 2 2 2 2 <,=> <,=> <,=> <,=>

  • >

> > >

  • <=?//

<=?// <=?// <=?// ? ? ? ? ? ? ? ?

  • 2

2 2 2

  • U2"

U2" U2" U2"

  • 2

2 2 2@98"L9 @98"L9 @98"L9 @98"L9 2 2 2 2 <I)N <I)N <I)N <I)N

  • =>

=> => =>

  • 2

2 2 2 9 9 9 9

  • >?

>? >? >?

  • 9

9 9 9 ? ? ? ?

  • 2

2 2 2

slide-48
SLIDE 48

Getting Started With Perl

Command line Perl

Can write “one-liners” on the

command line.

Use the -e flag and put the script in

quotes afterwards.

Here’s the line counting script written

as a “one-liner”.

  • 0*-

*- *- *-

  • 0*L//38L

*L//38L *L//38L *L//38L

slide-49
SLIDE 49

Getting Started With Perl

Command line Perl

Specifying the -n flag will enclose the

script in a “while (<>) { … }” loop.

Note that when 38 is not assigned to a

scalar, the value is placed in the default scalar K.

This script extracts the second white

space delimited column of each line.

  • *,L

*,L *,L *,L

  • /9LB#C

/9LB#C /9LB#C /9LB#C

  • 9-

9- 9- 9- split function’s second parameter defaults to $_

slide-50
SLIDE 50

Getting Started With Perl

CPAN

Comprehensive Perl Archive Network A mass of Perl modules, generally well

documented and ready to use, that do a very wide range of tasks.

Web interface: http://search.cpan.org/ Perl comes with a command line

module installer.

  • 0(FEA7

(FEA7 (FEA7 (FEA70 0*- *- *- *- 8(;;7 8(;;7 8(;;7 8(;;7

slide-51
SLIDE 51

Getting Started With Perl

CPAN Example

We want to pipe a list of filenames of

MP3s to the script.

The script should extract the artist

from each MP3 file – the hard part.

It should then produce a list of all

artists that we have songs by, sorted in order of the number of songs we have by that artist.

slide-52
SLIDE 52

Getting Started With Perl

CPAN Example

Goto CPAN web interface, search for

  • MP3. The top result looks promising.
slide-53
SLIDE 53

Getting Started With Perl

CPAN Example

Each module has a synopsis with the

most common use cases.

slide-54
SLIDE 54

Getting Started With Perl

CPAN Example

In this case, we need not read any

further – the synopsis has all we need to know to extract the artist!

slide-55
SLIDE 55

Getting Started With Perl

CPAN Example

Run CPAN installer (as root). The first

time, it needs to be configured.

B B B B+ + + +, , , ,V+ V+ V+ V+C C C C

  • E;

E; E; E; B, B, B, B,V+ V+ V+ V+C C C C

  • 0(FEA7

(FEA7 (FEA7 (FEA70 0TT TT TT TT

  • $$"P"!FEA7

$$"P"!FEA7 $$"P"!FEA7 $$"P"!FEA7F2' F2' F2' F2'" " " " V V V V" " " " FEA7 FEA7 FEA7 FEA70 0J2 J2 J2 J2

  • "62

"62 "62 "62 #..' #..' #..' #..'" " " " ()JFEA7)" ()JFEA7)" ()JFEA7)" ()JFEA7)"

  • 2FEA7)FEA7""62

2FEA7)FEA7""62 2FEA7)FEA7""62 2FEA7)FEA7""62) ) ) ) FEA7"@)J2')" FEA7"@)J2')" FEA7"@)J2')" FEA7"@)J2')" 62)'@)TT 62)'@)TT 62)'@)TT 62)'@)TT

  • W6T)

W6T) W6T) W6T) 2' 2' 2' 2'"<7;)J "<7;)J "<7;)J "<7;)J ')))'T2T ')))'T2T ')))'T2T ')))'T2T

  • "=

"= "= "= A))22'HB)C A))22'HB)C A))22'HB)C A))22'HB)C

slide-56
SLIDE 56

Getting Started With Perl

CPAN Example

Once it’s configured itself (usually

works) we can install the module.

  • 8

8 8 8(E!;;' (E!;;' (E!;;' (E!;;' """ """ """ """ FI'2)I""" FI'2)I""" FI'2)I""" FI'2)I""" XI' XI' XI' XI' U' U' U' U'(I2 (I2 (I2 (I2 2(E!;;' 2(E!;;' 2(E!;;' 2(E!;;' 'F55RKY" 'F55RKY" 'F55RKY" 'F55RKY"

  • (E!'F55RKY"

(E!'F55RKY" (E!'F55RKY" (E!'F55RKY" """ """ """ """

  • I

I I I00 00 00 00 Z[ Z[ Z[ Z[ \'I \'I \'I \'I """ """ """ """ A2" A2" A2" A2" Y$@%!@! Y$@%!@! Y$@%!@! Y$@%!@!I I I I <"! <"! <"! <"!

  • /."!%

/."!% /."!% /."!% ) ) ) ) "O#FED= "O#FED= "O#FED= "O#FED=

  • I

I I I] ] ] ] Z[ Z[ Z[ Z[ \'I \'I \'I \'I """ """ """ """

  • I

I I I00 00 00 00 Z[ Z[ Z[ Z[

slide-57
SLIDE 57

Getting Started With Perl

CPAN Example

Now we can start to write our script…

  • (E!;;'

(E!;;' (E!;;' (E!;;' F')" F')" F')" F')" N<= N<= N<= N<= <2345678=> <2345678=> <2345678=> <2345678=> 2 2 2 2 !(E!;;' !(E!;;' !(E!;;' !(E!;;'0 08<2= 8<2= 8<2= 8<2= ,2! ,2! ,2! ,2!0 08 8 8 82 2 2 2<= <= <= <= >2BC?// >2BC?// >2BC?// >2BC?// ? ? ? ?

slide-58
SLIDE 58

Getting Started With Perl

CPAN Example

…and finish it. We supply with a

block containing the condition. The values from the list being sorted are in and , and 38 means compare.

Z@)T" Z@)T" Z@)T" Z@)T" , , , , >>?38>?? >>?38>?? >>?38>?? >>?38>?? I)N I)N I)N I)N 2 2 2 2 <,=> <,=> <,=> <,=> 9>? 9>? 9>? 9>?

  • 9

9 9 9 ? ? ? ?

slide-59
SLIDE 59

Getting Started With Perl

Going Further

I can’t even start to fully cover Perl in

an hour.

The best way to learn Perl is to write

Perl.

The Perl documentation is available on

the web: http://perldoc.perl.org/

Loads of CGI tutorials – just search.

slide-60
SLIDE 60

Getting Started With Perl

Going Further

Documentation is also available at the

console.

Get help on functions:

02

Get help on installed modules:

(E!;;'

slide-61
SLIDE 61

Getting Started With Perl

Going Further

O’Reilly books are very good. Programming Perl is considered the

definitive guide, and is excellent.

Learning Perl goes at a gentler pace.

slide-62
SLIDE 62

Getting Started With Perl

The End

Slides on my site:

http://www.jwcs.net/~jonathan/

Go forth, hack Perl and have fun.