Lecture%20Seven%20(II) - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture%20Seven%20(II)

Description:

Perl is an interpreted language for scanning files, extracting ... y = 'Con' . 'cat' . 'enation!n'; print $y; 9/22/09. COEN276 Winter 98 John Xiao. 26 ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 33
Provided by: john51
Learn more at: https://www.cse.scu.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture%20Seven%20(II)


1
Lecture Seven (II)
  • Perl Programming

2
Introduction
  • Perl is an interpreted language for scanning
    files, extracting information from those text
    files, and printing reports based on that
    information.
  • Perl is mainly used to handle text files, but can
    be used for binary files (databases), process
    manipulation, network tasks.

3
Introduction
  • Perl is not a strong-typed language, like C, C,
    Java
  • Perl does not arbitrarily impose limit on data
    structures. Strings have unlimited length.
    Recursion is of unlimited depth.
  • Information source http//www.perl.com/perl/faq/,
    http//www.phlab.missouri.edu/cgi-bin/perlman

4
A simple example
  • perl -e print hello dolly\n
  • hello dolly
  • "-e" is followed by Perl command (execute
    command line instead of reading from a script
    file).

5
Compared with sed and awk
  • Unlike sed and awk, where a sequence of commands
    is executed for each line of input, Perl executes
    each command just once.
  • Use the -n option to implicitly loop over a file,
    or use an explicit loop to iterate the file.

6
Another simple example
  • cat foo
  • this is the first line
  • second line
  • perl -ne print foo
  • -n option is used to implicitly loop through
    the file a line at a time.
  • this is the first line
  • second line

7
Example continued
  • perl -ne print if /this/ foo
  • this is the first line
  • date perl -ne print Today is _
  • _ is a variable referring to input
  • Today is October 15, 1997

8
A Simple Script - foo.perl
  • !/usr/contrib/bin/perl -w
  • This is a comment line
  • print Hello\n
  • perl -c foo.perl
  • foo.perl syntax ok
  • foo.perl

9
Perl IO
  • PERL works with stdin, stdout, stderr, but does
    not access the streams directly
  • Filehandles STDIN, STDOUT, STDERR
  • Printing output
  • any unknown word (not a filehandle, label,
    reserved word) is treated as if surrounded by
    single quotes
  • perl words ARE case sensitive
  • single quotes - all characters treated as
    literals
  • double quotes - all chars literal except variable
    substitution, escape sequences

10
Special Literals
  • \t tab
  • \n newline
  • \\ backslash
  • special literals (should not be double quoted)
  • _LINE_ current line number
  • _FILE_ current filename

11
IO
  • print Hello, world, \n
  • HelloWorld
  • print Hello world\n
  • Hello world
  • print Hello, world,\n no comma allowed after
    filehandle (ERROR filehandle must be specified
    when literals not quoted)
  • print STDOUT Hello, world,\n
  • Helloworld

12
IO
  • print The price is 100.\n
  • The price is . (looks for scalar variable 100)
  • print The price is \100.\n
  • The price is 100.
  • print The price is ,100,.\n
  • The price is 100
  • print The number is ,0777,.\n
  • The number is 511 (octal due to leading 0)

13
IO
  • print The number is ,0xAbcF,.\n
  • The number is 43983 (hex due to leading 0x)
  • print The unformatted number is ,14.56,.\n
  • The unformatted number is 14.5600000. (use
    default format)
  • print \tIn double quotes\t\n
  • In double quotes
  • print \tIn single quotes\t\n
  • \tIn single quotes\t\n

14
IO
  • print Current line number is ,_LINE,\n
  • Current line number is 7 (interpret, literal
    not in quotes)

15
Printf print formatted string
  • printf(Names s\t Number d\n,John,50)
  • Name John Number 50
  • c - character
  • s - string
  • d - decimal
  • x - hex
  • o - octal
  • e - floating point, scientific notation
  • f - floating point

16
Three Types Variables
  • Scalars can be numeric or character as determined
    by context
  • 123 12.4 5E-10 0xff (hex) 0377 (octal)
  • 'What you see is (almost) what \n you get
  • 'Don\'t Walk
  • "How are you?"
  • "Substitute values of x and \n in \" quotes.
  • date
  • uptime -u
  • du -sk filespec sort -n
  • x
  • list_of_things5
  • lookup'key'

17
Three Types Variables
  • Double-quotes allow substitution of variables and
    control codes.
  • Back-quotes also allow substitution, then try to
    execute the result as a system command, returning
    as the final value whatever the system command
    outputs.
  • Arrays of scalars (also called lists) are
    sequentially-arranged scalars
  • _at_dow('Sunday', 'Monday', 'Tuesday')
  • (13,14,15,16,17,18,19) equivalent to (13..19)
  • (13,14,15,16,17,18,19)2..4 equivalent to
    (15,16,17)
  • _at_dow the entire array

18
Three Types Variables
  • Associative arrays (also called hashes)
    facilitate arbitrary recall
  • states(CA,California,ME,Maine,.)
  • statesME

19
Example
  • salary 5000
  • _at_months (Mar, Apr, May)
  • states (CA,California,ME,Maine,MT,Mon
    tana)
  • print salary\n
  • print _at_months\n
  • print months0\n
  • print statesCA\n
  • print x 3,\n x initialized to 0 since
    context is numeric
  • print name\n name initialized to empty
    string due to context
  • 5000
  • Mar Apr May
  • Mar
  • California
  • 3

