Absolute Beginner's Introduction to Perl 5 and the Perl Community
Will Willis
What is Perl?
- High-level, general-purpose, open sourced programming language
- Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed
- Perl is nicknamed "the Swiss Army chainsaw of programming languages"
due to its flexibility and adaptability.
Origins
- High-level, general-purpose programming language originally developed by Larry Wall in 1987
- Larry continues to oversee the development of Perl. This Role is best conveyed by tese 2 Rules from the offical Documentation:
- Larry is always by definition right about how Perl should behave. This means he has final
veto power on the core functionality.
- Larry is allowed to change his mind about any matter at a later date, regardless of whether
he previously invoked Rule 1.
Larry is always right, even when he was wrong. It's rare to see either Rule exercised, but they are often alluded to.[source]
The Perl Philosophy
- TMTOWTDI
- Three Virtues of a Programmer
- Laziness - The quality that makes you go to great effort to reduce overall energy expenditure.
- Impatience - The anger you feel when the computer is being lazy.
- Hubris - Excessive pride, the sort of thing Zeus zaps you for. Also the quality that makes you write (and maintain) programs that other people won't want to say bad things about.
Why Perl?
- Fills a gap
- Functional and Object Oriented
- It's everywhere
- It's simple (enough)
- It makes easy things easy and hard things possible
- Mature
Getting Perl
- For all operating Systems
Your First Perl Script/Program
print "Hello World\n";
- Put this in a file, hello.pl
- Execute like so:
Variables
- A place where we put/retreive data
- variable names contain alphanumeric characters and underscores
- start with a punctuation mark (Sigil) indicating the type of data
- $ - scalar
- @ - array
- % - hash
- More on variables
Variables?
while (<>) {
chomp;
print join("\t", (split /:/)[0, 2, 1, 5] ), "\n";
}
Special Variables & The Default Variable
Operators and Functions
- Routines built into the language to do "stuff"
- see
perlop
and perlfunc
Arithmetic Operators
- + add
- - subtract
- * multiply
- / divide
- % modulus (finds the remainder of division of one number divided by another)
- ** exponentiation
Shortcut Operators
- $total = $total + $amount
- can be abbreviated to
- $total += $amount
- $x++ is the same as $x = $x + 1
- $x-- is the same as $x = $x - 1
String Operators
- Concatenation (.)
- $name = $firstname . ' ' . $lastname;
- Repetition (x)
- $funny = 'lol ' x 5;
- $line = '-' x 80;
- $page .= $line;
File Test Operators
- -e $file
- -r $file -w $file
- -d $file -T $file
Functions
- Have longer names than operators
- Can take more arguments than operators
- Arguments follow the function name
- See perlfunc for a complete list of Perl's built-in functions
- can return scalars or lists (or nothing)
- More on Functions and Operators
Conditional Constructs
- Conditional constructs allow us to choose different routes of execution through the program
- the unit of program execution is a block of code
- Blocks are delimited with braces { ... }
- conditional Blocks are controlled by the evaluation of an expression.
What is Truth?
- In Perl it is easier to answer "what is false?"
- 0 (the number)
- '' (the empty string)
- () (an empty list)
- undef (an undefined value)
Comparison Operators
- $x == $y
- $x eq $y
- $x != $y
- $x ne $y
- $x > $y
- $x gt $y
- $x >= $y
- $x ge $y
Comparison Examples
- 62 > 42 # true
- '0' == (3 * 2) - 6 # true
- 'apple' gt 'banana' # false
- 'apple' == 'banana' # true(!)
- 1 + 2 == '3 bears' # true
- 1 + 3 == 'three' # false
Boolean Operators
- EXPR_1 and EXPR_2
- true if both are true
- EXPR_1 or EXPR_2
- true if either is true
if
- if (expression) { do stuff... }
- executes "do stuff..." only if "expression" is true
if ($presenter eq 'Will') {
clap();
}
if ... else
- if (expression) { BLOCK1 } else { BLOCK2 }
if ($presenter eq 'Will') {
clap();
} else {
hiss();
}
while
-
while (EXPR) { BLOCK }
- repeat BLOCK while EXPR is true
until
- the oposite of while
-
until (EXPR) { BLOCK }
- execute BLOCK until EXPR is true
for - more complex looping
- for (INIT; EXPR; INCR) { BLOCK }
- Like C
- Execute INIT If EXPR is false, exit loop, otherwise execute BLOCK, execute INCR and retest EXPR
for
for ($i = 1; $i <= 10; $i++) {
print "$i squared is " , $i * $i , "\n";
}
foreach
- foreach - simpler looping over lists
- foreach VAR (LIST) {BLOCK}
- For each element of LIST, set VAR to equal the element and execute BLOCK
foreach my $i (1..10) {
print "$i squared is ", $i * $i, "\n";
}
Beaking out of Loops
- next - jump to next iteration of loop
- last - jump out of loop
- redo - jump to start of same iteration of loop
foreach my $i (1..10) {
print "$i squared is ", $i * $i, "\n";
last if $i > 4;
}
Subroutines
- Self contained "mini-programs" within your program
- Make it easy to repeat code
sub NAME {
BLOCK
}
Subroutine Example
sub yyyymmdd {
my @a = localtime();
my $yyyymmdd = sprintf( "%04d%02d%02d", $a[5] + 1900, $a[4]
+ 1, $a[3] );
return $yyyymmdd;
}
calling a Subroutine
- &yyyymmdd;
- yyyymmdd();
- yyyymmdd;
The last one only works if the sub has been predeclared.
Subroutine Arguments
- Functions become more useful if you can pass arguments to them
- yyyymmdd('yesterday');
- Arguments end up in the
@_
array within the sub
Regular Expressions
- Patterns that match strings
- a "mini-language" within Perl
- Key to perl's text processing power
- sometimes overused
- see perldoc perlre
The Match Operator
m/PATTERN/
- Works on $_ by defualt
- in scalar context returns true if successful
- in list context returns list of "captured" text
- m is optional if you use / characters
- with m you can use any delimiters
- More on Regular Expressions
Matching Example
while(){
print if /foo/;
print if /bar/i;
print if m|http://|;
}
Substitutions
s/PATTERN/REPLACEMENT/
- Works on $_ by defualt
- in scalar context returns true if successful
- in list context returns number of replacements
- again, you can use any delimiters
Substitution Example
while(){
s/teh/the/gi;
s/freind/friend/gi;
s/sholud/should/gi;
print;
}
Binding Operator
- If we want m// or s// to work on something other than $_ we need to use the binding operator
- $name =~ s/Will/William/;
Metacharacters
Matching something other than literal text
- ^ - matches start of string
- $ - matches end of string
- . - matches any character (except new lines)
- \s - matches a whitespace character
- \S - matches a non-whitespace character
More Metacharacters
- \d - matches any digit
- \D - matches any non-digit
- \w - matches any "word" character
- \W - matches any "non-word" character
- \b - matches a word boundry
- \B - matches anywhere except a word boundary
Metacharacter examples
while () {
print if m|^http|;
print if /\bperl\b/;
print if /\S/;
print if /\$\d\.\d\d/;
}
Quantifiers
Specify the number of occurrences
- ? - match zero or one
- * - zero or more
- + match one of more
- {n} - match exactly n
- {n,} - match n or more
- {n,m} - match between n and m
Quantifier examples
while () {
print if /so+n/;
print if /\d*\.\d+/;
print if /\bA\w{3}\b/;
}