Introduction to Unix CS 21 Lecture 15 Lecture

  • Slides: 48
Download presentation
Introduction to Unix – CS 21 Lecture 15

Introduction to Unix – CS 21 Lecture 15

Lecture Overview n Perl Programming n n Why would you want to use it?

Lecture Overview n Perl Programming n n Why would you want to use it? How does it compare to awk, sed, and bash? Syntax Semantics

Why Perl? n Perl tries to be a little bit of everything n n

Why Perl? n Perl tries to be a little bit of everything n n There’s More Than One Way To Do It TMTOWTDI Designed to perform string manipulation and regular expression matching Capability to perform all the same tasks that awk and sed perform with little effort

Perl Mentality n n Perl was designed to try to make common activities simple

Perl Mentality n n Perl was designed to try to make common activities simple Also designed to make not-so-common activities not that complicated If you get good at perl, you can pack a lot of information into small programs Perception: You’re not a hacker unless you know perl

Running Perl n One liners n n perl –e ‘print “Hello world!n” ; ’

Running Perl n One liners n n perl –e ‘print “Hello world!n” ; ’ Perl scripts n n #!/usr/bin/perl –w Adds warnings, which is VERY important

Major Differences From Bash and Awk programs n Every line must end with a

Major Differences From Bash and Awk programs n Every line must end with a ; n n In bash and awk, simply hitting return ended a command Every use of a variable must have the $ n n In bash: my. Var=0 In perl: $my. Var=0 ;

Printing In Perl n The print statement n n print $my. Var ; print

Printing In Perl n The print statement n n print $my. Var ; print $my. Var, “ and “, $my. Other. Var ; Doesn’t go on to a new line like echo does in bash Need to add an explicit marker for the end of the line: “n” n print $my. Var, “n” ;

Variables In Perl n n Declared bash style (no previous declaration) Scalars n n

Variables In Perl n n Declared bash style (no previous declaration) Scalars n n n Arrays n n A single value. (5, “Hello”, 4. 3) $variable. Name A group of values @array. Name Others Automatic Interpretation

What Is An Array? $variable 1 “hi” $variable 2 “there” $variable 3 5. 3

What Is An Array? $variable 1 “hi” $variable 2 “there” $variable 3 5. 3 $variable 4 4. 1 @array “hi” “there” 0 1 5. 3 4. 1 2 3

Accessing Array Elements In Perl n n n All elements are numbered starting from

Accessing Array Elements In Perl n n n All elements are numbered starting from zero Accessing the array as a whole requires the @ Accessing each individual element requires the $ n n @my. Array = (5, “Hello”, 4. 3”) ; print $my. Array[1], “n” ;

File Input And Output n In order to read or write to an external

File Input And Output n In order to read or write to an external file, we need a file handle n n n Reading a line from a file: n n Special variable that refers to an external file Should be in all caps to avoid confusion <FILE> Writing to a file: n print FILE “hello file!n” ;