20
Name Convention
  • Scalar variables start with '', even when
    referring to an array element. The variable name
    reference for a whole list starts with '_at_', and
    the variable name reference for a whole
    associative array starts with ''.
  • Lists are indexed with square brackets enclosing
    a number, normally starting with 0. In Perl 5,
    negative subscripts count from the end. Thus,
    things5 is the 6th element of array _at_things,
    and ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')1
    equals 'Mon'.
  • Associative arrays are indexed with curly
    brackets enclosing a string. whatever,
    _at_whatever, and whatever are three different
    variables.

21
Name Convention
  • Examples
  • _at_days (31,28,31,30,31,30,31,31,30,31,30,31)
  • A list with 12 elements.
  • days Last index of _at_days 11 for
    above list
  • days 7 shortens or lengthens list _at_days
    to 8 elements
  • _at_days (days0, days1,... )
  • _at_days3,4,5 (30,31,30)
  • _at_days'a','c' same as (days'a',days'c')
  • days (key1, value1, key2, value2,
    ...)

22
Name Convention
  • Case is significant--
  • FOO, Foo and foo are all different variables.
  • If a letter or underscore is the first character
    after the , _at_, or , the rest of the name may
    also contain digits and underscores. If this
    character is a digit, the rest must be digits.
  • Perl has several dozen special variables whose
    second character is non-alphanumeric. For
    example, / is the input record separator,
    newline "\n" by default. An uninitialized
    variable has a special "undefined" value which
    can be detected by the function defined().
    Undefined values convert depending on context to
    0, null, or false.

23
Name Convention
  • The variable "_" Perl presumes when needed
    variables are not specified. Thus
  • ltSTDINgt assigns a record from
    filehandle STDIN to _
  • print prints the curent value of
    _
  • chop removes the last character
    from _
  • _at_things split parses _ into white-space
    delimited words, which become successive
  • elements of list _at_things.

24
Name Convention
  • Subroutines and functions are referenced with an
    initial '', which is optional if reference is
    obviously a subroutine or function such as
    following the sub, do, and sort directives sub
    square return _0 2 print "5 squared is
    ", square(5)
  • Filehandles don't start with a special character,
    and so as to not conflict with reserved words are
    most reliably specified as uppercase names
    INPUT, OUTPUT, STDIN, STDOUT, STDERR, etc.

25
Number and Characters
  • Example
  • !/usr/local/bin/perl
  • print '007',' has been portrayed by at least ',
    004, ' actors. '
  • print 73, ' ', 73, ' ', 7/3, ' ', 73, ' ',
    73, ' '
  • x 7
  • print x
  • print ' Doesn\'t resolve variables like x and
    backslashes \n. '
  • print "Does resolve x and backslash\n"
  • y "A line containing x and ending with line
    feed.\n"
  • print y
  • y "Con" . "cat" . "enation!\n"
  • print y

26
Another Example
  • !/usr/contrib/bin/perl -w
  • if (ARGV gt 0) no args -1
  • who join(' ', _at_ARGV)
  • else
  • who 'World'
  • print "Hello, who!\n"

27
Substitution Example
  • Substitutes Text In Multiple File
  • perl -e 's/gopher/World Wide Web/gi' -p -i.bak
    .html
  • "-e execute command line instead of reading
    from a file
  • "s substitutes "gopher" with "World Wide Web
  • "g" is global across each line,
  • "i" turn off case sensitiveity.
  • "-i make a copy of the file with the name
    ltoriginal file namegt.ltextension givengt
  • "-p loop through the files line by line

28
Another Example
  • !/usr/local/bin/perl -w
  • original'gopher'
  • replacement"World Wide Web"
  • nchanges 0
  • undefine /
  • for (i0 i lt ARGV i)
  • if (! open(INPUT,"lt ARGVi ") )
  • print STDERR "Can't open input file
    bakfile\n"
  • next
  • dataltINPUTgt Read input file as one
    long record.
  • close INPUT

29
continued
  • if (data s/original/replacement/gi)
  • bakfile "file.bak"
  • if (! rename(file,bakfile)) die
    "Can't rename file !"
  • if (! open(OUTPUT,"gtfile") ) die
    "Can't open output file file\n"
  • print OUTPUT data
  • close OUTPUT
  • print STDERR "file changed\n"
  • nchanges
  • else print STDERR "file unchanged\n"
  • print STDERR "nchanges files changed.\n"
  • exit(0)

30
Expressiveness Sometimes Means Confusing
  • The same expression can be written in many
    different ways in Perl
  • if (x 0) y 10 else y 20
  • y x0 ? 10 20
  • y 20 y 10 if x0
  • unless (x 0) y20 else y10
  • if (x) y20 else y10
  • y (10, 20)x ! 0

31
Comparison
  • Output
  • Hello
  • x is 7
  • x is now 5
  • Use eq, ne, lt, gt, etc for strings.
  • x 'operator'
  • print ltltTHATSALL
  • Hello
  • THATSALL
  • x 7
  • if (x 7) print "x is x\n"
  • if (x 5) print "x is now x\n
  • x 'stuff'
  • if (x eq 'stuff')
  • print "Use eq, ne, lt, gt, etc for
    strings.\n"

32
Using Arrays
  • _at_stuff ('This', 'is', 'a', 'list.')
  • print "So \stuff1 stuff1, ", "and
    \stuff stuff.\n"
  • print _at_stuff,"\n"
  • print join('...',_at_stuff),"\n"
  • splice(_at_stuff, 3, 0, ('fine', 'little'))
  • replace 0 elements from index 3 by 'fine'...
  • print join('...',_at_stuff),"\n"
  • Output
  • So stuff1 is, and stuff 3.
  • Thisisalist.
  • This...is...a...list.
  • This...is...a...fine...little...list.
Write a Comment
User Comments (0)
About PowerShow.com