Perl Practical Extraction and Report Language Tutorial - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Perl Practical Extraction and Report Language Tutorial

Description:

The function's arguments are supplied as a comma separated list in parenthesis. ... An Example of a list: ('cat', 'dog', 'tiger', 'elephant' ... – PowerPoint PPT presentation

Number of Views:380
Avg rating:3.0/5.0
Slides: 34
Provided by: Sta778
Category:

less

Transcript and Presenter's Notes

Title: Perl Practical Extraction and Report Language Tutorial


1
Perl (Practical Extraction and Report Language)
Tutorial
  • Based on NCSA tutorial at
  • http//archive.ncsa.uiuc.edu/General/Training/Perl
    Intro/

2
Introduction
  • Interpreted programming language
  • Combines the syntax of Bourne shell, csh, awk,
    sed, grep, and C

3
Starting Perl
  • perl scriptname or
  • cat scriptname perl or
  • within an executable text file the first line
    should be
  • !/usr/bin/perl

4
Basic Syntax
  • Perl is free form - whitespace doesn't matter
  • All perl statements end with a
  • Comments begin with sign
  • Variables need not be declared (not strongly
    typed)

5
Basic Syntax Contd.
  • Variable names (identifiers) can be composed of
    lower or upper case letters, numbers and the
    underscore ( _ )
  • Scalars (a single value, either a string or a
    numeric) are accessed by prefixing an identifier
    with ''.
  • Scalars are assigned to using '
  • scalar expression

6
Strings
  • (apostrophe)
  • a pair of apostrophes is interpreted literally -
    no variable expansion takes place
  • instrument 'saxophone'
  • littleInstrument 'soprano instrument'
  • the string value of littleInstrument includes
    the text "instrument"

7
Strings Contd.
  • " (double quotes)
  • The double quote expands variables between the
    pair of quotes
  • instrument "saxophone"
  • littleInstrument "soprano instrument"
  • the string value of littleInstrument is "soprano
    saxophone"

8
Strings Contd.
  • (backtick)
  • This quote performs as it does in the UNIX shells
    - the text inside the backticks is executed as a
    separate process
  • To include an apostrophe or a double quote or a
    backtick in the string, you must escape it with a
    backslash '\'.

9
Perl Functions
  • Perl has many built-in functions. A list of
    functions is available at
  • http//www.perldoc.com/perl5.6/pod/perlfunc.html
  • The function's arguments are supplied as a comma
    separated list in parenthesis.
  • The commas are necessary, the parentheses are
    often not.
  • print("length " ,length("hello world"), \n)
  • prints the same thing as
  • print "length ", length hello world, \n

10
Lists
  • A list is an ordered collection of scalars.
  • Space for lists are dynamically allocated and
    removed from the program's memory.
  • Each element can be addressed by its integer
    position in the list. Lists are 0-indexed the
    first element is called "0".

