Perl programming language

26
HOLLY SPIRIT UNIVERSITY OF KASLIK Science faculty PERL PROGRAMMING Introduction to Perl programming language Prepared by Patrick Louis and Elie Obeid Supervised by Dr. Mohamad Awwad Kaslik-Lebanon October 2013

Transcript of Perl programming language

Page 1: Perl programming language

HOLLY SPIRIT UNIVERSITY OF KASLIKScience faculty

PERL PROGRAMMING

Introduction to Perl programming language

Prepared by Patrick Louis and Elie Obeid

Supervised by Dr. Mohamad Awwad

Kaslik-LebanonOctober 2013

Page 2: Perl programming language
Page 3: Perl programming language

Some Facts Around Perl

Perl is not an acronym, but some people refer to it as the Practical Extraction and Report Language.

Perl is a family of languages, it regroups Perl5 and Perl6, Perl 6 is a fork of Perl5. This presentation covers only Perl5.

Perl is a general purpose language, it can be used to do anything, from text parsing to webserver.

Perl is a procedural language with a bit of object oriented programming.

Perl is interpreted. The code is understood only when executed. In fact the code is compiledto a certain level.

Perl is a Dynamic language. Dynamic in the sense of freedom. The credo of Perl is "There is more than one way to do it".

Perl was first made to facilitates text manipulations. It's inspired by *nix tools such as sed, awk, grep...

Page 4: Perl programming language

Data Types

Perl has only 3 data types which simplifies a lot of things.

The Scalars, which are flat data, the Arrays, which combined data together and the Hashes which regroups one to one correspondence between data without any specific order.

Scalars identifiers start with the $ sign. They can contain any flat data. flat meaning a single element, an atom, namely, integers, floats, strings, references.

Page 5: Perl programming language

Arrays

Arrays, are containers for data, their identifiers start with the @ symbol. Arrays in Perl can be heterogeneous.

Let's note that arrays, in Perl, are flattened, meaning that if you insert an array inside another it won't create a 2D array.

To create a multidimensional array you need to use references.The Perl interpreter is smart enough to guess how to use the array in what situation. This means that it will know when you want to loop through it or only have its size.

In Perl everything is straight forward, you want to have a scalar from an array then you use the $ sign to access it. $#array return the index of the last element of the array, not its size (size-1). Like we said before the size is simply @array in a scalar context.

Arrays can be used as stack or queue with the push, pop, shift, unshift functions (Perl predefined).

The push and pop functions add or remove elements from the "right-hand" side of the list. Push adds an item to the end of the list, making it one element longer and pop removes an element from the end of the list. Here is a simple example, using push and pop that use an array named @stack as a stack. Note that it is possible to push more than one item onto a stack at once.

#!/usr/local/bin/perl

@stack = (); #an empty list. # ()

push (@stack, 1); # (1)push (@stack, "Hello"); # (1, "Hello")$value = pop(@stack); # (1)

Page 6: Perl programming language

print "\value: $value \@stack: @stack\n";push (@stack, "Hello", "World"); # (1, "Hello", "World");

print "\value: $value \@stack: @stack\n";

@stuff = ("Goodbye", "Cruel", "World");

push (@stack, @stuff);

print "\value: $value \@stack: @stack\n";

The output from this program:

[/home/angel3/blesser/PerlWork]>stack.plvalue: Hello @stack: 1value: Hello @stack: 1 Hello Worldvalue: Hello @stack: 1 Hello World Goodbye Cruel World[/home/angel3/blesser/PerlWork]> |

In this example we don't actually need the statement@stack = (); #an empty list.

to create an empty array or stack. It is a good idea to do this if you can, but often it is more convenient to let Perl automatically create the array when you first use it in a push function. Here is the same source without the initial empty list assignment. It works the exact same way:

#!/usr/local/bin/perl

push (@stack, 1); # (1)push (@stack, "Hello"); # (1, "Hello")$value = pop(@stack); # (1)

print "\value: $value \@stack: @stack\n";push (@stack, "Hello", "World"); # (1, "Hello", "World");

print "\value: $value \@stack: @stack\n";

@stuff = ("Goodbye", "Cruel", "World");

push (@stack, @stuff);

print "\value: $value \@stack: @stack\n";

In this case the statement:push (@stack, 1);does the following:

