Download - Perl -Basic Programming concepts.

Transcript
Page 1: Perl -Basic Programming concepts.

By Tanuj Maheshwari

Perl

Page 2: Perl -Basic Programming concepts.

IntroductionPerl stands for "Practical Extraction and

Report Language" Created by Larry Wall when awk ran out of

steam Perl grew at almost the same rate as the

Unix operating system

Page 3: Perl -Basic Programming concepts.

Introduction (cont.)Perl fills the gaps between program

languages of different levelsA great tool for leverageHigh portability and readily available

Page 4: Perl -Basic Programming concepts.

AvailabilityIt's free and runs rather nicely on nearly

everything that calls itself UNIX or UNIX-like

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

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

Page 5: Perl -Basic Programming concepts.

Running Perl on UnixSetup path variable to point to the

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

<filename>” Alternatively, change the file attribute to

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

The .pl extension is frequently associated to Perl scripts

Page 6: Perl -Basic Programming concepts.

Running Perl on Win32ActivePerl allows Perl scripts to be executed in

MS-DOS/WindowsPerl is being ported faithfullyThe #! directive is no longer used because it

does not mean anything to MS-DOS/WindowsPerl scripts are executed by typing “perl

<filename> Alternatively, double clicking on the file if the

extension .pl is being associated to the Perl interpreter

Page 7: Perl -Basic Programming concepts.

An Example#!/usr/bin/perlprint “Hello World!”;

The #! directive directs subsequent lines in the file to the perl executable

All statements are terminated with ; as in C/C++/Java

print by default outputs any strings to the terminal console. (such as printf in C or cout in C++)

Perl completely parses and compiles the script before executing it

Page 8: Perl -Basic Programming concepts.

VariablesThree main types of variables, Scalar ArrayHash Variables are all global in scope unless

defined to be private or localNote: remember that hash and array are

used to hold scalar values

Page 9: Perl -Basic Programming concepts.

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

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

last element #from @array

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

Page 10: Perl -Basic Programming concepts.

Examples (cont.)Assigning values to a hash

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

#or using a hash slice@hash{“greeting”,”available”} = (“Hello world!”, 1);

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

Page 11: Perl -Basic Programming concepts.

Conditional StatementsVariables alone will not support switches or

conditionsIf-Then-Else like clauses are used to make

decisions based on certain preconditionsKeywords: if, else, elsif, unlessEnclosed by ‘{‘ and ‘}’

Page 12: Perl -Basic Programming concepts.

A Conditional Statement Example

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

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

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

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

$name = <STDIN> reads from standard input

chomp is a built-in function that removes newline character

Page 13: Perl -Basic Programming concepts.

LoopsConditional statements cannot handle

repetitive tasksKeywords: while, foreach, for , until, do-while,

do-untilForeach loop iterates over all of the elements

in an array or hash, executing the loop body on each element

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

Page 14: Perl -Basic Programming concepts.

Loops (cont.)Do-while and do-until loops executes the

loop body once before checking for termination

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

Page 15: Perl -Basic Programming concepts.

While LoopSyntax:

while(some expression){statements;…

}Example:

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

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

Page 16: Perl -Basic Programming concepts.

Until LoopSyntax:

until(some expression){statements;…

}Example:

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

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

Page 17: Perl -Basic Programming concepts.

Foreach LoopSyntax:

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

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

print $b; }

Page 18: Perl -Basic Programming concepts.

Foreach Loop (cont.)Accessing a hash with keys function:

foreach $key (keys (%fred)) { # once for each key of %fred print "at $key we have $fred{$key}\n";

# show key and value }

Page 19: Perl -Basic Programming concepts.

For LoopSyntax:

For(initial_exp; test_exp; re-init_exp ) { statements;

…}

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

print "$i ";

}

Page 20: Perl -Basic Programming concepts.

Do-While and Do-Until LoopsSyntax:

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

some_expression;

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

Page 21: Perl -Basic Programming concepts.

Built-in functionsshift function

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

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

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

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

Page 22: Perl -Basic Programming concepts.

Built-In Functions (cont.)qw function

Ex: @words = qw(camel llama alpaca); # is equivalent to @words = (“camel”,”llama”,”alpaca”);

defined function Returns a Boolean value saying whether the

scalar value resulting from an expression has a real value or not. Ex: defined $a;

undefined function Inverse of the defined function

Page 23: Perl -Basic Programming concepts.

