Cours Perl

  • Upload
    pdio

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

  • 8/6/2019 Cours Perl

    1/120

    London Perl Workshop

    1st December 20071

    Beginners Perl

    An Introduction to Perl Programming

    Dave Cross

    Magnum Solutions Ltd

    [email protected]

  • 8/6/2019 Cours Perl

    2/120

    London Perl Workshop

    1st December 20072

    What

    W

    eW

    ill Cover What is Perl?

    Creating and running a Perl program Perl variables

    Operators and Functions

  • 8/6/2019 Cours Perl

    3/120

    London Perl Workshop

    1st December 20073

    WhatW

    eW

    ill Cover Conditional Constructs

    Subroutines

    Regular Expressions

    Further Information

  • 8/6/2019 Cours Perl

    4/120

    London Perl Workshop

    1st December 20074

    What is Perl?

  • 8/6/2019 Cours Perl

    5/120

    London Perl Workshop

    1st December 20075

    Perl's Nam

    e Practical Extraction and Reporting

    Language

    Pathologically Eclectic Rubbish Lister

    "Perl" is the language

    "perl" is the compiler

    Never "PERL"

  • 8/6/2019 Cours Perl

    6/120

    London Perl Workshop

    1st December 20076

    Typica

    l uses of Perl Text processing

    System administration tasks CGI and web programming

    Database interaction

    Other Internet programming

  • 8/6/2019 Cours Perl

    7/120

    London Perl Workshop

    1st December 20077

    Lesstypic

    al uses of Perl

    Human Genome Project

    NASA

  • 8/6/2019 Cours Perl

    8/120

    London Perl Workshop

    1st December 20078

    What

    is PerlLike?

    General purpose programming language

    Free (open source) Fast

    Flexible

    Secure

    Dynamic

  • 8/6/2019 Cours Perl

    9/120

    London Perl Workshop

    1st December 20079

    Th

    e Perl Ph

    ilosophy

    There's more than one way to do it

    Three virtues of a programmer Laziness

    Impatience

    Hubris

    Share and enjoy!

  • 8/6/2019 Cours Perl

    10/120

    London Perl Workshop

    1st December 200710

    Creating and Running aPerl Program

  • 8/6/2019 Cours Perl

    11/120

    London Perl Workshop

    1st December 200711

    Creat

    inga

    Perl Program

    Our first Perl programprint "Hello world\n";

    Put this in a file called hello.pl

  • 8/6/2019 Cours Perl

    12/120

    London Perl Workshop

    1st December 200712

    Runninga

    Perl Program

    Running a Perl program from the command

    line

    perl hello.pl

  • 8/6/2019 Cours Perl

    13/120

    London Perl Workshop

    1st December 200713

    Runninga

    Perl Program

    The "shebang" line (Unix, not Perl)#!/usr/bin/perl

    Make program executablechmod +x hello.pl

    Run from command line./hello.pl

  • 8/6/2019 Cours Perl

    14/120

    London Perl Workshop

    1st December 200714

    Perl Comm

    ent

    s Add comments to yout code

    Start with a hash (#) Continue to end of line

    # This is a hello world program

    print "Hello, world!\n"; # print

  • 8/6/2019 Cours Perl

    15/120

    London Perl Workshop

    1st December 200715

    Comma

    ndLine Op

    t

    ions Many options to control execution of the

    program For example, -w turns on warnings

    Use on command lineperl -w hello.pl

    Or on shebang line#!/usr/bin/perl -w

  • 8/6/2019 Cours Perl

    16/120

    London Perl Workshop

    1st December 2007

    16

    Perl variables

  • 8/6/2019 Cours Perl

    17/120

    London Perl Workshop

    1st December 2007

    17

    What

    isa

    Va

    riab

    le? A place where we can store data

    A variable needs a name to retrieve the data stored in it

    put new data in it

  • 8/6/2019 Cours Perl

    18/120

    London Perl Workshop

    1st December 2007

    18

    Va

    riab

    le Nam

    es Contain alphanumeric characters and

    underscores

    User variable names may not start with

    numbers

    Variable names are preceded by apunctuation mark indicating the type of

    data

  • 8/6/2019 Cours Perl

    19/120

    London Perl Workshop

    1st December 2007

    19

    Types of Perl Va

    riab

    le Different types of variables start with a

    different symbol

    Scalar variables start with $

    Array variables start with @

    Hash variables start with %

    More on these types soon

  • 8/6/2019 Cours Perl

    20/120

    London Perl Workshop

    1st December 2007

    20

    Decl

    a

    ring Va

    riab

    les You don't need to declare variables in Perl

    But it's a very good idea typos

    scoping

    Using the strict pragmause strict;

    my $var;

  • 8/6/2019 Cours Perl

    21/120

    London Perl Workshop

    1st December 2007

    21

    Sca

    la

    r Va

    riab

    les Store a single item of data my $name = "Dave";

    my $whoami = 'Just Another Perl

    Hacker';

    my $meaning_of_life = 42;

    my $number_le

    ss_than_1 = 0.000001;

    my $very_large_number = 3.27e17;

    # 3.27 times 10 to the power of 17

  • 8/6/2019 Cours Perl

    22/120

    London Perl Workshop

    1st December 2007

    22

    Type Conversions Perl converts between strings and numbers

    whenever necessary

    # add int to a floating point number

    my $sum = $meaning_of_life +

    $number_less_than_1;

    #putting the n

    umber in a

    stringprint "$name says, 'The meaning of

    life is $sum.'\n";

  • 8/6/2019 Cours Perl

    23/120

    London Perl Workshop

    1st December 2007

    23

    Quot

    ingSt

    rings Single quotes don't expand variables or

    escape sequencesmy $price = '$9.95';

    Double quotes domy $invline = "24 widgets @ $price

    each\n";

    Use a backslash to escape special

    characters in double quoted stringsprint "He said \"The price is \$300\"";

  • 8/6/2019 Cours Perl

    24/120

    London Perl Workshop

    1st December 2007

    24

    Bett

    erQ

    uot

    e Ma

    rks This can look ugly

    print "He said \"The price is \$300\"";

    This is a tidier alternative

    print qq(He said "The price is \$300");

    Also works for single quotes

    print q(He said "That's too expensive");

  • 8/6/2019 Cours Perl

    25/120

  • 8/6/2019 Cours Perl

    26/120

  • 8/6/2019 Cours Perl

    27/120

  • 8/6/2019 Cours Perl

    28/120

    London Perl Workshop

    1st December 2007

    28

    A

    rra

    yS

    lices Returns a list of elements from an array

    print @fruits[0,2,4];

    # prints "apples", "guavas",

    # "grapes"

    print @fruits[1 .. 3];

    # prints "oranges", "guavas",

    # "passionfruit"

    Note use of @ as we are accessing more

    than one element of the array

  • 8/6/2019 Cours Perl

    29/120

    London Perl Workshop

    1st

    December 2007

    29

    S

    ett

    ingA

    rray

    Va

    lues $array[4] = 'something';

    $array[400] = 'something else';

    Also with slices @array[4, 7 .. 9] = ('four', 'seven',

    'eight', 'nine');

    @array[1, 2] = @array

    [2, 1];

    Doesn't need to be an array! ($x, $y) = ($y, $x);

  • 8/6/2019 Cours Perl

    30/120

    London Perl Workshop

    1st

    December 2007

    30

    ArraySize

    $#array is the index of the last element

    in @array

    Therefore $#array + 1 is the number of

    elements $count = @array;

    # or $count = scalar @arraydoes the same thing and is easier to

    understand

  • 8/6/2019 Cours Perl

    31/120

    London Perl Workshop

    1st

    December 2007

    31

    Hash Variables

    Hashes implement look-up tables or

    dictionaries

    Initialised with a list%french = ('one', 'un', 'two', 'deux',

    'three', 'trois');

    "fat comma" (=>) is easier to understand%german = (one => 'ein',two => 'zwei',

    three => 'drei');

  • 8/6/2019 Cours Perl

    32/120

    London Perl Workshop

    1st

    December 2007

    32

    Accessing Hash Values

    $three = $french{three};

    print $german{two}; As with arrays, notice the use of $ to

    indicate that we're accessing a single value

  • 8/6/2019 Cours Perl

    33/120

    London Perl Workshop

    1st

    December 2007

    33

    HashSlices

    Just like array slices

    Returns a list of elements from a hashprint @french{'one','two','three'};# prints "un", "deux" & "trois"

    Again, note use of @ as we are accessing

    more than one value from the hash

  • 8/6/2019 Cours Perl

    34/120

  • 8/6/2019 Cours Perl

    35/120

    London Perl Workshop

    1st

    December 2007

    35

    More AboutHashes

    Hashes are not sorted

    There is no equivalent to $#array print %hash is unhelpful

    We'll see ways round these restrictions later

  • 8/6/2019 Cours Perl

    36/120

    London Perl Workshop

    1st

    December 2007

    36

    Special Perl Variables

    Perl has many special variables

    Many of them have punctuation marks asnames

    Others have names in ALL_CAPS

    They are documented in perlvar

  • 8/6/2019 Cours Perl

    37/120

  • 8/6/2019 Cours Perl

    38/120

    London Perl Workshop

    1st

    December 2007

    38

    Using $_

    while () {if (/regex/) {

    print;}}

    Three uses of $_

  • 8/6/2019 Cours Perl

    39/120

    London Perl Workshop

    1st

    December 2007

    39

    ASpecial Array

    @ARGV

    Contains your program's command linearguments

    perl printargs.pl foo bar baz

    my $num = @ARGV;print "$num arguments: @ARGV\n";

  • 8/6/2019 Cours Perl

    40/120

    London Perl Workshop

    1st

    December 2007

    40

    ASpecial Hash

    %ENV

    Contains the environment variables thatyour script has access to.

    Keys are the variable names

    Values are the well values! print $ENV{PATH};

  • 8/6/2019 Cours Perl

    41/120

    London Perl Workshop

    1st

    December 2007

    41

    Operators and Functions

  • 8/6/2019 Cours Perl

    42/120

  • 8/6/2019 Cours Perl

    43/120

    London Perl Workshop

    1st

    December 2007

    43

    Arithmetic Operators

    Standard arithmetic operationsadd (+), subtract (-), multiply (*), divide (/)

    Less standard operationsmodulus (%), exponentiation (**)

    $speed = $distance / $time;

    $vol = $length * $breadth * $height;$area = $pi * ($radius ** 2);$odd = $number % 2;

  • 8/6/2019 Cours Perl

    44/120

    London Perl Workshop

    1st

    December 2007

    44

    Shortcut Operators

    Often need to do things like$total = $total + $amount;

    Can be abbreviated to$total += $amount;

    Even shorter$x++; #same as $x += 1 or $x = $x + 1

    $y--; #same as $y -= 1 or $y = $y - 1

    Subtle difference between $x++ and ++$x

  • 8/6/2019 Cours Perl

    45/120

    London Perl Workshop

    1st

    December 2007

    45

    String Operators

    Concaternation (.)$name = $firstname . ' ' . $surname;

    Repetition (x)$line = '-' x 80;$police = 'hello ' x 3;

    Shortcut versions available$page .= $line; # $page = $page . $line$thing x= $i; # $thing = $thing x $i

  • 8/6/2019 Cours Perl

    46/120

  • 8/6/2019 Cours Perl

    47/120

  • 8/6/2019 Cours Perl

    48/120

  • 8/6/2019 Cours Perl

    49/120

    London Perl Workshop

    1st

    December 2007

    49

    String Functions

    length returns the length of a string$len = length $a_string;

    uc and lc return upper and lower caseversions of a string$string = 'MiXeD CaSe';print "$string\n", uc $string, "\n",

    lc $string;

    See also ucfirst and lcfirst

  • 8/6/2019 Cours Perl

    50/120

  • 8/6/2019 Cours Perl

    51/120

    London Perl Workshop

    1st

    December 2007

    51

    Substrings

    substr returns substrings from a string$string = 'Hello world';

    printsubs

    tr($string, 0, 5);# prints 'Hello'

    Unlike many other languages you canassign to a substring

    substr($string, 0, 5) = 'Greetings';print $string;# prints 'Greetings world'

  • 8/6/2019 Cours Perl

    52/120

  • 8/6/2019 Cours Perl

    53/120

  • 8/6/2019 Cours Perl

    54/120

  • 8/6/2019 Cours Perl

    55/120

  • 8/6/2019 Cours Perl

    56/120

  • 8/6/2019 Cours Perl

    57/120

    London Perl Workshop

    1

    st

    December 2007

    57

    File Operations

    open opens a file and associates it with afilehandle

    open(FILE, 'in.dat'); You can then read the file with $line = ; # one line@lines = ; # all lines

    Finally, close the file with closeclose(FILE);

  • 8/6/2019 Cours Perl

    58/120

    London Perl Workshop

    1

    st

    December 2007

    58

    Other File Functions

    read to read a fixed number of bytes into abuffer

    $bytes = read(FILE, $buffer, 1024);

    seek to move to a random postion in a fileseek(FILE, 0, 0);

    tell to get current file position$where = tell FILE;

    truncate to truncate file to given sizetruncate FILE, $where;

  • 8/6/2019 Cours Perl

    59/120

  • 8/6/2019 Cours Perl

    60/120

    London Perl Workshop

    1

    st

    December 2007

    60

    localtime Caveats

    $mon is 0 to 11

    $year is years since 1900

    $wday is 0 (Sun) to 6 (Sat)

  • 8/6/2019 Cours Perl

    61/120

  • 8/6/2019 Cours Perl

    62/120

    London Perl Workshop1st December 2007

    62

    Conditional Constructs

    Conditional constructs allow us to choosedifferent routes of execution through the

    program This makes for far more interesting

    programs

    The unit of program execution is a blockofcode

    Blocks are delimited with braces { }

  • 8/6/2019 Cours Perl

    63/120

    London Perl Workshop1st December 2007

    63

    Conditional Constructs

    Conditional blocks are controlled by theevaluation of an expression to see if it is

    true or false But what is truth?

  • 8/6/2019 Cours Perl

    64/120

    London Perl Workshop1st December 2007

    64

    What is Truth?

    In Perl it's easier to answer the question"what is false?"

    0 (the number zero) '' (the empty string)

    undef (an undefined value)

    () (an empty list) Everything else is true

  • 8/6/2019 Cours Perl

    65/120

    London Perl Workshop1st December 2007

    65

    Comparison Operators

    Compare two values in some way are they equal

    $x == $y or$x eq $y$x != $y or$x ne $y

    Is one greater than another$x > $y or$x gt $y

    $x >= $y or$x ge $y Also < (lt) and

  • 8/6/2019 Cours Perl

    66/120

  • 8/6/2019 Cours Perl

    67/120

    London Perl Workshop1st December 2007

    67

    Boolean Operators

    Combine two or more conditionalexpressions into one

    EXPR_1 and EXPR_2true if both EXPR_1 and EXPR_2 are true

    EXPR_1 or _EXPR_2

    true if either EXPR_1 or _EXPR_2 are true

    alternative syntax && forand and || foror

  • 8/6/2019 Cours Perl

    68/120

  • 8/6/2019 Cours Perl

    69/120

    London Perl Workshop1st December 2007

    69

    if

    if - our first conditional

    if (EXPR) { BLOCK }

    Only executes BLOCK if EXPR is trueif ($name eq 'Doctor') {regenerate();

    }

  • 8/6/2019 Cours Perl

    70/120

    London Perl Workshop1st December 2007

    70

    if ... else ...

    if else ... - an extended ifif (EXPR) { BLOCK1 } else { BLOCK2}

    If EXPR is true, execute BLOCK1,otherwise execute BLOCK2

    if ($name eq 'Doctor') {regenerate();

    } else {die "Game over!\n";

    }

  • 8/6/2019 Cours Perl

    71/120

  • 8/6/2019 Cours Perl

    72/120

  • 8/6/2019 Cours Perl

    73/120

    London Perl Workshop1st December 2007

    73

    while

    while - repeat the same codewhile (EXPR) { BLOCK}

    Repeat BLOCKwhile EXPR is truewhile ($dalek_prisoners) {print "Ex-ter-min-ate\n";$dalek_prisoners--;

    }

  • 8/6/2019 Cours Perl

    74/120

    London Perl Workshop1st December 2007

    74

    until

    until - the opposite of whileuntil (EXPR) { BLOCK}

    Execute BLOCKuntil EXPR is trueuntil ($regenerations == 12) {print "Regenerating\n";regenerate();$regenerations++;

    }

  • 8/6/2019 Cours Perl

    75/120

  • 8/6/2019 Cours Perl

    76/120

    London Perl Workshop1st December 2007

    76

    for

    An examplefor ($i = 1; $i

  • 8/6/2019 Cours Perl

    77/120

    London Perl Workshop1st December 2007

    77

    foreach

    foreach - simpler looping over listsforeach VAR (LIST) { BLOCK}

    For each element of LIST, set VAR toequal the element and execute BLOCKforeach $i (1 .. 10) {print "$i squared is ",

    $i * $i, "\n";}

  • 8/6/2019 Cours Perl

    78/120

    London Perl Workshop1st December 2007

    78

    foreach

    Another examplemy %months = (Jan => 31, Feb => 28,

    Mar => 31, Apr => 30,May => 31, Jun => 30, );

    foreach (keys%months) {print "$_ has $months{$_} days\n";

    }

  • 8/6/2019 Cours Perl

    79/120

    London Perl Workshop1st December 2007

    79

    Using while Loops

    Taking input from STDIN

    while () {

    print;}

    This is the same as

    while (defined($_ = )) {print $_;}

  • 8/6/2019 Cours Perl

    80/120

    London Perl Workshop1st December 2007

    80

    Breaking Out ofLoops

    next - jump to next iteration of loop

    last - jump out of loop

    redo - jump to start ofsame iteration ofloop

  • 8/6/2019 Cours Perl

    81/120

    London Perl Workshop1st December 2007

    81

    Subroutines

  • 8/6/2019 Cours Perl

    82/120

    London Perl Workshop1st December 2007

    82

    Subroutines

    Self-contained "mini-programs" withinyour program

    Subroutines have a name and a block ofcode

    sub NAME {BLOCK

    }

  • 8/6/2019 Cours Perl

    83/120

    London Perl Workshop1st December 2007

    83

    Subroutine Example

    Simple subroutine examplesub exterminate {

    print "Ex-Ter

    -Min

    -Ate!!\n";$timelords--;

    }

  • 8/6/2019 Cours Perl

    84/120

    London Perl Workshop1st December 2007

    84

    Calling aSubroutine

    &slay;

    slay();

    slay;

    last one only works if function has beenpredeclared

  • 8/6/2019 Cours Perl

    85/120

    London Perl Workshop1st December 2007

    85

    Subroutine Arguments

    Functions become far more useful if youcan pass arguments to them

    exterminate('The Doctor'); Arguments end up in the @_ array within

    the functionsub exterminate {

    my ($name) = @_;print "Ex-Ter-Min-Ate $name\n";$timelords--;

    }

  • 8/6/2019 Cours Perl

    86/120

    London Perl Workshop1st December 2007

    86

    Multiple Arguments

    As @_ is an array it can contain multiplearguments

    sub exterminate {foreach (@_) {print "Ex-Ter-Min-Ate $_\n";$timelords--;

    }}

  • 8/6/2019 Cours Perl

    87/120

    London Perl Workshop1st December 2007

    87

    Calling Subroutines

    A subtle difference between &my_sub andmy_sub()

    &my_subpasses on the contents of@_ tothe called subroutinesub first { &second };subsecond { print @_ };

    first('some', 'random', 'data');

  • 8/6/2019 Cours Perl

    88/120

    London Perl Workshop1st December 2007

    88

    By Value or Reference

    Passing by value passes the value of thevariable into the subroutine. Changing the

    argument doesn't alter the external variable Passing by value passes the actualvariable.

    Changing the argument alters the externalvalue

    Perl allows you to choose

  • 8/6/2019 Cours Perl

    89/120

    London Perl Workshop1st December 2007

    89

    By Value or Reference

    Simulating pass by valuemy ($arg1, $arg2) = @_;

    Updating $arg1 and $arg2 doesnt effectanything outside the subroutine

    Simulating pass by referenceUpdating the contents of@_ updates the

    external values$_[0] = 'whatever';

  • 8/6/2019 Cours Perl

    90/120

    London Perl Workshop1st December 2007

    90

    Returning Values

    Use return to return a value from asubroutine

    sub exterminate {if (rand > .25) {print "Ex-Ter-Min-Ate $_[0]\n";$timelords--;return 1;

    } else {return;}

    }

  • 8/6/2019 Cours Perl

    91/120

    London Perl Workshop1st December 2007

    91

    Returning aList

    Returning a list from a subroutinesub exterminate {my @exterminated;

    foreach (@_) {if (rand > .25) {print "Ex-Ter-Min-Ate $_\n";$timelords--;push @exterminated, $_;

    }

    }return @exterminated;

    }

  • 8/6/2019 Cours Perl

    92/120

    London Perl Workshop1st December 2007

    92

    Regular Expressions

  • 8/6/2019 Cours Perl

    93/120

    London Perl Workshop1st December 2007

    93

    Regular Expressions

    Patterns that match strings

    A bit like wild-cards

    A "mini-language" within Perl (AlienDNA)

    The key to Perl's text processing power

    Sometimes overused! Documented in perldoc perlre

  • 8/6/2019 Cours Perl

    94/120

    London Perl Workshop1st December 2007

    94

    Match Operator

    m/PATTERN/ - the match operator

    works on $_ by default

    in scalar context returns true if the matchsucceeds

    in list context returns list of "captured" text

    m is optional if you use / characters with m you can use any delimiters

  • 8/6/2019 Cours Perl

    95/120

    London Perl Workshop1st December 2007

    95

    Match Examples

    m/PATTERN/ examples

    while () {

    print if /foo/;print if /bar/i;print if m|http://|;

    }

  • 8/6/2019 Cours Perl

    96/120

    London Perl Workshop1st December 2007

    96

    Substitutions

    s/PATTERN/REPLACEMENT/ - thesubstitution operator

    works on $_ by default in scalar context returns true if substitution

    succeeds

    in list context returns number ofreplacements

    can choose any delimiter

  • 8/6/2019 Cours Perl

    97/120

    London Perl Workshop1st December 2007

    97

    Substitution Examples

    s/PATTERN/REPLACEMENT/ examples

    while () {

    s/teh/the/gi;s/freind/friend/gi;s/sholud/should/gi;print;

    }

  • 8/6/2019 Cours Perl

    98/120

    London Perl Workshop1st December 2007

    98

    Binding Operator

    If we want m// ors/// to work onsomething other than $_ then we need to

    use the binding operator $name =~ s/Dave/David/;

  • 8/6/2019 Cours Perl

    99/120

    London Perl Workshop1st December 2007

    99

    Metacharacters

    Matching something other than literal text

    ^ - matches start of string

    $ - matches end of string

    . - matches any character (except \n)

    \s - matches a whitespace character

    \S - matches a non-whitespace character

  • 8/6/2019 Cours Perl

    100/120

    London Perl Workshop1st December 2007

    100

    More Metacharacters

    \d - matches any digit

    \D - matches any non-digit

    \w - matches any "word" character

    \W - matches any "non-word" character

    \b - matches a word boundary

    \B - matches anywhere except a wordboundary

  • 8/6/2019 Cours Perl

    101/120

    London Perl Workshop1st December 2007

    101

    Metacharacter Examples

    while () {print if m|^http|;

    print if /\bperl\b/;print if /\S/;print if /\$\d\.\d\d/;

    }

  • 8/6/2019 Cours Perl

    102/120

    London Perl Workshop1st December 2007

    102

    Quantifiers

    Specify the number of occurrences

    ? - match zero or one

    * - match zero or more

    + - match one or more

    {n} - match exactly n

    {n,} - match n or more

    {n,m} - match between n and m

  • 8/6/2019 Cours Perl

    103/120

    London Perl Workshop1st December 2007

    103

    Quantifier Examples

    while () {print if /whiske?y/i;

    print if /so+n/;print if /\d*\.\d+/;print if /\bA\w{3}\b/;

    }

  • 8/6/2019 Cours Perl

    104/120

    London Perl Workshop1st December 2007

    104

    Character Classes

    Define a class of characters to match

    /[aeiou]/# match any vowel

    Use - to define a contiguous set /[A-Z]/# match upper caseletters

    Use ^ to match inverse set /[^A-Za-z] # match non-letters

  • 8/6/2019 Cours Perl

    105/120

    London Perl Workshop1st December 2007

    105

    Alternation

    Use | to match one of a set of options

    /rose|donna|martha/i;

    Use parentheses for grouping

    /^(rose|donna|martha)$/i;

  • 8/6/2019 Cours Perl

    106/120

    London Perl Workshop1st December 2007

    106

    Capturing Matches

    Parentheses are also used to capture partsof the matched string

    The captured parts are in $1, $2, etcwhile () {if (/^(\w+)\s+(\w+)/) {print "The first word was $1\n";

    print "Thesec

    ond word was$2";}

    }

  • 8/6/2019 Cours Perl

    107/120

    London Perl Workshop1st December 2007

    107

    Returning Captures

    Captured values are also returned if thematch operator is used in list context

    my @nums = $text =~ /(\d+)/g;print "I found these integers:\n";print "@nums\n";

  • 8/6/2019 Cours Perl

    108/120

    London Perl Workshop1st December 2007

    108

    More Information

  • 8/6/2019 Cours Perl

    109/120

    London Perl Workshop1st December 2007

    109

    Perl Websites

    Perl Home Page http://www.perl.org

    CPAN http://www.cpan.org

    http://search.cpan.org

    Perl Mongers (Perl User Groups) http://www.pm.org

    http://london.pm.org

  • 8/6/2019 Cours Perl

    110/120

    London Perl Workshop1st December 2007

    110

    Perl Websites

    use perl;(Perl news site) http://use.perl.org

    Perl Monks (Perl help and advice) http://www.perlmonks.org

    Perl documentation online http://perldoc.perl.org

  • 8/6/2019 Cours Perl

    111/120

    London Perl Workshop1st December 2007

    111

    Perl Conferences

    The Perl Conference(part of the Open Source Convention)

    July, 21-25 2008 Portland, Oregon http://conferences.oreilly.com

    Yet Another Perl Conference 2008 Copenhagen, Denmark

    http://www.yapceurope.org

  • 8/6/2019 Cours Perl

    112/120

    London Perl Workshop1st December 2007

    112

    Perl Conferences OtherYAPCs

    Chicago, Illinois

    Brazil

    Tokyo

    OSDC

    Israel

    Australia

  • 8/6/2019 Cours Perl

    113/120

    London Perl Workshop1st December 2007

    113

    Perl Workshops One-day grassroots conferences

    Like this one

    Germany, Israel, Pittsburgh, Nordic,Netherlands, France, Belgium, Russia,Minnesota, Austria

    Perl Review Calendar

    www.theperlreview.com/community_calendar

  • 8/6/2019 Cours Perl

    114/120

  • 8/6/2019 Cours Perl

    115/120

    P l B k

  • 8/6/2019 Cours Perl

    116/120

    London Perl Workshop1st December 2007

    116

    Perl Books

    Books you should have access to Programming Perl (3rd edition)

    Wall, Christiansen & Orwant (O'Reilly) The Perl Cookbook (2nd edition)

    Christiansen & Torkington (O'Reilly)

    Perl Best PracticesConway (O'Reilly)

    Perl in a NutshellSiever, Spainhour & Patwardhan (O'Reilly)

    P l B k

  • 8/6/2019 Cours Perl

    117/120

    London Perl Workshop1st December 2007

    117

    Perl Books

    Books you should probably look at Mastering Regular Expressions

    Friedl (O'Reilly)

    Data Munging with PerlCross (Manning)

    Advanced Perl ProgrammingCozens (O'Reilly)

    Perl MedicScott (Addison Wesley)

    P l B k

  • 8/6/2019 Cours Perl

    118/120

    London Perl Workshop1st December 2007

    118

    Perl Books

    Specialised Perl books Object Oriented Perl

    Conway (Manning) Programming the Perl DBI

    Descartes & Bunce (O'Reilly)

    Writing CGI Applications with PerlMeltzer & Michelski (Addison Wesley)

    Practical mod_perlBekman & Cholet (O'Reilly)

    P l M i

  • 8/6/2019 Cours Perl

    119/120

    London Perl Workshop1st December 2007

    119

    Perl Magazines

    The Perl Review http://www.theperlreview.com

  • 8/6/2019 Cours Perl

    120/120

    That's All Folks Questions

    Lunchtime