Perl Programming Language - PowerPoint PPT Presentation

1 / 62
About This Presentation
Title:

Perl Programming Language

Description:

Facilitates very quick program development -- many problems can be solved very ... printargs('frog', 'and', 'toad'); # Prints 'frog and toad' sub printfirsttwo ... – PowerPoint PPT presentation

Number of Views:1196
Avg rating:3.0/5.0
Slides: 63
Provided by: WSE995
Category:

less

Transcript and Presenter's Notes

Title: Perl Programming Language


1
Perl Programming Language
  • Software Project
  • June 7-8
  • Some slides are adapted from
  • Efi Fogel (Tel-Aviv university)
  • Jason Stajich (Duke University)
  • http//www.id.cbs.dk/dh/perl/perlintro.html

2
Why Perl?
  • Practical Extraction and Report Language
  • SIMPLE scripting language
  • FAST text processing
  • Cross-platform
  • Facilitates very quick program development --
    many problems can be solved very quickly with
    surprisingly short programs.
  • Designed to work smoothly in the same ways that
    natural language works smoothly

3
Process Overview
C/C
C Libraries
Hello.c
Hello.o
Edit
Hello
Run
Hello
Compile
Link
Perl
Perl Packages
Executable
Hello.pl
Interpret and Run
Hello
Edit
4
Perl "hello" example
  • Simply say print "Hello world!\n"
  • To execute perl -e "print \"Hello world\!\n\""
  • Or, edit the file hello to contain the one-liner
    program above, and type perl hello

!/usr/bin/perl print Hello world\n
5
Executing Perl
  • The perl interpreter is usually located in
    /usr/bin/perl. !/usr/bin/perl - tells the shell
    to look for perl program and pass the rest of the
    file to if for execution.
  • Use strict - strict error checking
  • Generates compile-time error if you use a
    bareword identifier that's not a predeclared
    subroutine.
  • Generates compile-time error if you access a
    variable that wasn't declared via my, or wasn't
    imported.
  • Generates runtime error if you use any symbolic
    references.

!/usr/bin/perl use strict The body of the
script print hello world\n
Can be also /usr/local/bin/perl. Check with
which perl
6
Perl Documentation
  • Run man perl or perldoc perl to read the
    top-level page.If you know which section you
    want, you can go directly there by using man
    perlvar or perldoc perlvar.
  • Other online tutorials
  • E.g http//www.perldoc.com/perl5.6/pod/func/

7
Perl on Windows ActivePerl
  • ActivePerl distribution (developed by
    ActiveState Tool Corporation ) is
    athttp//www.activestate.com/.
  • Perl for Win32 - The ActivePerl binary comes as
    a self-extracting executable that uses the
    standard Win32 InstallShield setup wizard to
    guide you through the installation process.
  • By default, Perl is installed into the directory
    C\Perl\version, where version is the current
    version number (e.g., 5.005).

8
Perl Variables
9
Variable Localization and Scope
  • Perl variables can be either global or local
  • x 1
  • default global scope (a kind of static scope)
    visible throughout program
  • my y 2
  • my lexical scope (a kind of static scope)
    visible only inside its block (like in C)
  • local z 3
  • local dynamic scope visible inside its block
    and any subroutines called from that block
  • Note that value of dynamic scope variable (z)
    will depend on the order of the subroutine calls

10
Scalar Variables
  • Scalar variables can be strings or numbers
  • For example
  • var1 "aloha"
  • var2 5
  • Can use the same operators as C or Java
  • var2
  • Can also include them in a single print statement
  • print "var1 and var2" aloha and 6

11
Scalar Variables
  • You do not have to declare variables in PERL.
  • The first character of a variable indicates its
    type.
  • means that the variable is a scalar, which can
    be a string, an integer or a real number.
  • Scalar is initialized to 0 or null string.

the_string "hello world" print
the_string,"\n"
12
Operators
  • - (addition, subtraction)
  • / (multiply, divide, modulus,
    exponentiation)
  • -- (autoincrement, autodecrement)
  • - etc. (assignment operators)

13
Logical Operators
  • Logical operators are evaluated from left to
    right. condition1 condition2 condition3 -
    condition2 condition3 is evaluated first, then
    the result of that evaluation is used in the
    evaluation.

