Professional Documents
Culture Documents
TO
PERL PROGRAMMING
Please switch mobiles to silent mode
No texting/messaging
Course Purpose
The purpose of this course is:
To impart knowledge on how to program
using Perl
Lesson 1: Overview
What is Perl?
Practical Extraction and Report Language.
Text-processing language - powerful regular
expressions
Scripting language - easy to write functional code
and programs
Open-source and free language - supported by a
helpful international community
Text processing
System administration tasks
CGI and web programming
Database interaction
Other Internet programming
Other uses :
The Human Genome Project relies on Perl for
DNA sequencing.
NASA use Perl for satellite control.
#!/usr/bin/perl
# This Program will print Hello World
print Hello, World\n;
Lesson Objectives
Define Data Types in Perl
Data Types
Scalars - single values
Scalars
Store any single value
Number
integer (12, 1E+100)
real (3.14159, 2.71828182845905)
decimal (15), octal (017), or hexadecimal (0xF)
String
a single character ("a")
many characters ("A quick brown...")
Reference
Scalars
Have a name preceded by a $ character
Must not begin with a digit
Case-sensitive
Some special names are reserved ($_, $1, $/)
No need to specify whether its an integer or a
character
$var = 42 ;
$var = This is life;
Scalars - MY
May be declared in a local scope with a my qualifier
Helps avoid common mistakes
Limits the variable to its enclosing block
Declaring a variable without assigning a value leaves
its value undefined
May declare a variable and assign a value in the
same statement
my $apple_variety;
# Value undefined
$apple_variety = "Red Delicious"; # Defined
my $apple_color = "red";
# Declare and define
Scalars
Dynamically assume whatever value is assigned
my $quantity = 6;
$quantity = "half dozen";
$quantity = 0.5 * 12;
Outputting Scalars
my $name = Ankit;
print My name is : $name\n;
print My name is : $name\n;
Output
My name is : Ankit
My name is : $name\n
Arrays
Store any number of ordered scalars
Arrays
Indexed by number
Arrays
my @fruits = ("apples", "bananas", "cherries");
print "Fruit flies like $fruits[1].\n";
print "Life is like a bowl of $fruits[$#fruits].\n;
print "We need more $fruits[-3] to make the pie.\n";
$fruits[0] = "oranges"; # Replace apples with oranges
Arrays
May be assigned in several ways
Arrays
@prime_numbers = (2, 3, 5, 7, 11, 13); # Comma-eparated
@composite_numbers = (4, 6, 8..10, 12, 14..16); # Numeric
ranges
@fruits = ("apples", "bananas", "cherries");
@fruits = qw(apples bananas cherries); # Same as above
@veggies = qw(radishes spinach);
@grocery_list = (@fruits, @veggies, "milk");
print "@grocery_list\n";
Arrays
Dynamically assume whatever values or size
needed
Can dynamically lengthen or shorten arrays
May be defined, but empty
Arrays
my @fruits;
# Undefined
@fruits = qw(apples bananas cherries); # Assigned
@fruits = (@fruits, "dates");
# Lengthen
@fruits = ();
# Empty
unshift @fruits, "acorn"; # Add an item to the front
my $nut = shift @fruits; # Remove from the front
print "Well, a squirrel would think an $nut was a fruit.\n";
push @fruits, "mango"; # Add an item to the end
my $food = pop @fruits; # Remove from the end
print "My, that was a yummy $food!\n";
Arrays
Easy to iterate
foreach loop iterates over entire array
Good to localize the scalar to the loop
my @fruits = qw(apple orange grape cranberry);
foreach my $fruit (@fruits) {
print "We have $fruit juice in the refrigerator.\n";
}
We have apple juice in the refrigerator.
We have orange juice in the refrigerator.
We have grape juice in the refrigerator.
We have cranberry juice in the refrigerator.
Arrays
Copying Arrays
Hashes
Store any number of unordered scalars
Hashes
Hashes
Indexed by key name
An index in curly braces refers to a hash item
Each item is a scalar, so use scalar syntax $hash{$key}
Items are stored in random order
Hashes
print "A bicycle has $wheels{bike}wheels.\n";
Hashes
May be assigned in several ways
Hashes
my %dessert = ("pie", "apple", "cake", "carrot", "sorbet",
"orange");
my %dessert = (pie => "apple", cake => "carrot", sorbet
=> "orange"); # Same, but easier to read
my %ice_cream = (bowl => "chocolate", float => "root
beer");
my %choices = (%dessert, %ice_cream);
print "I would like $choices{sorbet} sorbet.\n";
I would like orange sorbet.
Hashes
Dynamically assume whatever values or size
needed
Can dynamically lengthen or shorten hashes
May be defined, but empty
No predefined size or "out of bounds" error
New keys are auto-instantiated when used
Old keys may be deleted
Hashes
my %wheels = (
unicycle => 1,
bike => 2,
tricycle => 3);
$wheels{car} = 4; # Creates a new key/value pair
Hashes
Easy to iterate
keys returns a list of the keys
values returns a list of the values
Hashes
my @animals = keys %sounds;
my @noises = values %sounds;
Hashes
Length of Arrays
$length = %original;
#$length now contains the number of buckets
$length = keys(%original);
#$length now contains the number of keys
Summary
Three Basic Data Types
Scalars - single values
Have a name preceded by a $ character
Special Scalars
$0
$$
$!
$?
$.
# Explicit initialization
# Works on $_
Lesson 4: Context
Lesson Objective
Define context in Perl
Understand the effect of context on
various data types
Context
Context determines how variables and values are
evaluated
In Perl, is "1" a number, a string, or a boolean
value?
Context
Several types of scalar context
numeric
string
boolean
Numeric Context
Numeric operations apply a numeric context
Undefined values are treated as zero
Scalars are evaluated as numbers
Strings are converted to their numeric value
automatically
Non-number strings are converted to zero
Arrays are evaluated as their length
Numeric Context
my $number;
my $string;
$string = "5.2";
# 5.2 in numeric context
$number = $string + 17;
print "Number is $number.\n";
$string = "five";
# Zero in numeric context
$number = $string + 17;
print "Number is $number.\n";
String Context
String operations apply a string context
Period (.) performs string concatenation
String Context
String operations apply a string context
my $string;
my $number;
# Empty string in string context
$string = $number . 17; # Concatenate
print "String is '$string'\n";
$number = 5.2;
# "5.2" in string context
$string = $number . 17;
print "String is '$string'\n";
$number = 5.2;
Boolean Context
Boolean operations apply a boolean context
Undefined values are treated as false
Scalars are evaluated as booleans
Numbers are evaluated as true if non-zero
Boolean Context
my $string;
if ($string) {
print "A: Hello, $string.\n";
}
$string = "world";
if ($string) {
print "B: Hello, $string.\n";
}
Arrays
In scalar context, returns the length of the array
Hashes
In scalar context, returns a measure of the space used
# entire list
$count = @pets;
# $count = 3
@new_pets = @pets;
# entire array
# true
@mix = %pets;
%new_pets = %pets;
# entire hash
Summary
String
String operations apply a string context
boolean
Boolean operations apply boolean context
Lesson 5: Operators
Numeric Operators
Numeric operators provide numeric context
String Operators
String operators provide string context
Common operators are provided
Concatenation (.)
Repetition (x)
Assignment (.=, x=)
Boolean Operators
Boolean operators provide boolean context
Many types of operators are provided
Relational (<, >, lt, gt)
Conditional (?:)
Boolean Operators
Separate operators for numeric vs. string
comparison
Numeric (<=, >=, >, <, ==)
String (le, ge, gt, lt, eq)
List Operators
List operators provide list context
Many useful operations or functions are built-in
sort
reverse
push/pop
unshift/shift
split/join
grep
Lesson Objectives
How to control the flow of your program in
Perl
Flow
Control
Flow control is more expressive in Perl
Conditional statements
if
unless
Loop statements
while
until
for
foreach
Modifiers
Simple statements may be modified
Conditional Statement
Conditional statements provide boolean context
if statement controls the following block
if, elsif, else
Yes, that does say elsif. Oh well.
unless is opposite of if
Equivalent to if (not $boolean)
unless, elsif, else
There is no unlesif, thankfully.
Conditional Statement
my ($a, $b) = (0, 1);
if (!$a && !$b) {print "Neither\n";} #
Conventional
if (not $a and not $b) {print "Neither\n";} #
Same, but in English
if (not ($a or $b)) {print "Neither\n";} # Same,
but parentheses
unless ($a or $b) {print "Neither\n";} # Same, but
clearest
Loop Statements
Loop statements provide a boolean context
while
Loops while boolean is true
until
Loops until boolean is true
Opposite of while
do
At least one loop, then depends on while or
until
Contd.
while ($hungry) {
$hungry = eat($banana);
}
do {
$answer = get_answer();
$correct = check($answer);
} until ($correct);
Loop Statements
for
Like C: for (initialization; condition; increment)
foreach
Iterates over a list or array
Flow Control
my @string = (red,green,,blue);
for( my $I =0; $I<=$#string; $I++) {
$string = $string[$I];
next if ($string ne );
if( $string eq ) {
print Out of Loop\n;
last;
}
}
Flow Control
my @string = (red,green,,blue);
foreach my $string (@string) {
next if ($string ne );
if( $string eq ) {
print Out of Loop\n;
last;
}
}
Flow Control
my @string = (red,green,,blue);
foreach (@string) {
next if ($_ ne );
if( $_ eq ) {
print Out of Loop\n;
last;
}
}
Contd.
for (my $i = 10; $i >= 0; $i---) {
print "$i...\n"; # Countdown
}
foreach my $i (reverse 0..10) {
print "$i...\n"; # Same
}
%hash = (dog => "lazy", fox => "quick");
foreach my $key (keys %hash) {
print "The $key is $hash{$key}.\n";
# Print out hash pairs
}
foreach (keys %hash) {
print "The $_ is $hash{$_}.\n";
# Print out hash pairs
}
Lesson 7: Subroutines
Lesson Objectives
How to declare subroutines?
Handling arguments
Calling subroutines
Subroutines
Subs group related statements into a single
task
Perl allows various ways of handling
arguments
Perl allows various ways of calling subs
perldoc perlsub gives complete description
Declaring Subroutines
Subroutines are declared with the sub keyword
Subroutines return values
Explicitly with the return command
Implicitly as the value of the last executed
statement
Contd.
#!/usr/bin/perl
my @numbers = & getnumbers;
foreach $num (@numbers) {
print $numbers\n;
}
sub getnumbers {
return (1 .. 10);
}
Handling Arguments
Two common means of passing arguments to
subs
Pass by value
Pass by reference
Perl allows either
Contd.
sub add_one
{ # Like pass by value
my ($n) = @_; # Copy first argument
return ($n + 1); # Return 1 more than argument
}
sub plus_plus {
# Like pass by reference
$_[0] = $_[0] + 1; # Modify first argument
}
Contd.
my ($a, $b) = (10, 0);
add_one($a); # Return value is lost, nothing changes
$b = add_one($a); # $a is 10, $b is 11
plus_plus($a); # Return value lost, but a now is 11
$b = plus_plus($a); # $a and $b are 12
Calling Subroutines
Subroutine calls usually have arguments in parentheses
Parentheses are not needed if sub is declared first
But using parentheses is often good style
Subroutine calls may be recursive
Subroutines are another data type
Name may be preceded by an & character
& may not be needed when calling subs
Contd.
print factorial(5) . "\n"; # Parentheses required
sub factorial {
my ($n) = @_;
return $n if $n <= 2;
$n * factorial($n - 1);
}
print factorial 5 . "\n"; # Parentheses not required
Summary
Subroutines are declared with the sub keyword
Subroutines return values
Explicitly with the return command
Implicitly as the value of the last executed
statement
Lesson 8:
References
Lesson Objectives
How to create references
Argument passing using references
References
reference is a scalar value that points to a memory location that
holds some type of data.
Creating References
$refScalar = \$scalar;
$refArray = \@array;
value.
$refHash = \%hash;
$refRef = \$refScalar;
my %hash1 = (
);
my %hash2 = (
);
my @arrEmployee = (\%hash1,\%hash2);
References
firstSub( (1..5), ("A".."E"));
sub firstSub {
my(@firstArray, @secondArray) = @_;
print("The first array is @firstArray.\n");
print("The second array is
@secondArray.\n");
}
The first array is 1 2 3 4 5 A B C D E.
The second array is .
References
firstSub( \(1..5), \("A".."E") );
sub firstSub {
my($ref_firstArray, $ref_secondArray) = @_;
print("The first array is
@{$ref_firstArray}.\n");
print("The second array is
@{$ref_secondArray}.\n");
}The first array is 1 2 3 4 5.
The second array is A B C D E.
Lesson 9: Files
Lesson Objectives
Identify standard file descriptors in Perl
How to access files in Perl
File Access
Access to files is similar to shell redirection
open allows access to the file
Redirect characters (<, >) define access type
Can read, write, append, etc.
Filehandle refers to opened file
close stops access to the file
$! contains IO error messages
perldoc perlopentut has complete description
File Access
open (INPUT, "< datafile) || die "Can't open
input file: $!";
open (OUTPUT, "> outfile ) || die "Can't open
output file: $!";
File Access
Standard files are opened automatically
File Access
print STDOUT "Hello, world.\n"; # STDOUT not
needed
open (STDERR, ">> logfile) || die "Can't redirect
errors to log: $!";
File Access
Reading from files
File Access
print "What type of pet do you have? ";
my $pet = <STDIN>; # Read a line from STDIN
chomp $pet;
# Remove newline
print "Enter your pet's name: ";
my $name = <>;
# STDIN is optional
chomp $name;
print "Your pet $pet is named $name.\n";
What type of pet do you have? parrot
Enter your pet's name: Polly
Your pet parrot is named Polly.
File Access
Reading from files
Easy to loop over entire file
Loops will assign to $_ by default
Be sure that the file is open for reading first
open(MYFILE, $myFile) or die "Can't open input file: $!";
while(<MYFILE>);
{
print $_;
}
close (MYFILE);
File Access
rename renames a file
rename(oldname,newname);
-e name2 || rename(name1,name2);
This syntax checks that name2 does not already
exist
We can use rename to move a file
rename(./exp/my.pl, ./verilog/my.pl);
Directory Access
Directory Access
closedir: Stops access to a directory
closedir(dirvar);
readdir: Returns the name of the first file stored in the
directory. If called again , returns the next file.
readdir(dirvar);
Directory Access
opendir(DIR,".") || die "Cannot open:$!";
while($file = readdir(DIR)) {
print "\n$file";
}
closedir(DIR);
Summary
Regular Expressions
Regexes perform textual pattern matching
Regexes ask:
Does a string...
...contain the letters "dog" in order?
Regular Expressions
my $string = "Did the fox jump over the dogs?";
print "1: $string\n" if $string =~ /dog/; # matches
print "2: $string\n" if $string !~ /z/;
# matches
print "3: $string\n" if $string =~ /^[yY]/; # No matches
print "4: $string\n" if $string =~ /\?$/; # matches
print Only Letters\n" if $string =~ /^[a-zA-Z]+$/; #
matches
print Only digits\n" if $string =~ /^\d+$/; # matches
Regex Examples
Regexes may be quoted in several ways
Regex quotes are usually slashes (/regex/)
May use other quotes with the match operator m
Use other quotes when matching slashes (m[/])
Comparison operators (=~, !~) test for matches
Regex Examples
my $string = "Did the fox jump over the dogs?";
print "1: $string\n" if $string =~ /dog/;
# matches
Regex Examples
Regexes are their own mini-language
Match letters, numbers, other characters
Exclude certain characters from matches
Regex Examples
Provided character classes include:
\d, \D for digits, non-digits
\w, \W for word and non-word characters (letters,
digits, underscore)
Regex Examples
my $string = "Did the fox jump over the dogs?";
print "1: $string\n" if $string =~ m/[bdl]og/; # bog, dog, log
Regex Examples
/./
Regex Examples
Match boundaries between types of characters
^ matches the start of the string
$ matches the end of the string
\b matches a word boundary
my $string = "Did the fox jump over the dogs?";
print "1: $string\n" if $string =~ m/^[Yy]/; # no match
print "2: $string\n" if $string =~ m/\?$/; # match
print "3: $string\n" if $string =~ m/the\b/; # match
Regex Examples
Quantify matches
+ matches the preceding character 1 or more
times
/de+f/ Matches any of the following
Regex Examples
* matches the preceding character 0 or more times
/\s*/ means zero or more white space
/hm+/ matches hm, hmm, hmmm whereas
/hm*/ matches all those, but also single h
Regex Examples
? matches the preceding character 0 or once
/joh?n/ matches jon and john
{4} matches exactly 4 times
/hm{2,4}/ matches hmm hmmm hmmmm
{3,6} matches 3 to 6 times
Regex Examples
my $string = "Did the fox jump over the dogs?";
print "1: $string\n" if $string =~ m/z*/;
# matches
# no match
Regex Examples
Group subpatterns
() group a subpattern
Match repetitions
\1, \2 refer to the first and second groups
my $string = "Did the fox jump over the dogs?";
print "1: $string\n" if $string =~ m/(fox){2}/; # "foxfox"
print "2: $string\n" if $string =~ m/(the\s).*\1/; # matches
Regex Examples
Use \1 .. \9 within regex, $1 .. $9 outside
$string = this red bat is red\n;
if ($string =~ /^this (red|blue|green) (bat|ball) is \1/) {
($color, $object) = ($1, $2);
}
If the string is in $_ variable, then
($color, $object) = ~ /^this (red|blue|green) (bat|ball) is \1/;
Regex Examples
Regex Examples
Special variables:
$& means all text matched
$` is text before match
$' is text after match
$+ returns the contents of the last parenthetical
match
Regex Examples
#!/bin/perl
$_ = quadrangle;
/d(.)/;
# match a d followed by any
character
print $`;
# Whats before dr
=> qua
print $ ;
# Whats after dr
=> angle
print $&;
# What matched /d(r)/ => dr
print $+;
Regex Examples
Perl has several regex operators
Regex Examples
my $string = "Did the fox jump over the dogs?";
$string =~ s/dog/cat/; # substitute "cat" for "dog"
$string =~ s(fox)(kangaroo); # substitute "kangaroo"
for "fox"
print "$string\n";
$string =~ tr/a-z/n-za-m/;
print "$string\n";
#Rot13
Regex Examples
Perl has several regex modifiers
g global: allows for multiple substitutions
i case insensitive matching
s treat string as one line
m treat string as multiple lines
Regex Examples
my $breakfast = 'Lox Lox Lox lox and bagels';
$breakfast =~ s/Lox //g;
print "$breakfast\n";
my $paragraph = "First Line\nSecond Line\nThird
Line\n";
my ($first, $second) = ($paragraph =~ /(^.*$)\n(^.*$)/m);
print "$first, $second\n";
Greediness
Greediness
+? One or More times; as few as possible
*? Zero or more times; as few as possible
?? Zero or once; zero if possible
{x,y}? Between x and y; x if possible
Regex Examples
When two or more greedy operators
both want to match the same
bbbbb =~ /^(.+)b(.+)$/;
characters,
the left most wins
places bbb in $1 and b in $2
bbbbb =~ /^(.+?)b(.+)$/;
Summary
Formatted O/P
Formatted O/P
@<<< left-flushed the string
@|||
centers the string
@>>> right-flushes the string
Formatted O/P
Top gun
1986
The Godfather 1972
Nested DataStructures
1. Array of Arrays
2. Array of Hashes
3. Hash of Arrays
Array of Arrays
@first_array = (1,2,3,4,5);
@second_array = (a,b,c,d,e);
@arr_of_arr = (\@first_array, \@ second_array);
Accessing the Values
@arr1 = @{$arr_of_arr[0]}
@arr2 = @{$arr_of_arr[1]}
Array of Hashes
%hash1 = { 1 => a, 2 =>b, 3=>c};
%hash2 = (x,10,y,20,z,30);
@arr_of_arr = (\%hash1, \%hash2);
%deref_hash= %{$arr_of_arr[0]}
$arr_ref = \@arr_of_arr;
Hash of Arrays
@first_array = (1,2,3,4,5);
@second_array = (a,b,c,d,e);
%hash = (Key1, \@first_array, Key2 , \@ second_array);
Hash of Hash
%hash1 = { 1 => a, 2 =>b, 3=>c};
%hash2 = (x,10,y,20,z,30);
%hash3 = {1 => \%hash1, 2 => \%hash2};
%hash3 = { 1 => {
1 =>a,
2 =>b,
3 =>c
},
2 => {
x=>10,
y=>20,
z=>30,
} };
Access method
%deref_hash = %{$hash3 { 1 } };
$hash3{2}->{y};
$ref = \%hash3
$ref->{2}->{y}
$ref->{2}{y};
Package
Focus
Declaring a package
Calling a package
Calling a subroutine from package
Declaring a package
package Foo;
sub bar {
print "Hello $_[0]\n"
}
sub blat {
print "World $_[0]\n"
}
1;
Foo::bar( "a" );
Foo::blat( b" );
Subroutine names must be fully qualified (because they are isolated in
their own package)
CPAN
Comprehensive Perl Archive Network
References
Programming Perl by Larry Wall