Perl - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Perl

Description:

OBJECTIVES What is Perl Concepts Variables Control Structures Modules Objects Windows – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 35
Provided by: Schoolo212
Category:

less

Transcript and Presenter's Notes

Title: Perl


1
Perl
OBJECTIVES
  • What is Perl
  • Concepts
  • Variables
  • Control Structures
  • Modules
  • Objects
  • Windows

2
Perl
  • Practical Extraction and Report Language
  • Originally designed as a glue language
  • Perl is a scripting language
  • Each invocation of a Perl script compiles then
    executes code
  • Uses a C-like syntax
  • Has object-oriented programming features
  • Highly portable between OSs

3
Running Perl
  • On Unix
  • Set line 1 to !/usr/bin/perl (or wherever Perl
    is installed)
  • On Windows
  • Set file extension to
  • .pl for standard Perl
  • .pls for PerlScript (ActiveX scripting engine)

4
Variables
  • Perl is not a strongly typed language, the
    contents of a variable are converted as necessary
  • The first character of a variable name indicates
    the type of a variable
  • name
  • The name part of variable can also be enclosed in
  • name
  • _at_reference_to_array

Scalar Name of individual value
Array _at_ List a values, keyed by index
Hash List of values, keyed by string
Subroutine Callable Perl code
Typeglob Everything
5
Variables - Scalar
  • A scalar represents a single value
  • Integer
  • Floating point
  • String
  • Reference
  • The data held by the variable is converted as
    necessary
  • Scalar names start with a
  • name
  • As an lvalue
  • name george burdell

6
Variables - Arrays
  • An array is an ordered list of scalars
  • Arrays are indexed by a number, starting at 0
  • Arrays indexed by negative numbers are ordered
    backwards from the end of the array
  • The indexing operator is
  • An array starts with _at_
  • _at_names
  • _at_names1,3,5 slice
  • _at_names2 .. 6 slice
  • A single element of an array starts with
  • names4
  • namesvalue

7
Variables - Arrays
  • As an lvalue
  • names4 345
  • _at_names (1,2,3,4,5)
  • _at_names 1 .. 5
  • last_value names-1

8
Variables - Hashes
  • A hash, or associative array, is an un-ordered
    list of scalars
  • Hashes are indexed by strings
  • The indexing operator is
  • A hash starts with
  • months
  • monthsJan,Feb slice
  • A single element of a hash starts with
  • monthsMar
  • monthssome_string
  • As an lvalue
  • monthsMar March
  • months (Jan gt January, Feb gt
    February)

9
Variables - Namespaces
  • Two types of namespaces
  • Global
  • Lexical
  • Global variables are kept in symbol tables that
    are named and accessible
  • Created in the context of a package (default is
    main)
  • Referenced from another package by
    packagevariable
  • Lexical variables are created and exist only in
    the context of a Perl block

10
Literals Numeric
  • Numeric literals can take several formats
  • 12345 integer
  • 12345.67 floating point
  • 1.23e06 scientific
  • 1_234_567
  • 0123 octal
  • 0xffff hexidecimal
  • 0b101010 binary

11
Literals - String
  • Backslash escape characters
  • \n newline
  • \r carriage return
  • \t tab
  • \033 character represented by octal 033
  • \cX Control-X
  • \x263a Unicode character
  • Translation escapes
  • \u force next character to uppercase
  • \l force next character to lowercase
  • \U force all following characters to uppercase
  • \L force all following characters to lowercase
  • \E end \U or \L switch

12
Literals - String
  • There are several ways to quote a string
  • Substitution for variables in a string is known
    as interpolation
  • print The value is value\n
  • print The value is ,value,\n
  • Interpolation occurs for variables and back slash
    literals

Usual General Meaning Interpolate
q/ / Literal string No
qq/ / Literal string Yes
qx/ / Command execution Yes
( ) qw/ / Word list No
/ / m/ / Pattern match Yes
s/ / / s/ / / Pattern substitution Yes
y/ / / tr/ / / Character translation No
13
Literals - String
  • There is flexibility in choosing quotes
  • string qqThis method allows inclusion of
    and
  • Command execution, executes the command and
    returns its output as a string
  • result qx(ls)
  • Word list
  • _at_months qw(January February March April)

14
Interpolation
  • Interpolation is the process of expanding a
    variable in a string literal
  • Scalars are resolved in place, numeric values are
    converted to characters
  • Arrays are interpolated by joining all the
    elements of the array separated by the value of
    the special variable
  • print _at_months
  • Hashes are interpolated similarly

15
Context
  • Every operation in Perl is evaluated in one of
    two contexts scalar or list
  • Assignment to a scalar lvalue will cause the
    right side to be evaluated in scalar context
  • Assignment to an arrary, hash, or a slice lvalue
    will cause the right side to be evaluated in list
    context
  • Assignment to a list on the left will cause the
    right side to be evaluated in list context
  • Some operations return different values depending
    on the context in which they are evaluated
  • number_of_matches m/(,)/
  • _at_numbers m/(,)/

16
List Values
  • A list consists of values enclosed in ( ) and
    separated by commas
  • _at_array (1,3,5,7,9,11)
  • In list context the above example loads the array
    with the values
  • In a scalar context, each value is evaluated and
    the last value is returned, value 11 below
  • value (1,3,5,7,9,11)
  • There is an important difference between a list
    and an array, when an array is evaluated in
    scalar context it returns its length, length
    6
  • length _at_array
  • length scalar _at_array
  • length _at_array 0