Declaring File Handles: The Open Command n open(HANDLE, “filename”) ; n n open(HANDLE, “>

Declaring File Handles: The Open Command n open(HANDLE, “filename”) ; n n open(HANDLE, “> filename”) ; n n Open for reading Open for writing open(HANDLE, “>> filename”) ; n Open for writing by appending

STDIN, STDOUT, And STDERR n As usual, three files/streams are already to go whenever

STDIN, STDOUT, And STDERR n As usual, three files/streams are already to go whenever you run a perl program n n STDIN, STDOUT, and STDERR Read lines one at a time from STDIN n $line = <STDIN>

Example

Example

Cutting Off The Newline: chomp n n When reading in a line at a

Cutting Off The Newline: chomp n n When reading in a line at a time, perl keeps the newline at the end of the line Bash doesn’t, and awk doesn’t Use the chomp command to get rid of it chomp($line = <STDIN>) ;

Chomp Example

Chomp Example

Special Cases Made Easy n n Focusing on the idea that common cases should

Special Cases Made Easy n n Focusing on the idea that common cases should be made easy, there a lot of shortcuts available in perl $line = <> ; n n Reads a line at a time from all of the files listed on the command line or STDIN if no files were specified Acts just like other Unix programs

Example Of <>

Example Of <>

The Default Variable: $_ n n To make shortcuts even easier, if you don’t

The Default Variable: $_ n n To make shortcuts even easier, if you don’t assign a value, the results are automatically stored in a default variable named $_ <STDIN> ; n n n Stores the first line read into the default variable Other commands will use this default variable if no variable is supplied, making a lot of work go on “behind the scenes” Is this useful?

Example: Cat In Perl

Example: Cat In Perl

Conditional Statements n Just like awk and bash, perl has a set of statements

Conditional Statements n Just like awk and bash, perl has a set of statements that control the flow of execution n if unless Conditions are based upon comparisons or tests

Comparison Operators Numbers == != > < <= >= <=> Strings eq ne gt

Comparison Operators Numbers == != > < <= >= <=> Strings eq ne gt lt le ge cmp Description Equals Not equal Greater than Less than or equal Greater than or equal Compare (0=, 1>, -1<)

File Operations n -e n n -d n n File is a directory -f

File Operations n -e n n -d n n File is a directory -f n n File exists File is a normal file -T n File is a text file

If statements if (CONDITION) { STATEMENTS ; }

If statements if (CONDITION) { STATEMENTS ; }

Examples Of if

Examples Of if

If. . elsif. . else statements if (CONDITION) { } else { }

If. . elsif. . else statements if (CONDITION) { } else { }

Unless Statement unless (CONDITION) { STATEMENTS ; }

Unless Statement unless (CONDITION) { STATEMENTS ; }

Unless Flowchart unless Is Condition True? no Statements yes

Unless Flowchart unless Is Condition True? no Statements yes

Unless Example

Unless Example

Another Form n n Perl also has a form that is more like English

Another Form n n Perl also has a form that is more like English For single statements, you can place the if or unless and condition after the statement n n if (5>3) { $my. Var = 4 ; } $my. Var = 4 if (5 > 3) ;

Looping Statements n To allow repetition, several looping statements are allowed just like bash

Looping Statements n To allow repetition, several looping statements are allowed just like bash and awk n n n while foreach

While Statements while (CONDITION) { STATEMENTS ; }

While Statements while (CONDITION) { STATEMENTS ; }

For Statements for (INIT ; CONDITION; INCREMENT) { STATEMENTS ; }

For Statements for (INIT ; CONDITION; INCREMENT) { STATEMENTS ; }

Example Of for

Example Of for

Foreach Statements foreach $var ( @array ) { STATEMENTS ; }

Foreach Statements foreach $var ( @array ) { STATEMENTS ; }

Example Of foreach

Example Of foreach

Regular Expressions n n n Syntax is mostly that of egrep with a couple

Regular Expressions n n n Syntax is mostly that of egrep with a couple of differences Works on the default variable Default behavior is greedy n n Will match the largest string possible ? Restricts the match to the smallest possible

Example n n Jason Villarreal: 11342: Midterm 1: 59: 100 /: (. *): /

Example n n Jason Villarreal: 11342: Midterm 1: 59: 100 /: (. *): / n n : 11342: Midterm 1: 59: /: (. *? ): / n : 11342:

Substitution In Perl n n n Just like in vi s/old/new/ Works on the

Substitution In Perl n n n Just like in vi s/old/new/ Works on the default variable n In order to work with other variables, another operator is needed

Sed Functionality In Perl n A special comparison operator: n n n =~ Checks

Sed Functionality In Perl n A special comparison operator: n n n =~ Checks to see if a pattern appears in a variable Example: $line =~ /Jason/ ;

Example: Deleting All Quiz #3’s sed ‘/Quiz 3/d` database n Perl: open(DATABASE, “database”) ;

Example: Deleting All Quiz #3’s sed ‘/Quiz 3/d` database n Perl: open(DATABASE, “database”) ; while ($line = <DATABASE>) { print $line if ! ($line =~ /Quiz 3/) ; } n

Backreferences In R. E. n n Every time you use parenthesis in a regular

Backreferences In R. E. n n Every time you use parenthesis in a regular expression, the pattern matched becomes marked and can be accessed later Marked by a number n n n 1 stands for the first () 2 stands for the second () Etc…

Example Of Back References n Matching HTML tags n n <LI> … </LI> <H

Example Of Back References n Matching HTML tags n n <LI> … </LI> <H 1> … <H 2> </H 1>

Example

Example

Awk Functionality In Perl: split n n Breaking up a line or string based

Awk Functionality In Perl: split n n Breaking up a line or string based on a delimiter is done with a call to split Usage: split( Delimiter, Record ) ; Delimiter can be a regular expression Examples: n n @fields = split(“: ”, $record) ; ($field 1, $field 2) = split(“: ”, $record) ;

Example: Printing Out The Third Field Awk: { print $3 } n Perl: while

Example: Printing Out The Third Field Awk: { print $3 } n Perl: while ($line = <FILE>) { @fields = split(“: ”, $line) ; print $fields[2] ; } n

In Lab Today n Practice with tar and perl n n Writing very small

In Lab Today n Practice with tar and perl n n Writing very small perl programs Rewriting some previous programs in perl

Next Week n n La. Te. X Make

Next Week n n La. Te. X Make