14
Function substr, x, .
  • Returns a substring of a string
  • substr(string, offset, length)
  • Returns a substring of string starting at
    specified offset of specified length

string "Perl Programmer"
15
String Operators
16
Some string functions
  • reverse - reverse a string (or array)
  • length - get length of a string
  • uc - uppercase or lc - lowercase a string

17
Arrays
  • Arrays do not have to be predeclared and their
    size have to be specified. Arrays hold scalar
    values.
  • A variable beginning with the character _at_ is an
    array. To reference the first item of the array
    _at_x, we use x0 (x0 is scalar).

18
Arrays
  • For an array _at_x, the special variable x
    indicates the highest index used in the array.
    So, xx will be the last element in array _at_x.

Equivalent
19
Arrays
  • Strings and arrays are closely related and there
    are two functions for converting from one to the
    other.

Reverse operation
20
Perl - split
  • split /PATTERN/, EXPR
  • Scans a string given by EXPR for delimiters, and
    splits the string into a list of substrings,
    returning the resulting list value in list
    context, or the count of substrings in scalar
    context (token1, token3, token4) split("
    ", line)

21
_at_ARGV Special Variable
  • Stores command line arguments
  • ./program one 2 "three"
  • ARGV0 one
  • ARGV1 2
  • ARGV1 three

22
Control Structures
  • PERL's control structures are similar to those in
    C, except that all blocks must be bracketed.

if (x 1) print "ONE\n" elsif (x
2) print "TWO\n" else print
"OTHER\n"
23
Common mistakes
  • The while loop requires correct initialization
    and control expression watch out for array
    bounds, e.g for (I 0 I lt array I)
  • print arrayI
  • When you traverse an array in a loop, make sure
    that the index doesnt get out of bounds results
    in an error message.

24
Foreach Loop
  • foreach loop iterates over a list value and sets
    the control variable (var) to be each element of
    the list in turn
  • foreach var (list) ...If VAR is omitted, _
    is used. If LIST is an actual array, you can
    modify each element of the array by modifying VAR
    inside the loop (VAR is an alias for each item in
    the list that you're looping over.

25
Loop Control
  • The last command is like the break statement in C
    .
  • The next command is like the continue statement
    in C.
  • Any block can be given a label (by convention, in
    uppercase) which identifies the loop.

WID foreach this (_at_ary1) JET foreach
that (_at_ary2) if (this gt that)
this that
26
Hashes
  • A hash is a structure of key, value pairs.
  • A variable beginning with the character x is a
    hash.
  • x"dog" - returns the value (a scalar)
    associated with the key "dog". x"dog"
    "cat" - sets the value associated with "dog" to
    be "cat".
  • Hashes use curly brackets (x2), while arrays
    use square brackets (x2). x2 returns the
    value stored with the key 2 in the hash x,
    x2 returns the third element of the array _at_x.
  • Before a key is inserted into a hash, the value
    associated with that key is 0 or the empty
    string.

27
Hashes
  • A code to count the number of even numbers in
    0..10

for (count0countlt10count) if count
mod 2 is 0, then even if (count 2 0)
thearray"EVEN" print "There were
",thearray"EVEN", " even numbers\n"
28
Hashes
  • To print out all of the keys and values in a
    hash
  • Use when talking about the entire hash.
  • Use for specific values in the hash (e.g.
    thearray"EVEN")

29
Basic I/O
a ltSTDINgt read the next line _at_a ltSTDINgt
all remaining lines as a list, until
ctrl-D while (lineltSTDINgt)
chomp(line) other operations with line
here
30
Perl File Processing
  • open(FILE_HANDLE, "ltfile_name")
  • Open a file for reading
  • my _at_array ltFILE_HANDLEgt
  • Assign file contents to an array
  • 1st line array0, 2nd line array1, etc.
  • open(APPLE, "gtbanana")
  • Open file for writing
  • print APPLE _at_array
  • Write contents of array to a file
  • close(APPLE)
  • Close file

31
Perl - open
  • open FILEHANDLE, EXPR
  • open FILEHANDLE
  • !/usr/local/bin/perl
  • Program to open the password file, read it in,
  • print it, and close it again.
  • file '/proc/cpuinfo' Name the file
  • open(INFO, file) Open the file
  • _at_lines ltINFOgt Read it into an array
  • close(INFO) Close the file
  • print _at_lines Print the array

32
The Implicit Variable
When a line is read, it is stored in the special
variable _. This is a program to print all lines
from a file.
open(FILE1,ARGV0) while(ltFILE1gt) print
_ close(FILE1)
  • To read from stdin, we do not need to call open

while(ltSTDINgt) print _
33
Comparing Files
open(FILE1,ARGV0) open(FILE2,ARGV1)
while(ltFILE1gt) line_from_2 ltFILE2gt if
(_ eq line_from_2) print _
close(FILE1) close(FILE2)
34
Perl - readline
  • readline EXPR
  • Reads from the filehandle contained in EXPR.
  • line ltSTDINgt line readline(STDIN)
    same thing

35
File Test Operators
-e "/usr/bin/perl" or warn "Perl is improperly
installed\n" -f "/vmunix" and print "Congrats,
we seem to be running BSD Unix\n"
36
Perl Subroutine
When the subroutine is called any parameters are
passed as a list in the special _at__ list array
variable.
sub printargs print "_at__\n"
printargs("perly", "king") Prints "perly
king" printargs("frog", "and", "toad")
Prints "frog and toad"
The elements of _at__ can be accessed as a regular
array.
sub printfirsttwo print "Your first argument
was _0\n" print "and _1 was your
second\n"
37
Perl Subroutine
Result of a subroutine is always the last thing
evaluated. This subroutine returns the maximum of
two input parameters.
sub maximum if (_0 gt _1) _0
else _1 biggest
maximum(37, 24) Now biggest is 37
The printfirsttwo subroutine above also returns
a value, in this case 1. This is because the last
thing that subroutine did was a print statement
and the result of a successful print statement is
always .
38
Regular Expressions
while (line ltFILEgt) if(line /http/)
match operator // pattern binding
operator print line prints all
lines from FILE that include substring http
The pattern binding operator looks for a match
of the regular expression on the right of the
operator (//) in the string to the left of the
operator (..).
39
Regular Expressions
  • \s matches a space or tab
  • matches the start of a string
  • matches the end of a string
  • a matches the letter a
  • a matches 1 or more a's
  • a matches 0 or more a's
  • (ab) matches 1 or more ab's
  • abc matches a character that is not a or b or
    c
  • a-z matches any lower case letter . matches any
    character

40
Regular Expressions
  • To test whether a string in x contains the
    string "abc", we can use
  • if (x /abc/) . . .
  • To test whether a string begins with "abc",
  • if (x /abc/) . . .
  • To test whether a string begins with a capital
    letter if (x /A-Z/) . . .
  • To test whether a string does not begin with a
    lower case letter if (x /a-z/) . . .
  • In the above example, the first matches the
    beginning of the string, while the within the
    square brackets means "not".

41
Regular Expressions
  • We can change strings using a command of the
    form s/FROM/TO/options where FROM is the
    matching regular expression and TO is what to
    change this to. options can either be blank (for
    the first match) or it can be g, meaning do it
    globally.
  • To change all a's to b's in the string in
    variable x x s/a/b/g
  • To change the first a to b x s/a/b/
  • To change all strings of consecutive a's into one
    a x s/a/a/g
  • To remove all strings of consecutive a's x
    s/a//g
  • To remove blanks from the start of a stringx
    s/\s//g

42
Perl Regular Expressions
http//www.cs.tut.fi/jkorpela/perl/regexp.html
43
Regular Expressions - Examples
http//www.cs.tut.fi/jkorpela/perl/regexp.html
44
Regular Expressions - Examples
Inside a bracket has the meaning of "not"
http//www.cs.tut.fi/jkorpela/perl/regexp.html
45
Regular Expressions - Examples
  • /a-z\s\d/ match a lowercase word, at
    least some space, and any number of digits
  • /(\bhaiout\b)/ match one or more
    occurrences of the word "hat", "hit", "hot", or
    "hut" at the beginning of a string
  • _a-zA-Z0-9-(\._a-zA-Z0-9-) match at
    least one valid character followed by zero or
    more sets consisting of a period and one or more
    valid characters.

_a-z0-9-(\._a-z0-9-)_at_a-z0-9-(\.a-z0-9-
)
46
The Midterm Exam
47
Question 2
  • int main(void)
  • int a1,b2, c3
  • what_happens(a, b, c)
  • printf("ad bd cd\n", a, b, c)
  • return 0
  • void what_happens(int a, int b, int c)
  • c a
  • a b
  • b c
  • printf("ad bd cd\n", a, b, c)
    return

48
  • void what_happens(int a, int b, int c)
  • c a
  • a b
  • b c
  • printf("ad bd cd\n", a, b, c)
    return

in main
0012FF7C
0012FF78
0012FF8C
a
1
b
2
c
3
0012FF7C
0012FF78
0012FF8C
0012FF7C
Printed 2 1 1
49
Lets Complicate It
50
  • int do_balagan(int a, int b)
  • int c
  • c b
  • b a
  • a c
  • a a 5
  • b a b
  • printf("In balagan\n")
  • printf("ad bd cd\n", a, b, c)
  • return c

12
17
0012FF7C
0012FF78
51
Back to the main() function
  • int main(void)
  • int a,b
  • int c
  • a5
  • b7
  • c do_balagan(a, b)
  • printf("In main\n")
  • printf("ad bd cd\n", a, b, c)
  • return 0

52
Question 3
c1
122 5
1
real
image
2
abs1 14 5
c2
abs2 925 34
3
real
image
5
53
Question 4
Iteration 1initial a2, count 0final count
12 2
Iteration 2initial a2, count 2final
count 32 6
54
Question 5
n is the number of bits in a byte
Set mask to 10000000
Prints the integer a
Prints the binary value of an integer a
(condition) ? (if true) (if false) if (amask
0) putchar(0) else putchar(1)
Multiply a by 2!!! (i.e. a 1,2,4,8) In iteration
4 a 8
55
Question 6
Pointer to the next entry
If it is the beginning if a new word increment
the count
If there is only one word in s.
56
Question 7
Access to a freed pointer!!!
57
Binary and Text Files
  • File can be opened and processed as either text
    file or binary file
  • Text files
  • contain printable characters and control
    characters
  • organized into lines
  • system may convert or remove some input
    characters
  • system may insert or convert some output
    characters
  • Binary files
  • contain a series of characters
  • no characters translated on input or output
  • used for files with binary or unprintable
    characters

58
fread
  • size_t fread( buffer, size, count, fp )
  • Returns the number of items actually read, if
    less than
  • requested must use feof or ferror function to
    determine
  • if end-of-file or error occurred
  • buffer - character array to receive the input
  • size - size in bytes of each item
  • count -maximum number of items to be read
  • fp -file pointer to the input file

59
fwrite
  • size_t fwrite( buffer, size, count, fp )
  • fwrite function writes a requested number of
    items of
  • a specified size. Returns the number of items
  • actually written, if less than requested error
    occurred
  • buffer - character array containing data to be
    written
  • size - size in bytes of each item
  • count - number of items to write
  • fp - file pointer to the open output file

60
Binary File Example
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include ltstring.hgt
  • include ltunistd.hgt
  • include ltassert.hgt
  • int main(int argc, char argv)
  • int cal80, flo5100
  • int i 0, j 0
  • FILE filea, fileb
  • for (i0 ilt80 i)
  • cali i

61
Binary File Example
  • / write the data to the first file /
  • filea fopen(argv1, "wb" )
  • / write one 80-byte record /
  • i fwrite(cal, 80sizeof(int), 1, filea)
  • fclose(filea)
  • / read the data from the first file /
  • filea fopen(argv1, "rb" )
  • / read one 80-byte record /
  • i fread( cal, 80sizeof(int),1, filea )
  • /write the data to the second file /
  • fileb fopen(argv2, "wb" )
  • i fwrite( cal, 80sizeof(int), 1, fileb )
  • return 1

Open the first file for writing and write the
structures
Open the first file for reading and read the
structures
Write the data to the second file
62
Good Luck in the Project!!!
Write a Comment
User Comments (0)
About PowerShow.com