Download presentation
1
Introduction to Unix – CS 21
Lecture 15
2
Lecture Overview Perl Programming Why would you want to use it?
How does it compare to awk, sed, and bash? Syntax Semantics
3
Why Perl? Perl tries to be a little bit of everything
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
4
Perl Mentality 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
5
Running Perl One liners Perl scripts
perl –e ‘print “Hello world!\n” ;’ Perl scripts #!/usr/bin/perl –w Adds warnings, which is VERY important
6
Major Differences From Bash and Awk programs
Every line must end with a ; In bash and awk, simply hitting return ended a command Every use of a variable must have the $ In bash: myVar=0 In perl: $myVar=0 ;
7
Printing In Perl The print statement
print $myVar ; print $myVar, “ and “, $myOtherVar ; 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” print $myVar, “\n” ;
8
Variables In Perl Declared bash style (no previous declaration)
Scalars A single value. (5, “Hello”, 4.3) $variableName Arrays A group of values @arrayName Others Automatic Interpretation
9
What Is An Array? “hi” $variable1 “there” $variable2 5.3 $variable3
4.1 $variable4 “hi” “there” 5.3 4.1 @array 1 2 3
10
Accessing Array Elements In Perl
All elements are numbered starting from zero Accessing the array as a whole requires Accessing each individual element requires the $ @myArray = (5, “Hello”, 4.3”) ; print $myArray[1], “\n” ;
11
File Input And Output In order to read or write to an external file, we need a file handle Special variable that refers to an external file Should be in all caps to avoid confusion Reading a line from a file: <FILE> Writing to a file: print FILE “hello file!\n” ;
12
Declaring File Handles: The Open Command
open(HANDLE, “filename”) ; Open for reading open(HANDLE, “> filename”) ; Open for writing open(HANDLE, “>> filename”) ; Open for writing by appending
13
STDIN, STDOUT, And STDERR
As usual, three files/streams are already ready to go whenever you run a perl program STDIN, STDOUT, and STDERR Read lines one at a time from STDIN $line = <STDIN>
14
Example
15
Cutting Off The Newline: chomp
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>) ;
16
Chomp Example
17
Special Cases Made Easy
Focusing on the idea that common cases should be made easy, there are a lot of shortcuts available in perl $line = <> ; 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
18
Example Of <>
19
The Default Variable: $_
To make shortcuts even easier, if you don’t assign a value, the results are automatically stored in a default variable named $_ <STDIN> ; 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?
20
Example: Cat In Perl
21
Conditional Statements
Just like awk and bash, perl has a set of statements that control the flow of execution if unless Conditions are based upon comparisons or tests
22
Comparison Operators Numbers Strings Description == eq Equals != ne
Not equal > gt Greater than < lt Less than <= le Less than or equal >= ge Greater than or equal <=> cmp Compare (0=, 1>, -1<)
23
File Operations -e -d -f -T File exists File is a directory
File is a normal file -T File is a text file
24
If statements if (CONDITION) { STATEMENTS ; }
25
Examples Of if
26
If..elsif..else statements
if (CONDITION) { } elsif (CONDITION) else
27
Unless Statement unless (CONDITION) { STATEMENTS ; }
28
Unless Flowchart unless Is Condition True? no Statements yes
29
Unless Example
30
Another Form 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 if (5>3) { $myVar = 4 ; } $myVar = 4 if (5 > 3) ;
31
Looping Statements To allow repetition, several looping statements are allowed just like bash and awk while for foreach
32
While Statements while (CONDITION) { STATEMENTS ; }
33
For Statements for (INIT ; CONDITION; INCREMENT) { STATEMENTS ; }
34
Example Of for
35
Foreach Statements foreach $var ) { STATEMENTS ; }
36
Example Of foreach
37
Regular Expressions Syntax is mostly that of egrep with a couple of differences Works on the default variable Default behavior is greedy Will match the largest string possible ? Restricts the match to the smallest possible
38
Example Jason Villarreal:11342:Midterm1:59:100 /:(.*):/ /:(.*?):/
39
Substitution In Perl Just like in vi s/old/new/
Works on the default variable In order to work with other variables, another operator is needed
40
Sed Functionality In Perl
A special comparison operator: =~ Checks to see if a pattern appears in a variable Example: $line =~ /Jason/ ;
41
Example: Deleting All Quiz #3’s
sed ‘/Quiz 3/d` database Perl: open(DATABASE, “database”) ; while ($line = <DATABASE>) { print $line if ! ($line =~ /Quiz 3/) ; }
42
Backreferences In R.E. Every time you use parenthesis in a regular expression, the pattern matched becomes marked and can be accessed later Marked by a number \1 stands for the first () \2 stands for the second () Etc…
43
Example Of Back References
Matching HTML tags <LI> … </LI> <H1> … <H2> </H2> </H1>
44
Example
45
Awk Functionality In Perl: split
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: @fields = split(“:”, $record) ; ($field1, $field2) = split(“:”, $record) ;
46
Example: Printing Out The Third Field
Awk: { print $3 } Perl: while ($line = <FILE>) = split(“:”, $line) ; print $fields[2] ; }
47
In Lab Today Practice with tar and perl
Writing very small perl programs Rewriting some previous programs in perl
48
Next Week LaTeX Make
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.