Perl
- What is Perl?
- Scalars
- Scalar Variables
- Operators
- Assignments
- Simple Input and Output
- Control Structures
What is Perl?
- Perl had been in development since 1987
- Started out as a language for creating reports
- Grew into a general purpose tool
- Perl is ideally suited for processing data
- Powerful pattern matching and transformation features
- Perl has a short edit-compile-test-cycle
- Programs are compiled when they are run
- Perl encourages code reuse
- Support creation of modules of code
- A global repository of reusable modules at www.cpan.org
Scalars
- Perl has three types of variables: scalars, arrays, and hashes
- We being by discussing the most commonly used type of variable, scalars
- Scalar variables can store three kinds of values:
- Numbers
- Strings
- References
- We postpone discussing references until later
Number Literals
- Numbers stored in scalar variables are represented in double-precision floating-point form
- Number literals can have the form of integers or floating-point values
123 123.45 .45 123. 1e2 .45e2 1.23e-2
String Literals
- String literals can be enclosed in single quotes (') or double quotes (")
- Single-quoted strings are interpreted literally
- In this example "\n" is treated as two characters
'The yellow fox\n jumped over the fence'
- Double-quoted strings can contain escape sequences (such as "\n")
- If the string contains variables, they will be substituted by their values
"CD \t Artist \t Title \t Play Time"
More About String Literals
- If you want to use a different delimiter than ' or ", you can precede it with either "q" (single-quoted) or "qq" (double-quoted)
q$I don't want to stay$ qq#"I am late!", said the rabbit#
- Strings are not stored as arrays of characters, but treated as a single unit
- The null string (with no characters) is denoted '' or ""
Scalar Variables
- Scalar variables always begin with a dollar sign ($)
- The "$" should remind you of the "S" in scalar
- As in Java or C++, variable names are case-sensitive
$firstname $firstName $FirstName
- Double-quoted strings that contain variables have these variables substituted by their values
- For example, if the value of $answer is 42
"The answer to Life, the Universe and Everything is $answer"
- If not explicitly declared (see later when we discuss functions), variables are declared implictly when they are first encountered
Predefined Variables
- Perl has a large number of predefined variables (also called special variables)
- Their names of most begin with dollar signs
- The rest of the name is often just one special character (such as "_", "^" or "\")
- We will see many uses of these variables in what follows
perldoc
- Perl includes extensive online documentation
- Specific parts of the documentation can be extracted with the command "perldoc" followed by a topic
- For example, the following produces a list of predefined variables
perldoc perlvar
Arithmetic Operators
- Most numeric operators have been borrowed from C
- The most common ones are the binary operators +, -, , /, * (power), and % (modulus)
- There also the unary operators -- and ++
- As indicated earlier, numeric operations are performed in double-precision floating-point
5/2 => 2.5
String Operators
- The string concatenation operator is the period
"front" . " " . "page" "front page"
- The repetition operator is specified as an "x" (it takes a string and a number as operands)
"More! " x 3 "More! More! More! "
Predefined Functions on Strings
- Functions and operators in Perl are closely related
- In many cases, you can treat them interchangably, eg a predefined operator "doit" can be treated as an operator
doit x
- Or, you can treat is as a function, which has a list of parameters
doit(x)
- The most common functions on strings are:
- chomp removes any terminating newline characters
- length returns the number of characters in a string
- lc changes a string to all lowercase
- uc changes a string with lowercase letters to uppercase
- join concatenates strings and inserts a string between them
chomp "hello, world!\n" "hello, world!"
- Multiple parameters must be separated by commas
join '_', 'a', 'yellow', 'fox' 'a_yellow_fox'
Assignments
- Perl's assignment statements are the same as in Java
$salary = 40000;
- Similar to Java, assignment statements return the assigned value as their value
- We can use this in conditional statements (eg, inside a while)
- There are also the usual shorthand operators, eg
$salary += $raise;
- Note that all Perl statements (except those at the end of a block -- see control structures) are terminated by a semicolon
Input from the Console
- Although we will be talking mostly about using Perl in CGI scripts, it is useful to know how you can read data from the console
- The following code reads a single line from the standard input
- The angles (< and >) indicate the line input operator
$data = <STDIN>;
- Generally, you don't want the newline character at the end:
$data = <STDIN>; chomp $data;
- You can even shorten this to (although this might be less clear):
chomp($data = <STDIN>);
Output to the Console
- The print function directs output to the console
- The arguments to print are one or more strings, separated by commas
- No implicit newline character is added to the last string
print "One, ", $two, "buckle my shoe\n";
- The following program illustrates some of what we discussed up to now:
#!/usr/bin/perl -wprint "Name: "; $name = <STDIN>; chomp $name;
print "Surname: "; $surname = <STDIN>; chomp $surname;
print "Welcome, $name $surname!\n";
"Here" Documents
- When printing multiple lines of text, you don't need to print each one separately
- Instead you can use what is called a "here" document
- It simplifies printing large blocks of text without worrying about quoting rules
- To begin a here document, follow print by the "<<" operator and a label
print << HERE;
- Then type your text, followed by the label in a new line (no semicolon is necessary; in fact, it would be considered part of the label)
print << HERE; <tr> <th>$name</th> <td>$ENV{$name}</td> </tr> HERE
Control Structures
- Perl has the usual set of control structures (if, while, etc)
- Plus some unique ones (such as unless)
- Each control structure has three pieces:
- Type of control structure (loop, branch, etc)
- Condition
- Block of executable statements
Note On Conditions
- There are no booleans in Perl
- 0, the empty string, or an undefined variable (no value has been assigned to it) are interpreted as false
- Everything (!) else (eg a number != 0) is interpreted as true
- Thus the following is a valid condition
- It evaluates true if the $number has been assigned a value != 0, and false otherwise
$number
- Because assignment statements have values, they can be used as conditions
- One common example is the line operator which returns the empty string when it encounters end-of-file
while ($next = <STDIN>) { ... }
Relational Expressions
- Perl has two sets of relational expressions, one for numbers, and one for string
- To compare numbers, use the following comparison operators
== != < > <= >=
- To compare strings, a separate set of comparison operators must be used
eq ne lt gt le ge
Complex Conditions
- Conditions can be created from multiple simple conditions using the following logical operators (note that you can use symbolic or text versions)
and && or || not !
- Parentheses should be used to make complex conditions easy to read
(($name eq "Lucy") && ($age >= 18))
Conditional Branches
- The if statement is similar to that of other languages, only its syntax is a bit different
- else + if = elsif
if ($salary > 50000) { $taxRate = .50; } elsif ($salary > 30000) { $taxRate = .40; } else { $taxRate = .30; }
- Perl also supports an unless statement, which test for the inverse condition
- unless = if with only an else clause
unless ($link) { print "No link provided"; }
- Note that a sequence of statements within braces ({ and }) is called a block in Perl
Loops
- Perl support while and for statements similar to those of Java; until is similar except it uses the inverse of the condition
$n = 0; while ($next = <STDIN>) { chomp $next; if ($next eq "Rheostatics") { $n++; } } print "Rheostatics occurs $n times\n";
- Another loop control structure is foreach; it is used for iterating through an array, and will be discussed after we introduced arrays
Special Variable $_
- The special variable $_ is used frequently in Perl programs
- $_ is also known as the input space
- it is the default parameter of many predefined functions and operators
- The while construct above can be rewritten to make use of $_
- $_ is the default target of the line input operator
- It is also the default argument of the chomp operator and the print function
$n = 0; while (<STDIN>) { chomp; if ($next eq "Rheostatics") { $n++; } } print "Rheostatics occurs $n times\n";