17
List Values
  • List interpolation
  • (_at_array1, _at_array2, 1)
  • Each element above is evaluated and inserted into
    the list that is generated
  • There are no lists of lists
  • Lists can be indexed using
  • (day,month,year) (localtime())3..5
  • Lists may be used as lvalues (see above)

18
Arrays and Context
  • An array when referenced using _at_ operates in a
    list context
  • An array element operates in a scalar context
  • When a list is assigned to an array each value is
    inserted into the next element
  • Special forms of arrays
  • length scalar _at_array
  • last_index array
  • scalar _at_array array 1

19
Hashes and Context
  • A hash when referenced in the form operates in
    list context
  • A hash element operates in a scalar context
  • When a list is assigned to a hash each pair of
    values in the list is taken as a key-value pair
  • colors (red,0xff0000,green,0x00ff00,blue,
    0x0000ff)
  • There is a special syntax available for this
  • colors (red gt 0xff0000, green gt 0x00ff00,
    blue gt 0x0000ff)
  • Use the keys function to generate a list of keys
    for a hash
  • To find the number of keys in a particular hash
  • number_of_keys scalar keys hash

20
Filehandles and Input
  • A filehandle is a name that refers to a file
  • Filehandles are normally all upper case
  • STDIN, STDOUT, STDERR are predefined
  • Use ltgt operator to read from a filehandle
  • line ltSTDINgt read one line from STDIN
  • _at_lines ltSTDINgt read all lines from STDIN
  • Read and print entire STDIN
  • while(ltgt) print

21
Operators
Terms and list operators
-gt
--

! \ unary unary -
!
/ x
- .
ltlt gtgt
Named unary operators
lt gt lt gt lt gt le ge
! ltgt eq ne cmp




.. ...
? (ternary)
- (etc)
, gt
List operators
not
and
or xor
  • Operator precedence
  • Operators can be overloaded when using objects

22
Simple Statements
  • A simple statement is an expression that is
    evaluated
  • A simple statement is terminated with a
  • A simple statement may be followed by a modifier
  • if expr
  • unless expr
  • while expr
  • until expr
  • foreach list
  • Examples
  • print Value is i\n if i gt 5
  • print ii-- \n while i ! 0

23
Compound Statements
  • Expressions containing blocks
  • A block is normally contained in
  • if statement
  • if (expr) block
  • if (expr) block else block
  • if (expr) block elsif (expr) block
  • if (expr) block elsif (expr) block else block
  • unless statement is similar

i max if (i max) print The max
is five\n exit else i
  • i max
  • unless (i max)
  • i
  • else
  • print The max is five\n
  • exit

24
Compound Statements
  • while statement
  • label while (expr) block
  • label while (expr) block continue block
  • until statement
  • label until (expr) block
  • label until (expr) block continue block

while (ltSTDINgt) chomp _at_fields
split(//) print Field 1
fields0\n
25
Compound Statements
  • for loop
  • label for (expr1 expr2 expr3) block
  • expr1 start condition
  • expr2 ending condition
  • expr3 loop statement

for (my i 0i lt 10i) print
ii\n
26
Compound Statements
  • foreach statement
  • label foreach (list) block
  • label foreach var (list) block
  • label foreach var (list) block continue block
  • Loops over each entry in the list
  • When var is omitted then _ is used

foreach my key (sort keys people) print
Key key, Valuepeoplekey\n
foreach my entry (_at_items) print Item
entry\n
27
Compound Statements
  • Labeled block
  • label block
  • label block continue block
  • Equivalent to a single iteration loop
  • Can be used with last, next, and redo

28
Loop Control
  • These statements can be used with blocks
  • The optional label further refines their effect
  • last label
  • Exit the loop (block)
  • The continue block is not executed
  • next label
  • Skip the rest of this iteration and start the
    next iteration
  • Execute the continue block before the next
    iteration begins
  • redo label
  • Restart the loop with the current iteration
    parameters
  • The continue block is not executed
  • The label parameter enables multi-level block
    control

29
Declarations
  • Subroutine declaration is a global declaration
  • Must declare a subroutine before using it
  • sub count
  • Can define a subroutine at declaration
  • sub count
  • Pragmas are directives to the Perl compiler
  • use strict
  • use integer
  • use warnings

30
Declarations
  • Variable declarations
  • Lexically scoped declarations
  • my var
  • my (var1, var2)
  • my value function()
  • Lexically scoped global declarations
  • our var
  • Dynamically scoped global declarations
  • local var

31
Pattern Matching
  • Regular Expressions
  • Rule based pattern matching mechanism
  • Simple patterns
  • m/Class/
  • Complex pattern
  • m/AE0-9A-Z/

32
Regular Expressions
  • Meta-characters
  • \ ( ) ? .
  • Have special meanings inside patterns
  • \ is the escape character used to use one of the
    meta-characters as itself in a pattern, eg, \\ or
    \.
  • Quantifiers
  • ? 3 2,5
  • REs normally match maximal text
  • Add ? to end to match minimal text
  • Character classes
  • or
  • Grouping
  • ( )

33
Regular Expressions
  • The pattern matching operators
  • m// match
  • s/// substitute
  • tr/// transliterate
  • Binding operators
  • binds string to pattern operator
  • !
  • Examples
  • string m/AE0-94A-Z/
  • string s/old/new/
  • string s(old)(new) can use arbitrary
    delimiters
  • string soldnew

34
Regular Expressions
  • Maximal and Minimal matches
  • exasperate m/e(.)e/
  • Returns xasperat
  • exasperate m/e(.?)e/
  • Returns xasp
Write a Comment
User Comments (0)
About PowerShow.com