1. creates an empty array named @stack2. increases the size of the array by one and3. assigns the scalar number 1 to the first element.

Perl can be very terse.Adding and Removing Elements from the Beginning of an Array.shift ARRAYunshift ARRAY,LIST

Page 7: Perl programming language

The shift function remove the first element in an array (the zeroth element) and returns it. The unshift function adds elements to the beginning of an array. These work just like pop and push respectively except that they work at the front or "left-side" of the array. Here is an example:

#!/usr/local/bin/perl

unshift (@stack, 1); # (1)unshift (@stack, "Hello"); # ("Hello", 1)$value = shift(@stack); # (1)

print "\value: $value \@stack: @stack\n";

unshift (@stack, "Hello", "World"); # ("Hello", "World", 1);

print "\value: $value \@stack: @stack\n";

@stuff = ("Goodbye", "Cruel", "World"); # ("Goodbye", "Cruel", "World", "Hello", "World", 1)

unshift (@stack, @stuff);

print "\value: $value \@stack: @stack\n";

When we simply print out the list in order from 0 to the last element we get the reverse of what we got with push and pop. Of course the arrays is still working as a stack - just the internal order is reversed because we are adding and removing elements from the front.

[/home/angel3/blesser/PerlWork]>backwardStack.plvalue: Hello @stack: 1value: Hello @stack: Hello World 1value: Hello @stack: Goodbye Cruel World Hello World 1[/home/angel3/blesser/PerlWork]> |

Queues and DequeuesSince we can add and remove elements from either end of an array it is very easy to create queues and dequeues. For example to create a queue just push elements onto the end of an array and shift them off the front of the array as needed.Circular ListsIf you need a circular list it is often better to simply "shift" an element from the front of an array and then "push" it on to the end. This doesn't give a truly circular list but can be madeto provide the same functionality. Here is the shift and push in one statement:push(@circularList, shift(@circularList));Ofcourse you can take an element off the end and add it on at the front:unshift(@circularList, pop(@circularList));Slices and SplicesNot satisfied with adding and removing elements from the front and end of arrays you can also access "slices" of an array and insert, or splice, in lists into any position inside an array.Here is an example of getting a "slice" of an array.

Page 8: Perl programming language

#!/usr/local/bin/perl

@anArray = ("Practical", "Pathologically", "Eclectic", "Extraction", "Rubish", "and", "Report", "Language", "Lister");

@acronym = @anArray[0, 3, 5, 6, 7];

print "@acronym\n";print "@anArray\n";

The output from this program:

[/home/angel3/blesser/PerlWork]>slice.plPractical Extraction and Report LanguagePractical Pathologically Eclectic Extraction Rubish and Report Language Lister[/home/angel3/blesser/PerlWork]> |

splice ARRAY,OFFSET,LENGTH,LIST splice ARRAY,OFFSET,LENGTH splice ARRAY,OFFSETThe splice function can insert a list into an array at any offset into the array and will remove(and return) any elements from the offset to the length specified. If length is zero no elements are removed. Here is a simple example:

#!/usr/local/bin/perl

@anArray = (10, 20, 30, 40, 50);@msg = ("Hello", "World");@removed = splice(@anArray, 2, 0, @msg);

print "anArray: @anArray \nremoved: @removed\n";

@msg = ("Practical", "Extraction");@removed = splice(@anArray, 2, 3, @msg);

print "anArray: @anArray \nremoved: @removed\n";

The output from this program:

[/home/angel3/blesser/PerlWork]>splice.planArray: 10 20 Hello World 30 40 50 removed: anArray: 10 20 Practical Extraction 40 50 removed: Hello World 30

Page 9: Perl programming language

[/home/angel3/blesser/PerlWork]> |

Page 10: Perl programming language

Hashes

Hashes are like arrays but without order and with a one to one correspondence (mapping) toaccess the data. Their identifier starts with the % sign.

Hashes can be written with the same syntax as arrays but with an even number of elements. We can access the data inside using the following syntax: “$hash{10}” , 10 being the key of the element we want to access.

Because hashes can contain any data they can be used as "pseudo-objects".Imagine a hash that contains other hashes, data, and references to functions.

Some object oriented programming have been implemented in the recent versions of Perl5.

The bless keyword is used to create the class.

Page 11: Perl programming language

Loops

To loop through arrays we can use the foreach keyword or use a simple for loop.The same can be used for hashes but instead we loop through the keys.

