62
Perl Perl

Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Embed Size (px)

Citation preview

Page 1: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

PerlPerl

Page 2: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

IntroductionIntroduction

Perl stands for "Perl stands for "PPractical ractical EExtraction xtraction and and RReport eport LLanguage" anguage"

Created by Larry Wall when Created by Larry Wall when awkawk ran ran out of steam out of steam

Perl grew at almost the same rate as Perl grew at almost the same rate as the Unix operating systemthe Unix operating system

Page 3: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Introduction (cont.)Introduction (cont.)

Perl fills the gaps between program Perl fills the gaps between program languages of different levelslanguages of different levels

A great tool for leverageA great tool for leverage High portability and readily High portability and readily

availableavailable Perl can be “write-only,” without Perl can be “write-only,” without

proper care during programmingproper care during programming

Page 4: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

AvailabilityAvailability

It's free and runs rather nicely on nearly It's free and runs rather nicely on nearly everything that calls itself UNIX or UNIX-everything that calls itself UNIX or UNIX-likelike

Perl has been ported to the Amiga, the Atari Perl has been ported to the Amiga, the Atari ST, the Macintosh family, VMS, OS/2, even ST, the Macintosh family, VMS, OS/2, even MS/DOS and WindowsMS/DOS and Windows

The sources for Perl (and many precompiled The sources for Perl (and many precompiled binaries for non-UNIX architectures) are binaries for non-UNIX architectures) are available from the Comprehensive Perl available from the Comprehensive Perl Archive Network (the CPAN). Archive Network (the CPAN). http://http://www.perl.comwww.perl.com/CPAN/CPAN    

Page 5: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Running Perl on UnixRunning Perl on Unix Setup path variable to point to the Setup path variable to point to the

directory where Perl is locateddirectory where Perl is located Check /usr/local/bin or /usr/bin for “perl”Check /usr/local/bin or /usr/bin for “perl” Run a Perl script by typing “perl Run a Perl script by typing “perl

<filename>” <filename>” Alternatively, change the file attribute to Alternatively, change the file attribute to

executable and include “#!/usr/bin/perl” executable and include “#!/usr/bin/perl” in the first line of your perl scriptin the first line of your perl script

The .pl extension is frequently The .pl extension is frequently associated to Perl scriptsassociated to Perl scripts

Page 6: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Running Perl on Win32Running Perl on Win32

ActivePerl allows Perl scripts to be ActivePerl allows Perl scripts to be executed in MS-DOS/Windowsexecuted in MS-DOS/Windows

Perl was ported faithfullyPerl was ported faithfully The #! directive is no longer used because The #! directive is no longer used because

it does not mean anything to it does not mean anything to MS-DOS/WindowsMS-DOS/Windows

Perl scripts are executed by typing “perl Perl scripts are executed by typing “perl <filename> <filename>

Alternatively, double clicking on the file if Alternatively, double clicking on the file if the extension .pl is associated to the Perl the extension .pl is associated to the Perl interpreterinterpreter

Page 7: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

An ExampleAn Example

#!/usr/bin/perl#!/usr/bin/perl

print “Hello World!”;print “Hello World!”; The #! directive directs subsequent lines in the The #! directive directs subsequent lines in the

file to the perl executablefile to the perl executable All statements are terminated with ; as in C/C+All statements are terminated with ; as in C/C+

+/Java+/Java print by default outputs any strings to the print by default outputs any strings to the

terminal console (such as printf in C or cout in terminal console (such as printf in C or cout in C++)C++)

Perl completely parses and compiles the script Perl completely parses and compiles the script before executing itbefore executing it

Page 8: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

VariablesVariables

Three main types of variables - scalar, hash Three main types of variables - scalar, hash and arrayand array

Examples: $scale, %hash, @arrayExamples: $scale, %hash, @array Perl is not a strongly typed languagePerl is not a strongly typed language Retrieving values from the variables:Retrieving values from the variables: $scale, $hash{key}, $array[offset]$scale, $hash{key}, $array[offset] Variables are all global in scope unless Variables are all global in scope unless

defined to be private or localdefined to be private or local Note: remember that hash and array are Note: remember that hash and array are

used to hold scalar valuesused to hold scalar values

Page 9: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

ExamplesExamples