Built-In Functions (cont.)uc and ucfirst functions –vs- lc and lcfirst

functions<result> = uc(<string>)<result> = ucfirst(<string>)$string = “abcde”;$string2 = uc($string); #ABCDE$string3 = ucfirst($string); #Abcde

Lc and lcfirst has the reverse effect as uc and ucfirst functions

Page 24: Perl -Basic Programming concepts.

Basic I/OSTDIN and STDOUTSTDIN Examples:

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

{ # process $line here } STDOUT Examples:

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

Page 25: Perl -Basic Programming concepts.

Regular ExpressionsTemplate to be matched against a stringPatterns are enclosed in ‘/’sMatching against a variable are done by the =~

operatorSyntax: /<pattern>/Examples:

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

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

Page 26: Perl -Basic Programming concepts.

Creating PatternsSingle character patterns:

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

“?” matches zero or one of the preceding character

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

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

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

Page 27: Perl -Basic Programming concepts.

Creating Patterns (cont.)Predefined character class abbreviations:

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

Page 28: Perl -Basic Programming concepts.

Creating Patterns (cont.)Multipliers: *, + And {}* matches 0 or more of the preceding

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

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

characterab+c matches a followed by one or more bs and

followed by a c{} is a general multiplier

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

Page 29: Perl -Basic Programming concepts.

Creating Patterns (cont.)a{3} #matches any string with more than

three “a”s in itComplex patterns can be constructed from

these operatorsFor examples:

/a.*ce.*d/ matches strings such as

“asdffdscedfssadfz”

Page 30: Perl -Basic Programming concepts.

Creating Patterns: ExercisesConstruct patterns for the following strings:

1. "a xxx c xxxxxxxx c xxx d“2. a sequence of numbers3. three or more digits followed by the

string “abc” 4. Strings that have an “a”, one or

more “b”s and at least five “c”s

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

Page 31: Perl -Basic Programming concepts.

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

Page 32: Perl -Basic Programming concepts.

Anchoring PatternsNo boundaries are defined by the previous

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

and vice verseExamples:

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

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

Page 33: Perl -Basic Programming concepts.

Anchoring Patterns (cont.)^ and $^ matches beginning of a string$ matches end of a stringExampls:

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

Page 34: Perl -Basic Programming concepts.

More on matching operatorsAdditional flags for the matching operator:

/<pattern>/i #ignores case differences/fred/i #matches FRED,fred,Fred,FreD and etc…

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

Page 35: Perl -Basic Programming concepts.

More on Matching Operators (cont.)

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

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

For example:$string = “Hello World!”;if( $string =~/(\w*) (\w*)){

#prints Hello World print “$1 $2\n”;}

Page 36: Perl -Basic Programming concepts.

More on Matching Operators (cont.)

Alternatively:$string = “Hello World!”;($first,$second) = ($string =~/(\w*) (\w*));print “$first $second\n”; #prints Hello World

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

Page 37: Perl -Basic Programming concepts.

SubstitutionReplacement of patterns in strings/<pattern to search>/<pattern to

replace>/igi is to case insensitiveg enables the matching to be performed

more than onceExamples:

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

this”

Page 38: Perl -Basic Programming concepts.

Substitution (cont.)$which =~ s/this/that/g; #produces “that that that”$which =~ s/THIS/that/i; #produces “that this this”$which =~ s/THIS/that/ig; #produces “that that that”

Multipliers, anchors and memory operators can be used as well:$string = “This is a string”;$string =~ s/^/So/; # “So This is a string”$string =~ s/(\w{1,})/I think $1/; # “I think This is a

string”

Page 39: Perl -Basic Programming concepts.

Split and Join FunctionsSyntax:

<return value(s)> = split(/<pattern>/[,<variable>]);

<return value> = join(“<seperator>”,<array>);Examples:

$string = “This is a string”;@words = split(/ /,$string); #splits the string into

#separate words

@words = split(/\s/,$string); #same as above$string = join(“ “,@words); #”This is a string”

Great functions in parsing formatted documents

Page 40: Perl -Basic Programming concepts.

FunctionsAutomates certain tasks Syntax:

sub <name>{

…<statements>

}Global to the current package. Since we are

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

Page 41: Perl -Basic Programming concepts.

Functions (cont.)Example:

sub say_hello{

print “Hello world!\n”;}

Invoking a function:say_hello(); #takes in parameters&say_hello; #no parameters

Page 42: Perl -Basic Programming concepts.

Functions (cont.)Return valuesTwo types of functions: void functions (also

known as routine or procedure), and functionsvoid functions have no return valuesFunctions in Perl can return more than one

variable:sub threeVar

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

}

Page 43: Perl -Basic Programming concepts.

Functions (cont.)

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

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

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

have #any value, why?

Page 44: Perl -Basic Programming concepts.

Functions (cont.)Functions can’t do much without parametersParameters to a function are stored as a list

with the @_ variableExample:

sub say_hello_two{ $string = @_; #gets the value of the parameter}

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

Page 45: Perl -Basic Programming concepts.

Functions (cont.)For example:

sub add{

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

$three = add(1,2);

Page 46: Perl -Basic Programming concepts.

Functions (cont.)Variables are all global even if they are

defined within a functionmy keyword defines a variable as being

private to the scope it is definedFor example:

sub add{

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

Page 47: Perl -Basic Programming concepts.

Functions (cont.)$three = add(1,2); #$three gets the value of

3

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

Page 48: Perl -Basic Programming concepts.

ExercisesA trim() function that removes leading and

trailing spaces in a stringHint: use the s/// operator in conjunction with

anchors A date() function that converts date string,

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

table for the month strings.

Page 49: Perl -Basic Programming concepts.

File I/OFilehandleAutomatic filehandles: STDIN, STDOUT and

STDERR Syntax:

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

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

handle…Close(INPUTFILE); #closes file handle

Page 50: Perl -Basic Programming concepts.

File I/O (cont.)Handle access does not always yield trueCheck for return value of the open functionExample:

if(open(INPUT,”<inputs.txt”))… #do something

elseprint “File open failed\n”;

Page 51: Perl -Basic Programming concepts.

File I/O (cont.)The previous method is the standard practiceUnlike other languages, Perl is for lazy peopleIfs can be simplified by the logical operator “||”For example:

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

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

Page 52: Perl -Basic Programming concepts.

File I/O (cont.)Filehandles are similar to standard I/O handles<> operator to read linesFor example:

open(INPUT,”<inputs.txt”);while(<INPUT>){

chomp;print “$_\n”;

}Use print <handle_name> <strings> to output

to a file

Page 53: Perl -Basic Programming concepts.

File I/O (cont.)File copy example:

open(IN,$a) || die "cannot open $a for reading: $!"; open(OUT,">$b") || die "cannot create $b: $!"; while (<IN>) { # read a line from file $a into $_

print OUT $_; # print that line to file $b } close(IN) || die "can't close $a: $!"; close(OUT) || die "can't close $b: $!";

Page 54: Perl -Basic Programming concepts.

File I/O (cont.)File tests provides convenience for

programmers-e –r –w –x –d –f –l –T –BFor example:

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

}elsif(-d $name){

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

Page 55: Perl -Basic Programming concepts.

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

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

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

is #“string”And many more…refer to ActivePerl’s online

documentations for their functions

Page 56: Perl -Basic Programming concepts.

Packages and ModulesConcentrate only on their usage in the

Greenstone environmentPackage: a mechanism to protect codes from

tempering each other’s variablesModule: reusable package that is stored in

<Name of Module>.dmThe ppm (Perl Package Manager) for Linux

and Win32 version of Perl manages installation and uninstallation of Perl packages

Page 57: Perl -Basic Programming concepts.

Packages and Modules (cont.)

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

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

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

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

Page 58: Perl -Basic Programming concepts.

Packages and Modules (cont.)

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

For example:use Module qw(const1 const2 func1 func2

func3); const1, const2, func1, func2 and func3 can now

be used directly in the program

Page 59: Perl -Basic Programming concepts.

Packages and Modules (cont.)

Perl locates modules by searching the @INC array

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

Where to locate modules are an automatic process as the Makefiles and PPM take care placing modules in the correct path

Page 60: Perl -Basic Programming concepts.

Packages and Modules (cont.)An example that uses the package CGI.pm:

use CGI; #uses the CGI.pm module

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

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

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

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

Page 61: Perl -Basic Programming concepts.

Packages and Modules (cont.)Advantages: Encourages code reuse and

less work Disadvantages: 33% as fast as procedural

Perl according to the book “object-oriented Perl”, generation of Perl modules involves some ugly codes

Page 62: Perl -Basic Programming concepts.

Packages and Modules (cont.)Huge library

Page 63: Perl -Basic Programming concepts.

ThanksTanuj maheshwari