01 Perl Basics 06

download 01 Perl Basics 06

of 28

Transcript of 01 Perl Basics 06

  • 8/3/2019 01 Perl Basics 06

    1/28

    Perl BasicsPerl Basics

    A Perl Tutorial

    NLP Course - 2006

  • 8/3/2019 01 Perl Basics 06

    2/28

    What is Perl?What is Perl?

    Practical Extraction and Report Language Interpreted Language

    Optimized for String Manipulation and File I/O Full support for Regular Expressions

  • 8/3/2019 01 Perl Basics 06

    3/28

    Running Perl ScriptsRunning Perl Scripts

    Windows Download ActivePerl from ActiveState Just run the script from a 'Command Prompt'

    window UNIX Cygwin

    Put the following in the first line of your script

    #!/usr/bin/perl

    Run the script% perl script_name

  • 8/3/2019 01 Perl Basics 06

    4/28

    Basic SyntaxBasic Syntax

    Statements end with semicolon ; Comments start with #

    Only single line comments

    Variables You dont have to declare a variable before you

    access it You don't have to declare a variable's type

  • 8/3/2019 01 Perl Basics 06

    5/28

    Scalars and IdentifiersScalars and Identifiers

    Identifiers A variable name Case sensitive

    Scalar A single value (string or numerical) Accessed by prefixing an identifier with '$' Assignment with '='

    $scalar = expression

  • 8/3/2019 01 Perl Basics 06

    6/28

    StringsStrings

    Quoting Strings With ' (apostrophe)

    Everything is interpreted literally

    With " (double quotes) Variables get expanded

    With ` (backtick) The text is executed as a separate process, and

    the output of the command is returned as thevalue of the string

    Check 01_printDate.pl

  • 8/3/2019 01 Perl Basics 06

    7/28

    String Operation Arithmetic

    lt less than

    eq equal to ==

    le less than or equal to =

    ne not equal to !=

    cmp compare, return 1, 0, -1

    Comparison OperatorsComparison Operators

  • 8/3/2019 01 Perl Basics 06

    8/28

    Operator Operation

    ||, or logical or

    &&, and logical and

    !, not logical not

    xor logical xor

    Logical OperatorsLogical Operators

  • 8/3/2019 01 Perl Basics 06

    9/28

    Operator Operation

    . string concatenation

    x string repetition

    .= concatenation and assignment

    $string1 = "potato";

    $string2 = "head";

    $newstring = $string1 . $string2; #"potatohead"

    $newerstring = $string1 x 2; #"potatopotato"

    $string1 .= $string2; #"potatohead"

    String OperatorsString Operators

    Check concat_input.pl

  • 8/3/2019 01 Perl Basics 06

    10/28

    Perl FunctionsPerl Functions

    Perl functions are identified by their unique names(print, chop, close, etc)

    Function arguments are supplied as a commaseparated list in parenthesis. The commas are necessary The parentheses are often not Be careful! You can write some nasty and unreadable

    code this way!

    Check 02_unreadable.pl

  • 8/3/2019 01 Perl Basics 06

    11/28

    ListsLists

    Ordered collection of scalars Zero indexed (first item in position '0') Elements addressed by their positions

    List Operators (): list constructor , : element separator []: take slices (single or multiple element chunks)

  • 8/3/2019 01 Perl Basics 06

    12/28

    List OperationsList Operations

    sort(LIST)

    a new list, the sorted version of LIST reverse(LIST)

    a new list, the reverse of LIST join(EXPR, LIST)

    a string version of LIST, delimited by EXPR split(PATTERN, EXPR)

    create a list from each of the portions of EXPR that

    match PATTERN

    Check 03_listOps.pl

  • 8/3/2019 01 Perl Basics 06

    13/28

    ArraysArrays

    A named list Dynamically allocated, can be saved Zero-indexed

    Shares list operations, and adds to them Array Operators

    @: reference to the array (or a portion of it, with []) $: reference to an element (used with [])

  • 8/3/2019 01 Perl Basics 06

    14/28

    Array OperationsArray Operations

    push(@ARRAY, LIST)

    add the LIST to the end of the @ARRAY pop(@ARRAY)

    remove and return the last element of @ARRAY unshift(@ARRAY, LIST)

    add the LIST to the front of @ARRAY shift(@ARRAY)

    remove and return the first element of @ARRAY

    scalar(@ARRAY)

    return the number of elements in the @ARRAY

    Check 04_arrayOps.pl

  • 8/3/2019 01 Perl Basics 06

    15/28

    Associative Arrays - HashesAssociative Arrays - Hashes

    Arrays indexed on arbitrary string values Key-Value pairs Use the "Key" to find the element that has the

    "Value"

    Hash Operators % : refers to the hash {}: denotes the key $ : the value of the element indexed by the key

    (used with {})

  • 8/3/2019 01 Perl Basics 06

    16/28

    Hash OperationsHash Operations

    keys(%ARRAY)

    return a list of all the keys in the %ARRAY values(%ARRAY)

    return a list of all the values in the %ARRAY each(%ARRAY)

    iterates through the key-value pairs of the %ARRAY delete($ARRAY{KEY})

    removes the key-value pair associated with {KEY} from

    the ARRAY

  • 8/3/2019 01 Perl Basics 06

    17/28

    Arrays ExampleArrays Example#!/usr/bin/perl# Simple List operations

    # Address an element in the list@stringInstruments =

    ("violin","viola","cello","bass");@brass =

    ("trumpet","horn","trombone","euphonium","tuba");

    $biggestInstrument = $stringInstruments[3];

    print("The biggest instrument: ",$biggestInstrument);

    # Join elements at positions 0, 1, 2 and 4 into awhite-space delimited string

    print("orchestral brass: ",join(" ",@brass[0,1,2,4]),"\n");

    @unsorted_num = ('3','5','2','1','4');@sorted_num = sort( @unsorted_num );

    # Sort the listprint("Numbers (Sorted, 1-5): ",

    @sorted_num,"\n");

    #Add a few more numbers

    @numbers_10 = @sorted_num;

    push(@numbers_10, ('6','7','8','9','10'));

    print("Numbers (1-10): ",

    @numbers_10,

    "\n");

    # Remove the last

    print("Numbers (1-9): ",

    pop(@numbers_10),

    "\n");

    # Remove the first

    print("Numbers (2-9): ",

    shift(@numbers_10),

    "\n");

    # Combine two ops

    print("Count elements (2-9): ",

    $#@numbers_10;# scalar( @numbers_10 ),

    "\n");

    print("What's left (numbers 2-9): ",

    @numbers_10,

    "\n");

  • 8/3/2019 01 Perl Basics 06

    18/28

    Hashes ExampleHashes Example#!/usr/bin/perl# Simple List operations

    $player{"clarinet"} = "Susan Bartlett";$player{"basson"} = "Andrew Vandesteeg";$player{"flute"} = "Heidi Lawson";$player{"oboe"} = "Jeanine Hassel";

    @woodwinds = keys(%player);@woodwindPlayers = values(%player);

    # Who plays the oboe?print("Oboe: ", $player{'oboe'}, "\n");

    $playerCount = scalar(@woodwindPlayers);

    while (($instrument, $name) =each(%player))

    {print( "$name plays the$instrument\n" );

    }

  • 8/3/2019 01 Perl Basics 06

    19/28

    Pattern MatchingPattern Matching

    A pattern is a sequence of characters to besearched for in a character string /pattern/

    Match operators =~: tests whether a pattern is matched !~: tests whether patterns is not matched

  • 8/3/2019 01 Perl Basics 06

    20/28

    Pattern Matches Pattern Matches

    /def/ "define" /d.f/ dif

    /\bdef\b/ a defword /d.+f/ dabcf/^def/ defin start of

    line

    /d.*f/ df, daffff

    /^def$/ defline /de{1,3}f/ deef, deeef

    /de?f/ df, def /de{3}f/ deeef

    /d[eE]f/ def, dEf /de{3,}f/ deeeeef

    /d[^eE]f/ daf, dzf /de{0,3}f/ up to deeef

    PatternsPatterns

  • 8/3/2019 01 Perl Basics 06

    21/28

    Character RangesCharacter Ranges

    EscapeSequence

    Pattern Description

    \d [0-9] Any digit

    \D [^0-9] Anything but a digit\w [_0-9A-Za-z] Any word character

    \W [^_0-9A-Za-z] Anything but a word char

    \s [ \r\t\n\f] White-space\S [^\r\t\n\f] Anything but white-space

  • 8/3/2019 01 Perl Basics 06

    22/28

    BackreferencesBackreferences

    Memorize the matched portion of input

    Use of parentheses. /[a-z]+(.)[a-z]+\1[a-z]+/

    asd-eeed-sdsa, sd-sss-ws NOT as_eee-dfg

    They can even be accessed immediately after thepattern is matched \1 in the previous pattern is what is matched by (.)

  • 8/3/2019 01 Perl Basics 06

    23/28

    Pattern Matching OptionsPattern Matching Options

    EscapeSequence

    Description

    g Match all possible patterns

    i Ignore case

    x Ignore white-space in pattern

  • 8/3/2019 01 Perl Basics 06

    24/28

    SubstitutionsSubstitutions

    Substitution operator s/pattern/substitution/options

    If$string = "abc123def"; $string =~ s/123/456/

    Result: "abc456def" $string =~ s/123//

    Result: "abcdef" $string =~ s/(\d+)/[$1]/

    Result: "abc[123]def

    Use of backreference!

  • 8/3/2019 01 Perl Basics 06

    25/28

    Predefined Read-only VariablesPredefined Read-only Variables$& is the part of the string that matched the regular expression

    $` is the part of the string before the part that matched

    $' is the part of the string after the part that matched

    EXAMPLEEXAMPLE

    $_ = "this is a sample string";

    /sa.*le/; # matches "sample" within the string

    # $` is now "this is a "

    # $& is now "sample"

    # $' is now " string"Because these variables are set on each successful match, you should save the valueselsewhere if you

    need them later in the program.

  • 8/3/2019 01 Perl Basics 06

    26/28

    The split and join FunctionsThe split and join FunctionsThe split function takes a regular expression and a string, and looks for alloccurrences of the regular expression within that string. The parts of the stringthat don't match the regular expression are returned in sequence as a list ofvalues.

    The join function takes a list of values and glues them together with a glue string

    between each list element.Split ExampleSplit Example Join ExampleJoin Example

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

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

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

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

    $bigstring = join($glue,@list);

    For example to rebuilt the password filetry something like:

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

  • 8/3/2019 01 Perl Basics 06

    27/28

    String - Pattern ExamplesString - Pattern ExamplesA simple Example

    #!/usr/bin/perlprint ("Ask me a question politely:\n");

    $question = ;

    # what about capital P in "please"?if ($question =~ /please/){

    print ("Thank you for being polite!\n");

    }else{

    print ("That was not very polite!\n");}

  • 8/3/2019 01 Perl Basics 06

    28/28

    String Pattern ExampleString Pattern Example#!/usr/bin/perlprint ("Enter a variable name:\n");$varname = ;chop ($varname);# Try asd$asdas... It gets accepted!if ($varname =~ /\$[A-Za-z][_0-9a-zA-Z]*/){

    print ("$varname is a legal scalar variable\n");}elsif ($varname =~ /@[A-Za-z][_0-9a-zA-Z]*/){

    print ("$varname is a legal array variable\n");}elsif ($varname =~ /[A-Za-z][_0-9a-zA-Z]*/){

    print ("$varname is a legal file variable\n");}else{

    print ("I don't understand what $varname is.\n");}