Also, note the my $k after the foreach, it means that $k will take the value of the concerned element in the iterated array or hash at each iteration.

Notice the weird thing here. We call print without argument. So what does it print? In Perl there are default variables like $_, @_, $/, $!.

$_ is the default scalar, @_ is the default array, and $/ is the default delimiter.

Here we are printing the content of $_ it's the same as:

foreach $_ (@ar) {print $_ ." ";

}

Those default variables make the reading of the code harder.

Some people like to play around the "Perl Golf", writing the smallest Perl program to do a task, and the default variables make their code extremely small. Someone even wrote a fully featured web browser with a GUI on a one liner (it can be found on CPAN).

Page 12: Perl programming language

Variable Scope

Let's explain what does the my keyword does.The scope of a variable is delimited by braces, excepts the ones for loops and conditions.The my keyword makes the variable only lives in its scope.The our keyword makes the variable lives globally inside the program.The use strict; statement forces the programmer to use at least one of those keywords while definition or initializing a variable.Notice that the conditions and loop can be written at the end of the statement if there's only one thing to do inside of it.

Outputs

Page 13: Perl programming language

Loops (again)

We already saw the foreach and for. The while does the same thing as in any other language. Notice in the example the difference between " and '. The double quotes considerall special characters inside of it including the scalars. Thus, you don't have to use difficult syntaxes to concatenate text with variables. On the other hand, the single quotes print the text as it is.

Outputs

Page 14: Perl programming language

More About Default Variables

There are some variables which have a predefined and special meaning in Perl. They are the variables that use punctuation characters after the usual variable indicator ($, @, or %), such as $_ (explained below).Most of the special variables have an english like long name eg. Operating System Error variable $! can be written as $OS_ERROR. But if you are going to use english like names then you would have to put the “use English;” at the top of your program file. This guides the interpreter to pickup the exact meaning of the variable.The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines:#!/usr/bin/perl

foreach ('hickory','dickory','doc') {

print $_;

print "\n";

}

When executed, this will produce following result:hickorydickorydocAgain, let's check same example without using $_ variable explicitly:#!/usr/bin/perl

foreach ('hickory','dickory','doc') {

print;

print "\n";

}

When executed, this will also produce following result:hickorydickorydocThe first time the loop is executed, "hickory" is printed. The second time around, "dickory" is printed, and the third time, "doc" is printed. That's because in each iteration of the loop, the current string is placed in $_, and is used by default by print. Here are the places where Perl will assume $_ even if you don't specify it:

• Various unary functions, including functions like ord and int, as well as the all file tests (-f, -d) except for -t, which defaults to STDIN

• Various list functions like print and unlink.• The pattern-matching operations m//, s///, and tr/// when used without an =~

operator.

Page 15: Perl programming language

• The default iterator variable in a foreach loop if no other variable is supplied.• The implicit iterator variable in the grep and map functions.• The default place to put an input record when a line-input operation's result is tested

by itself as the sole criterion of a while test (i.e., ). Note that outside of a while test, this will not happen.

The second mostly used default variable is the @_. It's used a lot in subroutines where it stores the arguments.

Page 16: Perl programming language

Conditions

In the following example, we iterate through an array and use some conditions. This is pretty straight forward except that Perl has as a supplement the opposite of the conditional statements, namely, unless which is if not and until which is while not.

Page 17: Perl programming language

Operators

Let's review the operators for comparison and manipulation.In Perl you can choose between the logical operators: ! or not, || or or, && or and. The <, >, <=, >=, !=, ==, <=>, are used for comparing numerical values.<=> return a negative number in case the number is < than and vice-versa.To compare strings in Perl some special keywords are used: ne (not equal), eq (equal), lt (less than), ge (greater than or equal), le (less than or equal)

Example:If (5 > 4) { print "> for numeric values\n"; }if ('B' gt 'A') { print "gt (Greater Than) for string values\n"; }

The ||, or or can be used in conjunction with the die statement as a mean of error handling.The die() function returns the error message passed by the user or the default one $!.

Example:open “Hello.txt” or die (“Cannot open file”);

The Bitwise operators in Perl.| or& and<< shift left >> shift right^ xor ~ bitwise negation

Page 18: Perl programming language

Regex

Let's rapidly go over the best feature of Perl: Regex, replacement, transliteration, and the ability to use any delimiter.