Assigning values to a scalarAssigning values to a scalar$i = “hello world!”; $i = “hello world!”; $j = 1 + 1;$j = 1 + 1;($i,$j) = (2, 3)($i,$j) = (2, 3) Assigning values to an arrayAssigning values to an array$array[0] = 1; $array[0] = 1; $array[1] = “hello world!”; $array[1] = “hello world!”; push(@array,1); #stores the value 1 in the push(@array,1); #stores the value 1 in the

end of @arrayend of @array$value = pop(@array); #retrieves and removes the $value = pop(@array); #retrieves and removes the

last element last element #from @array#from @array

@array = (8,@array); #inserts 8 in front of @array@array = (8,@array); #inserts 8 in front of @array

Page 10: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Examples (cont.)Examples (cont.)

Assigning values to a hashAssigning values to a hash

$$hash{‘greeting’} = “Hello world!”; hash{‘greeting’} = “Hello world!”;

$hash{‘available’} = 1;$hash{‘available’} = 1;

#or using a #or using a hash slicehash slice

@hash{“greeting”,”available”} = (“Hello @hash{“greeting”,”available”} = (“Hello world!”, 1);world!”, 1);

Deleting a key-value pair from a hash:Deleting a key-value pair from a hash:delete $hash{‘key’};delete $hash{‘key’};

Page 11: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Conditional StatementsConditional Statements

Variables alone will not support Variables alone will not support switches or conditionsswitches or conditions

If-Then-Else like clauses are used to If-Then-Else like clauses are used to make decisions based on certain make decisions based on certain preconditionspreconditions

Keywords: if, else, elsif, unlessKeywords: if, else, elsif, unless Enclosed by ‘{‘ and ‘}’ Enclosed by ‘{‘ and ‘}’

Page 12: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

A Conditional Statement A Conditional Statement ExampleExample

print "What is your name? "; print "What is your name? "; $name = <STDIN>; $name = <STDIN>; chomp ($name); chomp ($name); if ($name eq "Randal") { if ($name eq "Randal") {

print "Hello, Randal! How good of you to be here!\n"; print "Hello, Randal! How good of you to be here!\n"; } else { } else {

print "Hello, $name!\n"; # ordinary greeting print "Hello, $name!\n"; # ordinary greeting } } unless($name eq “Randal”)unless($name eq “Randal”){{

print “You are not Randal!!\n”; #part of the ordinary print “You are not Randal!!\n”; #part of the ordinary greetinggreeting}}

$name = <STDIN> reads from standard $name = <STDIN> reads from standard inputinput

chomp is a built-in function that removes chomp is a built-in function that removes newline charactersnewline characters

Page 13: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

LoopsLoops

Conditional statements cannot Conditional statements cannot handle repetitive taskshandle repetitive tasks

Keywords: while, foreach, for , until, Keywords: while, foreach, for , until, do-while, do-untildo-while, do-until

Foreach loop iterates over all of the Foreach loop iterates over all of the elements in an array or hash, elements in an array or hash, executing the loop body on each executing the loop body on each elementelement

For is a shorthand of while loop For is a shorthand of while loop until is the reverse of whileuntil is the reverse of while

Page 14: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Loops (cont.)Loops (cont.)

Do-while and do-until loops execute Do-while and do-until loops execute the loop body once before checking the loop body once before checking for terminationfor termination

Statements in the loop body are Statements in the loop body are enclosed by ‘{‘ and ‘}’enclosed by ‘{‘ and ‘}’

Page 15: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

While LoopWhile Loop Syntax:Syntax:

while(some expression){while(some expression){statements;statements;……

}} Example:Example:

#prints the numbers 1 – 10 in reverse order#prints the numbers 1 – 10 in reverse order$a = 10;$a = 10;while ($a > 0) { while ($a > 0) {

print $a; print $a; $a = $a – 1;$a = $a – 1;} }

Page 16: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Until LoopUntil Loop Syntax:Syntax:

while(some expression){while(some expression){statements;statements;……

}} Example:Example:

#prints the numbers 1 – 10 in reverse order#prints the numbers 1 – 10 in reverse order$a = 10;$a = 10;until ($a <= 0) { until ($a <= 0) {