11
Lists Contd.
  • Typical operators include
  • ( )
  • The list constructor
  • ,
  • The comma is used to separate elements of the
    list
  • (brackets)
  • Brackets are used to take elements of the list
  • An Example of a list (cat", dog", tiger",
    elephant")
  • largestAnimal (cat", dog", tiger",
    elephant")3

12
Some functions manipulating lists
  • sort (LIST)
  • sort ((a, d, b, c, e)) gives a, b, c, d, e
  • reverse (LIST)
  • reverse ((a, b, c, d, e)) gives e, d, c, b,
    a
  • join (EXPR, LIST)
  • join ( , (a, b, c, d, e)) gives a b
    c d e
  • split (PATTERN, EXPR)
  • info a b c d
  • split (//, info) gives abcd

13
Arrays
  • An array is a named list.
  • As with lists, its space is dynamically allocated
    and removed, it is 0-indexed
  • _at_stringInstruments ("violin","viola","cello","b
    ass")

14
Some functions manipulating arrays
  • push (_at_arr, LIST)
  • _at_arr (a, b, c)
  • push (_at_arr, x, y, z) gives an array of
    a, b, c, x, y, z
  • pop (_at_arr)
  • _at_arr (a, b, c)
  • pop (_at_arr) removes and returns the last
    element c
  • shift (_at_array)
  • _at_arr (a, b, c)
  • shift (_at_arr) removes and returns the first
    element a, shifts the other elements forward
  • unshift (_at_array, LIST)
  • _at_arr (x, y, z)
  • unshift (_at_arr, a, b, c) gives a, b, c,
    x, y, z
  • scalar (_at_array)
  • _at_arr (a, b, c)
  • scalar (_at_arr) gives 3 the number of
    elements in the array

15
Associative Arrays (Hashes)
  • Arrays that are indexed not on ordered integers,
    but on arbitrary string values.
  • Elements of an associative array are referred to
    as "key" and "value" pairs - the "key" is used to
    find the element of the array that has the
    "value
  • (percent sign)
  • Refers to the entire array
  • (braces)
  • Used to denote the key
  • When used with , this is the value of the
    array element indexed on the key.

16
Associative Arrays Contd.
  • Examples
  • principalkey1" v1"
  • principalkey2" v2"
  • principalkey3" v3"
  • principalkey4" v4"
  • _at_Keys keys(principal)
  • _at_Values values(principal)

17
Conditionals
  • To do comparisons on different values, perl
    provides two sets of conditionals - one for
    numbers and one for strings.
  • Comparison Strings Numbers
  • Equality eq
  • Inequality ne !
  • Greater than gt
  • Greater than or equal to ge
  • Less than lt
  • Less than or equal to le
  • Comparison returns -1,0,1 cmp

18
Conditionals Contd.
  • It's important to use the right conditional -
    there are no "syntax" errors
  • That is if you use a variable in the wrong
    context, there could be unexpected results
  • You can build up conditionals using (AND) and
    (OR) operations

19
IF ELSE
  • if (EXPRESSION )
  • STATEMENTS
  • if (EXPRESSION )
  • executed if true STATEMENTS
  • else
  • executed if false STATEMENTS
  • if (EXPRESSION )
  • STATEMENTS
  • elsif
  • STATEMENTS optional additional ELSIF's
  • else
  • STATEMENTS
  • Unlike C, the braces are not optional - require
    braces

20
DO / WHILE LOOPS
  • while ( EXPRESSION )
  • STATEMENTS
  • If the condition is initially false, the body of
    the loop will never execute.
  • do
  • STATEMENTS
  • while (EXPRESSION)
  • The loop will execute at least once.

21
Loops
  • All loops support the following two control
    statements
  • last
  • Declare that this is the last statement in the
    loop completely exit the loop even if the
    condition is still true, ignoring all statements
    up to the loop's closing brace.
  • next
  • Start a new iteration of the loop
  • An Example one way to count to 10
  • number 0
  • while(1)
  • number
  • if (number 10 ) last

22
FOR LOOPS
  • for ( INITIAL_EXPR COND_EXPR LOOP_EXPR )
    STATEMENTS
  • The loop is initialized by evaluating
    INITIAL_EXPR, iterates while COND_EXPR is true,
    and evaluates LOOP_EXPR before beginning each
    subsequent loop.
  • The loop flow can be modified using next and
    last, as in the DO loop.
  • Unlike C, the braces around the block of
    STATEMETNTS are mandatory

23
FOR LOOPS Contd.
  • An Example countdown
  • for ( i10 i0 i- -)
  • print("i\n")

24
FOREACH LOOP
  • The syntax is
  • foreach SCALAR ( LIST )
  • STATEMENTS
  • Each time through the loop, SCALAR is assigned
    the next element in the LIST, and the STATEMENTS
    are executed.
  • The loop flow can be modified using next and
    last, as in the DO loop.
  • The braces are mandatory.

25
FOREACH LOOP Contd.
  • An Example
  • foreach number(one",two",three",four")
  • print(number," is smaller than a \n")
  • print(five\n")

26
Subroutines
  • Perl subroutine definitions take the following
    form
  • sub NAME STATEMENTS
  • In perl, subroutines are invoked differently than
    the built-ins
  • NAME(arg1,arg2,etc)
  • or if the subroutine takes no arguments
  • NAME
  • return values are used just like any other value
    - always return the last thing evaluated
  • loudestInstrument measureLoudest(_at_allInstrume
    nts)

27
Subroutines Contd.
  • Examples
  • sub printargs
  • print "_at__\n"
  • printargs("perly", "king") prints "perly
    king
  • printargs("frog", "and", "toad") Prints
    "frog and toad"
  • Just like any other list array the individual
    elements of _at__ can be accessed with the square
    bracket notation
  • sub printfirsttwo
  • print "Your first argument was _at__0\n" print
    "and _at__1 was your second\n"

28
Subroutines Contd.
  • sub maximum
  • if (_at__0 _1)
  • _at__0
  • else
  • _at__1
  • biggest maximum(37, 24) Now biggest is
    37

29
File Test Operators
  • Some file test operators
  • -r readable
  • -x executable
  • -e exists
  • -d is a directory
  • -t is a tty
  • -T text file

30
File Management
  • open(FILEHANDLE,FILENAME)
  • close(FILEHANDLE)
  • FILEHANDLEs aren't denoted by a special character
    like _at_ , and are not strings,
  • the FILENAME is a string, special formats are
    below.
  • open returns 1 on success, 0 on failure

31
File Management Contd.
  • Conventions for opening files
  • "FILE" open FILE for input (also
  • " FILE" open FILE for output, creating if
    necessary
  • " FILE" open FILE for appending

32
File Management Contd.
  • When we have a filehandle, we need to read or
    write to it. The read operator is , as in
  • read one line
  • line
  • read the entire file
  • _at_lines
  • To print to a FILEHANDLE, print takes an
    optional first argument which is the filehandle.
  • print takes a list as an argument .The first
    argument does not have a comma.
  • print(FILEHANDLE "your","list","here")

33
File Management Contd.
  • Reading Example
  • file '/etc/passwd' Name the file
  • open(INFO,
  • _at_lines Read it into an array
  • close(INFO) Close the file print
  • print _at_lines Print the array
  • Writing Example
  • open(FHANDLE, NewFile) Open the file
  • print (FHANDLE Your,List, Here) Write to
    the file
  • close(FHANDLE) Close the file
Write a Comment
User Comments (0)
About PowerShow.com