Lecture 11
Scalar data in Perl
Perl Control Structures
Strings vs. Words
Counters in Perl
File Management
Match Operators
A Stroll through Perl: Starting up
#!/usr/bin/perl -w
print ("Hello, World\n");
- obligatory
-w give me warnings
( ) optional; contains print’s argument
strings of English go inside double quotes
semicolon ends all statements
Scalar Data
Now we want our program to
ask for the user’s name
take in the name and assign it to a variable
print out
Hello, $name
where
$name is the user’s name
Using a Scalar Variable
#!/usr/bin/perl -w
print ("What is your name?\n");
$name = <STDIN>;
chomp ($name);
print ("Hello, $name\n");
same print statement as hello
<STDIN>
grabs one line of input from the screen
$name
holds this input
$name is known as a scalar variable
Adding a Condition
Syntax of an if condition
if (condition) {
action;
}
parentheses are required around the condition
curly braces are required around the action
if then else
Normally an if condition implies a choice
if (the gas is low) {
buy more;
} else {
continue driving;
}
The effect of the
if condition
An if creates a fork in the road
gas is low buy more
if
gas is high continue driving
The flow of the program can go in either direction
Control Structures
Because it affects the flow of the program it is a control flow statement
Other control structures (control flow statements) in Perl
while
for
foreach
The notion of a ‘string’
continent a new nation conceived in liberty
in this quote
- the word ‘in’ occurs once
- the string ‘in’ occurs twice
- a string is a sequence of characters
The need for a string counter
grep Powell ap.stories
asked Powell. Powell replied that he would
grep returns the line with multiple ‘Powell’s
tr -cs [A-z0-9] $1 | sort | uniq -c
43 Powell
gives the answer but with a lot of extra unnecessary computing and no context
.
A Perl Counter
Perl allows a scalar variable that can hold a numerical value; let’s call it $ct
We can start $ct with a value of 0
Each time our program finds ‘Powell’, it can add 1 to the value of $ct
At the end of the program, we can print the final value of $ct, which will be the number of times it found the string ‘Powell’
Writing a Perl Program
Writing a program around the counter involves more than using the counter.
Let’s step through a program that counts the number of instances of ‘in’ in the Gettysburg Address
Filehandles
allow you to "grab" data
we’ve already used <STDIN> as a filehandle in
$name = <STDIN>;
now we want to associate a filename with a variable called FH:
(FH, "$ARGV[0]")
Filehandles (2)
(FH, "$ARGV[0]")
we are writing a program called findwd.pl
the command line for this program will be
findwd.pl gettysburg
$ARGV[0]
is the file gettysburg
Opening a file
In most programming languages, the program has to be told how to treat a file.
Is the file to be written to, read from, or opened for processing?
open (FH,"$ARGV[0]")
Non-existent files
If Perl can’t find the file that is to be opened we want to
stop the program from running:
die
get a failure report:
"cannot open"
Putting it together
die "cannot open"
How Perl reads from a file
Like sed, grep, etc., Perl reads a file line by line
So, <FH> will have the value of the current line
If we want to do text processing a line at a time, we can bind a variable in our program to the current value of <FH>
$line = <FH>;
Getting a Perl script to read from a file
open (FH,"$ARGV[0]") || die "cannot open";
while ($line = <FH>)
open the file that’s listed first on the command line and bind it to FH
or stop the program and report failure
- bind the incoming line to the variable $line
The =~ operator
The equal sign binds a value to a variable
$name = <STDIN>
$a = "fred"
The match operator =~ matches a pattern somewhere in a string
$line =~ /in/
$line =~ /[Jj]ohn/