print $a; print $a; $a = $a – 1;$a = $a – 1;}}

Page 17: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Foreach LoopForeach Loop Syntax:Syntax:

foreach [<variable>] (@some-list){foreach [<variable>] (@some-list){ statements…statements…}}

Example:Example:#prints each elements of @a#prints each elements of @a@a = (1,2,3,4,5); @a = (1,2,3,4,5); foreach $b (@a) { foreach $b (@a) {

print $b; print $b; } }

Page 18: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Foreach Loop (cont.)Foreach Loop (cont.)

Accessing a hash with keys function:Accessing a hash with keys function:foreach $key (keys (%fred)) { foreach $key (keys (%fred)) {

# once for each key of %fred # once for each key of %fred print "at $key we have $fred{$key}\print "at $key we have $fred{$key}\

n"; n"; # show key and value # show key and value } }

Alternatively:Alternatively:while (($first,$last) = each(%lastname)) while (($first,$last) = each(%lastname)) { print "The last name of $first is $last\{ print "The last name of $first is $last\n"; } n"; }

Page 19: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

For LoopFor Loop

Syntax:Syntax:For(initial_exp; test_exp; re-init_exp ) { For(initial_exp; test_exp; re-init_exp ) {

statements;statements;

… …

} }

Example:Example:

#prints numbers 1-10#prints numbers 1-10for ($i = 1; $i <= 10; $i++) {for ($i = 1; $i <= 10; $i++) {

print "$i "; print "$i ";

}}

Page 20: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Do-While and Do-Until Do-While and Do-Until LoopsLoops

Syntax:Syntax:do {statments; do {statments; do{ statements;do{ statements;} while some_expression; } while some_expression; }until }until

some_expression;some_expression;

Prints the numbers 1-10 in reverse Prints the numbers 1-10 in reverse order:order:$a = 10;$a = 10; $a = 10;$a = 10;do{do{ do{do{ print $a;print $a; print $a;print $a; $a = $a – 1;$a = $a – 1; $a = $a - 1;$a = $a - 1;}while ($a > 0);}while ($a > 0); }until ($a <= 0);}until ($a <= 0);

Page 21: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Built-in functionsBuilt-in functions

shift function shift function Ex: $value = Shift(@fred) is similar to ($x,@fred) = Ex: $value = Shift(@fred) is similar to ($x,@fred) =

@fred; @fred; unshift function unshift function

Ex: unshift(@fred,$a); # like @fred = ($a,@fred); Ex: unshift(@fred,$a); # like @fred = ($a,@fred); reverse function reverse function

@a = (7,8,9); @a = (7,8,9); @b = reverse(@a); # gives @b the value of (9,8,7) @b = reverse(@a); # gives @b the value of (9,8,7)

sort functionsort function @y = (1,2,4,8,16,32,64);@y = (1,2,4,8,16,32,64); @y = sort(@y); # @y gets 1,16,2,32,4,64,8 @y = sort(@y); # @y gets 1,16,2,32,4,64,8

Page 22: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Built-In Functions Built-In Functions (cont.)(cont.)

qw functionqw function Ex: @words = qw(camel llama alpaca); # is Ex: @words = qw(camel llama alpaca); # is

equivalent to @words = equivalent to @words = (“camel”,”llama”,”alpaca”);(“camel”,”llama”,”alpaca”);

defined function defined function Returns a Boolean value saying whether the scalar Returns a Boolean value saying whether the scalar

value resulting from an expression has a real value value resulting from an expression has a real value or not or not

Ex: defined $a;Ex: defined $a;

undefined function undefined function Inverse of the defined functionInverse of the defined function

Page 23: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Built-In Functions Built-In Functions (cont.)(cont.)

uc and ucfirst functions –vs- lc and uc and ucfirst functions –vs- lc and lcfirst functionslcfirst functions <result> = uc(<string>)<result> = uc(<string>) <result> = ucfirst(<string>)<result> = ucfirst(<string>)

$string = “abcde”;$string = “abcde”;

$string2 = uc($string); #ABCDE$string2 = uc($string); #ABCDE

$string3 = ucfirst($string); #Abcde$string3 = ucfirst($string); #Abcde Lc and lcfirst has the reverse effect Lc and lcfirst has the reverse effect

