PERL - PowerPoint PPT Presentation

About This Presentation
Title:

PERL

Description:

Understand constants and variables. Understand operators. Understand ... Inside the program, they are stored in array _at_ARGV. Command Line Arguments. Example: ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 44
Provided by: RU
Category:
Tags: perl | argv

less

Transcript and Presenter's Notes

Title: PERL


1
PERL
  • By C. Shing
  • ITEC Dept
  • Radford University

2
Objectives
  • Understand the history
  • Understand constants and variables
  • Understand operators
  • Understand control structures
  • Understand functions
  • Understand file I/O
  • Understand command line arguments
  • Understand how to use Perl for system
    administration

3
History
  • Perl (Practical Extraction Report Language) by
    Larry Wall (1986)
  • Language between C and shell Scripting , need
    interpreter
  • Provide File access and Unix utilities
  • Download site http//www.perl.com
  • File extension (.pl)
  • Interpret with error message
  • perl -w filename.pl arg0 arg1
  • Artistic license variation of GNU public
    license, free

4
Constants
  • Scalar
  • Number integer, real, scientific notation
  • String within
  • Boolean
  • 1 represents true
  • 0 represents false

5
Variables
  • Scalar begin with
  • Number integer, real
  • String string, character
  • Array begin with _at_
  • Index starts from 0
  • Associative begin with
  • use hash

6
Control Structures
  • Print
  • Assignment
  • Conditional
  • Loop
  • Function (Subroutine)

7
Control Structures - Print
  • Syntax each statement ends with
  • Print variable constant
  • Example
  • print "Please enter a number"

8
Control Structures - Assignment
  • Syntax each statement ends with
  • Variable expression
  • Comment begins with

9
Control Structures - Assignment
  • Scalar Variable
  • Number integer, real
  • Example
  • i5
  • j-6.2
  • String string, character
  • Example
  • monthJanuary
  • char\t

10
Control Structures - Assignment
  • Scalar Variable
  • Number integer, real
  • Example
  • i5
  • j-6.2
  • number ltSTDINgt (keyboard input)
  • String string, character
  • Example
  • monthJanuary
  • char\t
  • guess ltSTDINgt (keyboard input)
  • fileString ltFILEHANDLEgt (getting data
    from file)

11
Control Structures - Assignment
  • Array Variable
  • Example
  • _at_array ("john", "susan", "david", "sarah",
    "ryan", "laura")
  • _at_numRange (1..6)
  • Where
  • array0 john
  • array1 susan
  • array5 laura

12
Control Structures - Assignment
  • Associative Variable
  • Example
  • hash ("smith"gt"john", "sides"gt "susan",
    "davis"gt"david", "star"gt"sarah",
    "rush"gt"ryan","lane"gt "laura")
  • Where
  • arraysmith john
  • arraysides susan
  • arraylane laura

13
Operators Arithmetic, Logic, Relational
  • Arithmetic ,-,,/
  • Logical ,
  • Relational
  • Numeric , !, gt, gt, lt, lt
  • String eq, ne, gt, ge, lt, le