We can use the qq$something$ to delimit text. You can replace $ with anything you want. It's easy in Perl to write long numbers, all you have to do is use an underscore inside of it.

Print 10_000_000;

Transliteration or translation is used to change a series of characters to another series of characters. The example makes $_ becomes “eHllo”

Example:

$_ = “Hello”;

$_ =~ tr/He/eH/;

The back reference of the regex are store in $1, $2 ..

Page 19: Perl programming language

Regular expressions give Perl its power. Perl regex are very popular. A lot of other programming languages and tools include them, namely, Python, PHP, Ruby, grep, etc..

The world of Regex is huge and hard to cover. Let's just say that they make parsing and replacing text, extremely easy.

Page 20: Perl programming language

Functions (Subroutines)

When talking about functions in Perl you are referring to the built in Perl functions like shift, print... The user written ones are called subroutines. The syntax is:

sub fooname {}

You can, at will, choose to force the user to pass some arguments to the subroutine with ($$$), $$$ being 3 different arguments. Note that the arguments are flattened, like with the arrays. To call a subroutine just write its name. However, if the subroutine is written after the call it must be called with () and have a prototype at the top of the code. To pass an array to the subroutine you must pass its reference. The arguments of the subroutine are stored inside @_.

The return can be explicit or it can simply be the last line of the subroutine.Subroutines in Perl can be written with parenthesis or not. It's the choice of the programmer.

Page 21: Perl programming language

Recursion

Can you guess what the following subroutine does?

<STDIN> is a way to take the standard input (user input).

This function is the Fibonacci sequence. This example shows exactly how Perl can become hard to read.

Page 22: Perl programming language

Comparing Perl To Other Languages

Variables and Data typesBeing a dynamically typed language, data-types in Perl are a much different subject than in C. In Perl, the type of a variable has a lot more to do with how it organizes data it contains than what is considered a type in C. For instance, there is no integer, string, character, or pointer type in Perl. Strings, characters, and numbers are all treated roughly interchangeably because they can be converted from one to the other internally by the Perl interpreter at runtime.Hence, all of the numeric data types in C, pointers, and even strings, all correspond to a single data-type in Perl: the scalar. Values that are not single values may be stored in arrays, as in many other languages. Unlike in C++, the size of an array in Perl can shrink or grow as needed, and need not even consist of a contiguous range of index. The Perl interpreter is *intelligent* enough to know in what context the data is being used. Note that Perl doesn't have constants by default. To have readonly variable you need to use an external module.

Dynamic TypingUnlike C, which is statically typed, Perl is dynamically typed. What this means is that the datatype of any particular scalar variable is not known at compile-time. Moreover, with dynamic typing in Perl, data types are automatically converted to one-another and reassigned as necessary. However, dynamic typing does not always makes thing easier. Many compile-time checks that a C or Java programmer are accustomed to are impossible in a language that doesn't know the type of variables at compile-time. For instance, if you misspell the name of a method to an object, Perl will only notify you of that error when that particular line is executed. This aspect of dynamic typing means that thorough testing is especially importantfor programs written in Perl.To ensure that the maximum number of compile-time checks and warnings are reported, you should always use two pragmas near the top of your programs:use strict;use warnings;The use strict pragma introduces several compile-time checks to your code. The most immediately apparent of these changes is that variables must be explicitly defined (my and our). For instance, without 'use strict', the following code is valid and will print "Hello world":$something = "Hello World"; # Create a variable out of thin airprint $something;However, with use strict, the first line must be changed to readmy $something = "Hello world";or a compile-time error will be reported. In the example above, having the variable $something just appear is convenient, in that it saves three characters of typing. However, imagine the slightly different program:$somehting = "Hello world";print $something;

Page 23: Perl programming language

In the code above, Perl will create a new variable called '$somehting' (a misspelling) and store "Hello world" in it. The next line was intended to print "Hello world" but because it refers to a different variable named "$something", it will print nothing. The second variable, "$something" will automatically be created for the print statement and be initialized to an empty string.

