introduction to unix – cs 21 lecture 15. lecture overview perl programming why would you want to...

48
Introduction to Unix – CS 21 Lecture 15

Upload: alexina-woods

Post on 05-Jan-2016

219 views

Category:

Documents


2 download

TRANSCRIPT

Introduction to Unix – CS 21

Lecture 15

Lecture Overview Perl Programming

Why would you want to use it? How does it compare to awk, sed, and

bash? Syntax Semantics

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

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

Running Perl One liners

perl –e ‘print “Hello world!\n” ;’ Perl scripts

#!/usr/bin/perl –w Adds warnings, which is VERY

important

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 ;

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” ;

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

What Is An Array?

$variable1

$variable2

$variable3

$variable4

“hi”

“there”

5.3

4.1

“hi” “there” 5.3 4.1@array

0 1 2 3

Accessing Array Elements In Perl All elements are numbered

starting from zero Accessing the array as a whole

requires the @ Accessing each individual element

requires the $ @myArray = (5, “Hello”, 4.3”) ; print $myArray[1], “\n” ;

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” ;

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

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>

Example

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>) ;

Chomp Example

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

Example Of <>

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?

Example: Cat In Perl

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

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<)

File Operations -e

File exists -d

File is a directory -f

File is a normal file -T

File is a text file

If statements

if (CONDITION){ STATEMENTS ;}

Examples Of if

If..elsif..else statementsif (CONDITION){}elsif (CONDITION){}else{}

Unless Statement

unless (CONDITION){

STATEMENTS ;}

Unless Flowchart

unless

Is Condition True?

yes

no

Statements

Unless Example

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) ;

Looping Statements To allow repetition, several looping

statements are allowed just like bash and awk while for foreach

While Statements

while (CONDITION){

STATEMENTS ;}

For Statements

for (INIT ; CONDITION; INCREMENT){

STATEMENTS ;}

Example Of for

Foreach Statements

foreach $var ( @array ){

STATEMENTS ;}

Example Of foreach

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

Example Jason

Villarreal:11342:Midterm1:59:100 /:(.*):/

:11342:Midterm1:59: /:(.*?):/

:11342:

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

Sed Functionality In Perl A special comparison operator:

=~ Checks to see if a pattern appears in

a variable Example: $line =~ /Jason/ ;

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

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…

Example Of Back References Matching HTML tags

<LI> … </LI> <H1> … <H2> </H2> </H1>

Example

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) ;

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

print $fields[2] ; }

In Lab Today Practice with tar and perl

Writing very small perl programs Rewriting some previous programs in

perl

Next Week LaTeX Make