14
Operators - String
  • String
  • Concatenation .
  • Example
  • month month.char
  • message "This is a number ".\t.number.
    "\n"
  • Match
  • Example
  • line s/the/THE/g (replace all the by THE
    in the line)
  • line /(\w)(\s)(\w)(\s)/ (line that
    matches the pattern word, spaces, word

15
Control Structures - Conditional
  • Syntax
  • If (condition)
  • If (condition)
  • else
  • If (condition1)
  • elsif (condition2)
  • else

16
Control Structures - Conditional
  • Example
  • if (guess eq "hello")
  • print "The word ".guess." that you guessed
    is correct.\n\n"

17
Control Structures - Conditional
  • Example
  • if (avg gt0.895avgall)
  • grade"A"
  • elsif (avg gt0.795avgall)
  • grade"B"
  • elsif (avg gt0.695avgall)
  • grade"C"
  • elsif (avg gt0.595avgall)
  • grade"D"
  • else

18
Control Structures - Loop
  • While
  • Until
  • Foreach
  • For

19
Control Structures While Loop
  • Syntax
  • while (condition)
  • when condition is false, exit loop

20
Control Structures While Loop
  • while (lineltUSERFILEgt)

21
Control Structures Until Loop
  • Syntax
  • until (condition)
  • when condition is true, exit loop

22
Control Structures Until Loop
  • Example
  • until (array20 eq "listing")

23
Control Structures Foreach Loop
  • Syntax
  • foreach variable (variableRange)
  • when variable is outside of variableRange, exit
    loop

24
Control Structures Foreach Loop
  • Example
  • foreach month (1..12)

25
Control Structures For Loop
  • Syntax
  • for (initialization condition ending body
    stmt)
  • when condition is false, exit loop

26
Control Structures For Loop
  • Example
  • for (i1 ilt12 i)

27
Control Structures - Function (Subroutine)
  • Library function
  • User (-defined) function

28
Control Structures Library Function
  • lc() or lc convert to lower case
  • uc() convert to upper case
  • open(), close(), die(), exit(), last() See File
    I/O
  • print()
  • length() length of string
  • index()

29
Control Structures Library Function
  • Example
  • search item through list
  • foreach user (_at_list)
  • if (user eq lc(item))
  • user_correct 1
  • foreach

30
Control Structures Library Function
  • keys() gives the key value of the hash
  • chomp() delete the new line character from
    variable
  • split (delimiter, variable) split a variable
    using delimiter
  • my() create local variable
  • shift (arrayName) get rid of 1st element in the
    arrayName
  • push (array1, array2) push array2 to the end of
    array1

31
Control Structures Library Function
  • Example
  • while (lineltNAMEFILEgt)
  • chomp(line)
  • username are separated by space(s)
  • _at_arraysplit /\s/, line

32
Control Structures Library Function
  • Example
  • foreach userkey (keys hash)
  • if (hashuserkey eq lc(name))
  • user_correct 1
  • lastnameuserkey
  • foreach

33
Control Structures User Function
  • Function call
  • Syntax
  • functionname (actualParameterList)
  • Example
  • check (name, _at_array)

34
Control Structures User Function
  • Function definition
  • Syntax
  • sub functionname
  • my localVar1 _0
  • my localVar2 _1
  • my _at_localArray _at__

35
Control Structures User Function
  • Function definition
  • Example
  • check (name, _at_array)
  • sub check
  • the following 3 lines declare local
    variables
  • my item _0
  • my _at_list _at__
  • my user_correct 0

36
Control Structures User Function
  • Example
  • printArray(_at_array)
  • sub printArray
  • _at_array is a local variable in the sub
  • my _at_array _at__
  • foreach cell (_at_array)
  • print cell."\t"
  • print "\n"
  • sub printArray

37
File I/O
  • Open/Close file
  • Syntax
  • open (FILEHANDLE, ltfilename)
  • close (FILEHANDLE)

38
File I/O
  • Open/Close file
  • Example
  • read all names into _at_array from input data
    file sixth2.txt
  • open (NAMEFILE, "ltsixth2.txt")
  • die "Error Unable to open the file !"
  • open output file for print
  • open (OUTFILE,"gtattend.txt")
  • print OUTFILE "Lastname\t\tFirstname\t\tE-Mail
    Name\n"

39
Command Line Arguments
  • perl w filename.pl arg0 arg1 arg2
  • Arg0, arg1 and arg2 are command line arguments
  • Inside the program, they are stored in array _at_ARGV

40
Command Line Arguments
  • Example
  • open (USERFILE, "ltuserARGV0.txt") die
    "Error Unable to open the file !"
  • open (OUTFILE,"gtattendARGV0.txt")
  • print OUTFILE "Library Attendance for ITEC
    100-ARGV0\n\n"

41
System Administration
  • Call system library when using shell commands in
    Perl
  • Give full path name for shell commands
  • Use which command to find full path name
  • Watch for reserved names in Perl

42
System Administration
  • Example Run Perl in Unix environment
  • system("/usr/local/gnu/bin/awk 'print
    \1,\2,\9' temp1.txt gt temp2.txt")
  • system("/usr/bin/rm -f temp2.txt")
  • If you run Perl in Unix, make sure to change data
    into Unix format, such as
  • system("/usr/bin/dos2unix session.txt
    session.txt")

43
Reference
  • Some simple examples
Write a Comment
User Comments (0)
About PowerShow.com