OperatorsPerl has most of the basic arithmetic and logic operators that C has, plus a few more. For instance, +, -, /, * and = all work in the same way as in C, and combinations like '+=' and '*=' also work the same way. One arithmetic operator that Perl has and C does not is the exponentiation operator, **.Perl also behaves differently in terms of logic operators. As in C, the number 0 is considered to have a false truth value, and all other numbers are considered to be true. However, in Perl, other non-numeric values also have truth values. In all, the special value undef, the number 0, an empty string "", and an empty list (), all constitute false values, and all other values are considered true when used in boolean expressions. This fact makes possible the very interesting behavior of Perl's logical or and and operators.Whereas the result of a boolean expression in C is simply a truth value, the result of a boolean expression in Perl is the value of the last term evaluated. This sounds confusing, but an example makes it clear.my $val = 'Ketchup' || 'Katsup';The example above stores a true value in $val, because both 'Ketchup' and 'Katsup' are true values and so the result of or'ing them together is also a true value. Specifically, however, $val will contain 'Ketchup', because that is the value of the last term evaluated. The second term in the or expression didn't need to be evaluated because the first term was true. Similarly,my $val = 0 || 'Katsup';stores Katsup in $val, because 0 is a false value and so the second term had to be evaluated to determine the truth of the expression as a whole.The logical and operator, && (and), works in a similar fashion.

Control structuresMost of Perl's basic control structures are the same or very similar to those in C. For instance, Perl has if statements, while, do-while, and for loops.The syntax for the if case differs only for the else if which is written elsif in Perl.Perl has also until and unless.A for loop in Perl's can very similar to their equivalents in C but it's not very Perl-like:for (my $i = 0; $i < 10; $i++){foo();}One difference between Perl's loops and C's is that in C, the break and continue operators exit and iterate through the loop, respectively, whereas in Perl those same tasks are performed by the last and next operators.Following Perl's TIMTOWTDI (There's more than one way to do it) philosophy, Perl offersseveral variations of these basic structures. For instance, the if statement has a post-fix formthat looks like this:foo() if ($true);Also, Perl has an unless statement that is equivalent to a negated if statement:

Page 24: Perl programming language

unless ($false){ bar();}While fun and potentially useful in certain situations, these alternative forms should be usedwith caution. Programmers scan code with their eyes to gain a preliminary idea of its structure and these alternative forms usually make this task more difficult. One exception that is generally acceptable is using the postfix if operator in conjunction with the next or last operators. For instance,while (1) {

last if ($quit);next if ($something);bar();

}

Subroutines Vs FunctionsSubroutines in Perl serve the same purpose as functions in C. However, subroutines in Perl are much more flexible. Whereas in C, each function must be declared before its use, in Perl this is not necessary because the availability of subroutines is evaluated during runtime. That is one reason that you can misspell a subroutine name in Perl and it won't be caught until that line of code is actually executed.Also, subroutines in Perl do not have formal argument lists as functions in C do. Consequently, the most basic syntax for a subroutine is extremely sparse:sub foo{}

Perl IdiomsPerl community that take advantage of the unique features of Perl.For instance, it is very common in C to wrap nearly every function call that may return an error code in an if statement. It is entirely possible to do the exact same thing in Perl, but Perl offers a much cleaner syntax for most cases usings its or operator. As we saw in the previous example with the “do_something or die()”.The Perl-like way of iterating through arrays/scale is much simpler than the C-way:foreach (0 .. 9){

Print "This is iteration $_\n";}

CPAN and PerldocsThe Perl module repository CPAN is huge and has almost anything you can dream of. It actas a package manager and makes the install process extremely easy, unlike the way of installing C headers and dynamic-libraries.With each modules comes a manual that describe how to use the module. It is offered as a Unix man page. For developers this means that once the module is installed they can consult all the available documentation on their own machine.All the default Perl functions are also documented the same way.

Page 25: Perl programming language

Perl Criticism

Perl has been referred to as "line noise" by some programmers who claim its syntax makes it a write-only language, as seen in the Fibonacci example. The Perl overview document perlintro states that the names of built-in "magic" scalar variables "look like punctuation or line noise". Also, Perl is interpreted so it is slower than compiled languages like C.

Page 26: Perl programming language

Conclusion

Perl is a fast interpreted language that is easy to use and learn. It has a lot documentation and a huge library of pre-made modules for almost anything you can think of. Perl can lookintimidating and hard to read at first look because of the default variables and inverted statements. Perl is mostly popular because of its regex and substitute powers. The Perl specific syntax for regex became the norm for other programming languages. In a nutshell, Perl is the duct-tape of programming languages.