as uc and ucfirst functionsas uc and ucfirst functions

Page 24: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Basic I/OBasic I/O

STDIN and STDOUTSTDIN and STDOUT STDIN Examples:STDIN Examples:

$a = <STDIN>; $a = <STDIN>; @a = <STDIN>; @a = <STDIN>; while (defined($line = <STDIN>)) while (defined($line = <STDIN>))

{ # process $line here } { # process $line here } STDOUT Examples:STDOUT Examples:

print(print(list of argumentslist of arguments); ); print “print “texttext”; ”; printf ([HANDLE], printf ([HANDLE], format, list of format, list of

argumentsarguments););

Page 25: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Regular ExpressionsRegular Expressions

Template to be matched against a stringTemplate to be matched against a string Patterns are enclosed in ‘/’sPatterns are enclosed in ‘/’s Matching against a variable is done by Matching against a variable is done by

the =~ operatorthe =~ operator Syntax: /<pattern>/Syntax: /<pattern>/ Examples:Examples:

$string =~/abc/ #matches “abc” anywhere $string =~/abc/ #matches “abc” anywhere in $stringin $string

<STDIN> =~ /abc/ #matches “abc” from <STDIN> =~ /abc/ #matches “abc” from standard standard #input #input

Page 26: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Creating PatternsCreating Patterns Single character patterns:Single character patterns:

““.” matches any single character except .” matches any single character except newline (\n), for example: /a./ newline (\n), for example: /a./

““?” matches zero or one of the preceding ?” matches zero or one of the preceding characterscharacters

Character class can be created by using “[“ Character class can be created by using “[“ and “]”. Range of characters can be and “]”. Range of characters can be abbreviated by using “-”, and a character abbreviated by using “-”, and a character class can be negated by using the “^” symbol.class can be negated by using the “^” symbol.

For examples:For examples: [aeiouAEIOU] matches any one of the vowels[aeiouAEIOU] matches any one of the vowels [a-zA-Z] matches any single letter in the English [a-zA-Z] matches any single letter in the English

alphabetalphabet [^0-9] matches any single non-digit[^0-9] matches any single non-digit

Page 27: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Creating Patterns (cont.)Creating Patterns (cont.)

Predefined character class Predefined character class abbreviations:abbreviations: \d == [0-9]\d == [0-9] \D == [^0-9]\D == [^0-9] \w == [a-zA-Z0-9]\w == [a-zA-Z0-9] \W == [^a-zA-Z0-9]\W == [^a-zA-Z0-9] \s == [ \r\t\n\f]\s == [ \r\t\n\f] \s == [^ \r\t\n\f]\s == [^ \r\t\n\f]

Page 28: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Creating Patterns (cont.)Creating Patterns (cont.)

Multipliers: *, + And {}Multipliers: *, + And {} * matches 0 or more of the preceding * matches 0 or more of the preceding

character character ab*c matches a followed by zero or more bs ab*c matches a followed by zero or more bs

and followed by a cand followed by a c + Matches 1 or more of the preceding + Matches 1 or more of the preceding

charactercharacter ab+c matches a followed by one or more bs ab+c matches a followed by one or more bs

and followed by a cand followed by a c {} is a general multiplier {} is a general multiplier

a{3,5} #matches three to five “a”s in a stringa{3,5} #matches three to five “a”s in a string a{3,} #matches three of more “a”sa{3,} #matches three of more “a”s

Page 29: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Creating Patterns (cont.)Creating Patterns (cont.)

a{3} #matches any string with more a{3} #matches any string with more than three “a”s in itthan three “a”s in it

Complex patterns can be Complex patterns can be constructed from these operatorsconstructed from these operators

For examples:For examples: /a.*ce.*d/ matches strings such as /a.*ce.*d/ matches strings such as

““aasdffdssdffdscecedfssadfdfssadfzz””

Page 30: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Creating Patterns: Creating Patterns: ExercisesExercises

Construct patterns for the following strings:Construct patterns for the following strings:

1. "a xxx c xxxxxxxx c xxx d“1. "a xxx c xxxxxxxx c xxx d“

2. a sequence of numbers2. a sequence of numbers

3. three or more digits followed by the 3. three or more digits followed by the string “abc”string “abc”

4. Strings that have an “a”, one or more 4. Strings that have an “a”, one or more “b”s and at least five “c”s “b”s and at least five “c”s

5. Strings with three vowels next to 5. Strings with three vowels next to each each other. Hint: try character other. Hint: try character class and general class and general multiplier multiplier

Page 31: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Creating Patterns: Creating Patterns: ExercisesExercises

Answers:Answers: /a.*c.*d//a.*c.*d/ /\d+/ or /[0-9]+//\d+/ or /[0-9]+/ /\d\d\d.*abc/ or /\d{3,}abc//\d\d\d.*abc/ or /\d{3,}abc/ /ab+c{5,}//ab+c{5,}/ /[aeiouAEIOU]{3}//[aeiouAEIOU]{3}/ Other possible answers?Other possible answers?

Page 32: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Anchoring PatternsAnchoring Patterns

No boundaries are defined by the No boundaries are defined by the previous patterns previous patterns

Word boundary: \w and \WWord boundary: \w and \W \b and \B is used to indicate word \b and \B is used to indicate word

boundaries and vice verseboundaries and vice verse Examples:Examples:

/fred\b/ #matches fred, but not frederick/fred\b/ #matches fred, but not frederick /\b\+\b/ #matches “x+y”, but not “x + y”, /\b\+\b/ #matches “x+y”, but not “x + y”,

“++” and ”+”. Why?“++” and ”+”. Why? /\bfred\B/ #matches “frederick” but not /\bfred\B/ #matches “frederick” but not

“fred“fred

Page 33: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Anchoring Patterns Anchoring Patterns (cont.)(cont.)

^ and $^ and $ ^ matches beginning of a string^ matches beginning of a string $ matches end of a string$ matches end of a string Exampls:Exampls:

/^Fred$/ #matches only “Fred”/^Fred$/ #matches only “Fred” /aaa^bbb/ #matches nothing/aaa^bbb/ #matches nothing

Page 34: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

More on matching More on matching operatorsoperators

Additional flags for the matching Additional flags for the matching operator:operator: /<pattern>/i #ignores case differences/<pattern>/i #ignores case differences

/fred/i #matches FRED,fred,Fred,FreD and /fred/i #matches FRED,fred,Fred,FreD and etc…etc…

/<pattern>/s #treat string as single line/<pattern>/s #treat string as single line /<pattern>/m #treat string as multiple /<pattern>/m #treat string as multiple

lineline

Page 35: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

More on Matching More on Matching Operators (cont.)Operators (cont.)

““(“ and “)” can be used in patterns to (“ and “)” can be used in patterns to remember matchesremember matches

Special variables $1, $2, $3 … can be Special variables $1, $2, $3 … can be used to access these matchesused to access these matches

For example:For example:$string = “Hello World!”;$string = “Hello World!”;

if( $string =~/(\w*) (\w*))if( $string =~/(\w*) (\w*))

{{

#prints Hello World#prints Hello World

print “$1 $2\n”;print “$1 $2\n”;

}}

Page 36: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

More on Matching More on Matching Operators (cont.)Operators (cont.)

Alternatively:Alternatively:$string = “Hello World!”;$string = “Hello World!”;

($first,$second) = ($string =~/(\w*) (\w*));($first,$second) = ($string =~/(\w*) (\w*));

print “$first $second\n”; #prints Hello Worldprint “$first $second\n”; #prints Hello World

Line 2: Line 2: Remember that the =Remember that the =~ returns values ~ returns values just like a function. Normally, it returns 0 just like a function. Normally, it returns 0 or 1, which stands for true or false, but in or 1, which stands for true or false, but in this case, the existence of “(“ and “)” make this case, the existence of “(“ and “)” make it return values of the matching patternsit return values of the matching patterns

Page 37: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

SubstitutionSubstitution

Replacement of patterns in stringReplacement of patterns in string s/<pattern to search>/<pattern to s/<pattern to search>/<pattern to

replace>/igreplace>/ig i indicates case insensitivei indicates case insensitive g enables the matching to be performed g enables the matching to be performed

more than oncemore than once Examples:Examples:

$which = “this this this”;$which = “this this this”;

$which =~ s/this/that/; #produces “that this $which =~ s/this/that/; #produces “that this this”this”

Page 38: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Substitution (cont.)Substitution (cont.)

$which =~ s/this/that/g; #produces “that that $which =~ s/this/that/g; #produces “that that that”that”

$which =~ s/THIS/that/i; #produces “that this $which =~ s/THIS/that/i; #produces “that this this”this”

$which =~ s/THIS/that/ig; #produces “that that $which =~ s/THIS/that/ig; #produces “that that that”that”

Multipliers, anchors and memory operators Multipliers, anchors and memory operators can be used as well:can be used as well:$string = “This is a string”;$string = “This is a string”;

$string =~ s/^/So/; # “So This is a string”$string =~ s/^/So/; # “So This is a string”

$string =~ s/(\w{1,})/I think $1/; # “I think This $string =~ s/(\w{1,})/I think $1/; # “I think This is a string”is a string”

Page 39: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Split and Join FunctionsSplit and Join Functions

Syntax: Syntax: <return value(s)> = <return value(s)> =

split(/<pattern>/[,<variable>]);split(/<pattern>/[,<variable>]); <return value> = join(“<seperator>”,<array>);<return value> = join(“<seperator>”,<array>);

Examples:Examples:$string = “This is a string”;$string = “This is a string”;

@words = split(/ /,$string); #splits the string into @words = split(/ /,$string); #splits the string into #separate words #separate words

@words = split(/\s/,$string); #same as above@words = split(/\s/,$string); #same as above

$string = join(“ “,@words); #”This is a string”$string = join(“ “,@words); #”This is a string” Great functions in parsing formatted Great functions in parsing formatted

documentsdocuments

Page 40: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

FunctionsFunctions

Automates certain tasks Automates certain tasks Syntax:Syntax:

sub <name>sub <name>{{

……<statements><statements>

}} Global to the current package. Since Global to the current package. Since

we are not doing OOP and packages, we are not doing OOP and packages, functions are “global” to the whole functions are “global” to the whole programprogram

Page 41: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Functions (cont.)Functions (cont.)

Example:Example:sub say_hellosub say_hello

{{

print “Hello world!\n”;print “Hello world!\n”;

}} Invoking a function:Invoking a function:

say_hello(); #takes in parameterssay_hello(); #takes in parameters

&say_hello; #no parameters&say_hello; #no parameters

Page 42: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Functions (cont.)Functions (cont.)

Return valuesReturn values Two types of functions: void functions Two types of functions: void functions

(also known as routine or procedure), (also known as routine or procedure), and functionsand functions

void functions have no return valuesvoid functions have no return values Functions in Perl can return more than Functions in Perl can return more than

one variable:one variable:sub threeVarsub threeVar

{{

return ($a, $b, $c); #returns a list of 3 variables return ($a, $b, $c); #returns a list of 3 variables

}}

Page 43: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Functions (cont.)Functions (cont.)

($one,$two,$three) = threeVar();($one,$two,$three) = threeVar(); Alternatively:Alternatively:

@list = threeVar(); #stores the three values @list = threeVar(); #stores the three values into a listinto a list

Note:Note:($one, @two, $three) = threeVar(); #$three ($one, @two, $three) = threeVar(); #$three

will not have will not have #any value, why?#any value, why?

Page 44: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Functions (cont.)Functions (cont.)

Functions can’t do much without Functions can’t do much without parametersparameters

Parameters to a function are stored as Parameters to a function are stored as a list with the @_ variablea list with the @_ variable

Example:Example:sub say_hello_twosub say_hello_two{{ $string = @_; #gets the value of the $string = @_; #gets the value of the

parameterparameter}}

Invocation:Invocation:say_hello_two(“hello world!\n”);say_hello_two(“hello world!\n”);

Page 45: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Functions (cont.)Functions (cont.)

For example:For example:sub addsub add

{{

($left,$right) = @_;($left,$right) = @_;

return $left + $right;return $left + $right;

}}

$three = add(1,2); $three = add(1,2);

Page 46: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Functions (cont.)Functions (cont.)

Variables are all global even if they Variables are all global even if they are defined within a functionare defined within a function

my my keyword defines a variable as keyword defines a variable as being private to the scope it is being private to the scope it is defineddefined

For example:For example:sub addsub add{{

my($left,$right) = @_;my($left,$right) = @_; return $left + $right;return $left + $right;}}

Page 47: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Functions (cont.)Functions (cont.)

$three = add(1,2); #$three gets the $three = add(1,2); #$three gets the value of 3value of 3

print “$one\n”; #prints 0print “$one\n”; #prints 0

Print “$two\n”; #prints 0Print “$two\n”; #prints 0

Page 48: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

ExercisesExercises

A trim() function that removes A trim() function that removes leading and trailing spaces in a stringleading and trailing spaces in a string Hint: use the s/// operator in conjunction Hint: use the s/// operator in conjunction

with anchors with anchors A date() function that converts date A date() function that converts date

string, “DD:MM:YY” to “13th of string, “DD:MM:YY” to “13th of December, 2003”December, 2003” Hint: use a hash table to create a lookup Hint: use a hash table to create a lookup

table for the month strings. table for the month strings.

Page 49: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

File I/OFile I/O

FilehandleFilehandle Automatic filehandles: STDIN, Automatic filehandles: STDIN,

STDOUT and STDERR STDOUT and STDERR Syntax:Syntax:

open(<handle name>,”(<|>|>>)open(<handle name>,”(<|>|>>)filenamefilename”);”);close(<handle name>);close(<handle name>);

Example:Example:open(INPUTFILE,”<inputs.txt”); #opens open(INPUTFILE,”<inputs.txt”); #opens

file handlefile handle……Close(INPUTFILE); #closes file handleClose(INPUTFILE); #closes file handle

Page 50: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

File I/O (cont.)File I/O (cont.)

Handle access does not always yield Handle access does not always yield truetrue

Check for return value of the open Check for return value of the open functionfunction

Example:Example: if(open(INPUT,”<inputs.txt”))if(open(INPUT,”<inputs.txt”))

… … #do something#do something

elseelse

print “File open failed\n”;print “File open failed\n”;

Page 51: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

File I/O (cont.)File I/O (cont.)

The previous method is the standard practiceThe previous method is the standard practice Unlike other languages, Perl is for lazy peopleUnlike other languages, Perl is for lazy people Ifs can be simplified by the logical operator Ifs can be simplified by the logical operator

“||”“||” For example:For example:

open(INPUT,”<inputs.txt”) ||die “File open open(INPUT,”<inputs.txt”) ||die “File open failed\n”;failed\n”;

Use $! variable to display additional operating Use $! variable to display additional operating system errorssystem errors die “cannot append $!\n”;die “cannot append $!\n”;

Page 52: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

File I/O (cont.)File I/O (cont.)

Filehandles are similar to standard I/O Filehandles are similar to standard I/O handleshandles

<> operator to read lines<> operator to read lines For example:For example:

open(INPUT,”<inputs.txt”);open(INPUT,”<inputs.txt”);

while(<INPUT>){while(<INPUT>){

chomp;chomp;

print “$_\n”;print “$_\n”;

}} Use print <handle_name> <strings> Use print <handle_name> <strings>

to output to a fileto output to a file

Page 53: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

File I/O (cont.)File I/O (cont.)

File copy example:File copy example:

open(IN,$a) || die "cannot open $a for open(IN,$a) || die "cannot open $a for reading: $!"; open(OUT,">$b") || die reading: $!"; open(OUT,">$b") || die "cannot create $b: $!"; "cannot create $b: $!";

while (<IN>) { while (<IN>) { # read a line from # read a line from file $a into $_ file $a into $_

print OUT $_; # print that line to print OUT $_; # print that line to file $b file $b

} }

close(IN) || die "can't close $a: $!"; close(IN) || die "can't close $a: $!";

close(OUT) || die "can't close $b: $!"; close(OUT) || die "can't close $b: $!";

Page 54: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

File I/O (cont.)File I/O (cont.)

File tests provides convenience for File tests provides convenience for programmersprogrammers

-e –r –w –x –d –f –l –T –B-e –r –w –x –d –f –l –T –B For example:For example:

if(-f $name){if(-f $name){print “$name is a file\n”;print “$name is a file\n”;

}}elsif(-d $name){elsif(-d $name){

print “$name is a directory\n”;print “$name is a directory\n”;}}

Page 55: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Special VariablesSpecial Variables

$_, @_$_, @_ $1, $2… - backreferencing variables$1, $2… - backreferencing variables $_ = "this is a test"; $_ = "this is a test";

/(\w+)\W+(\w+)/; # $1 is "this" and $2 is "is" /(\w+)\W+(\w+)/; # $1 is "this" and $2 is "is" $`, $& and $’ - match variables $`, $& and $’ - match variables

$string = “this is a simple string”;$string = “this is a simple string”;/si.*le/; #$& is now “sample”, $` is “this is /si.*le/; #$& is now “sample”, $` is “this is

a” and $’ is a” and $’ is #“string” #“string” And many more…refer to ActivePerl’s And many more…refer to ActivePerl’s

online documentation for more online documentation for more functionsfunctions

Page 56: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Packages and ModulesPackages and Modules

Concentrate only on their usage in the Concentrate only on their usage in the Greenstone environmentGreenstone environment

Package: a mechanism to protect code Package: a mechanism to protect code from tampering with other package’s from tampering with other package’s variablesvariables

Module: reusable package that is Module: reusable package that is stored in <Name of Module>.dmstored in <Name of Module>.dm

The The ppmppm (Perl Package Manager) for (Perl Package Manager) for Linux and Win32 version of Perl Linux and Win32 version of Perl manages installation and uninstallation manages installation and uninstallation of Perl packagesof Perl packages

Page 57: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Packages and Modules Packages and Modules (cont.)(cont.)

Install the module and put “use Install the module and put “use ModuleName” or “require ModuleName” ModuleName” or “require ModuleName” near the top of the programnear the top of the program

:: qualifying operator allows references to :: qualifying operator allows references to things in the package, such as things in the package, such as $Module::Variable$Module::Variable

So “use Math::Complex module” refers to So “use Math::Complex module” refers to the module Math/Complex.pmthe module Math/Complex.pm

new new creates an instance of the object, creates an instance of the object, then use the handle and operator -> to then use the handle and operator -> to access its functionsaccess its functions

Page 58: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Packages and Modules Packages and Modules (cont.)(cont.)

use use accepts a list of strings as well, so accepts a list of strings as well, so we can access the elements directly we can access the elements directly without the qualifying operatorwithout the qualifying operator

For example:For example:

use Module qw(use Module qw(const1 const2 const1 const2 func1 func2 func3func1 func2 func3); );

const1const1,, const2 const2,, func1 func1,, func2 func2 and and func3 func3 can now be used directly in the can now be used directly in the programprogram

Page 59: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Packages and Modules Packages and Modules (cont.)(cont.)

Perl locates modules by searching the Perl locates modules by searching the @INC @INC arrayarray

The first instance found will be used The first instance found will be used for the module referenced within a for the module referenced within a programprogram

Where to locate modules is an Where to locate modules is an automatic process as the Makefiles automatic process as the Makefiles and PPM take care to place modules and PPM take care to place modules in the correct pathin the correct path

Page 60: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Packages and Modules Packages and Modules (cont.)(cont.)

An example that uses the package An example that uses the package CGI.pm:CGI.pm:use CGI; use CGI; #uses the CGI.pm module#uses the CGI.pm module

$query = CGI::new(); $query = CGI::new(); #creates an instance of #creates an instance of CGICGI

$bday = $query->param("birthday"); $bday = $query->param("birthday"); #gets a #gets a named parameternamed parameter

print $query->header(); print $query->header(); #outputs html #outputs html headerheader

print $query->p("Your birthday is $bday."); print $query->p("Your birthday is $bday."); #outputs text to html#outputs text to html

Page 61: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Packages and Modules Packages and Modules (cont.)(cont.)

Advantages: Encourages code reuse Advantages: Encourages code reuse and less work and less work

Disadvantages: 33% as fast as Disadvantages: 33% as fast as procedural Perl according to the procedural Perl according to the book “object-oriented Perl”, book “object-oriented Perl”, generation of Perl modules involves generation of Perl modules involves some ugly codesome ugly code

Page 62: Perl. Introduction Perl stands for "Practical Extraction and Report Language" Perl stands for "Practical Extraction and Report Language" Created by Larry

Packages and Modules Packages and Modules (cont.)(cont.)

Huge libraryHuge library