Basic Perl Tutorial by hcw25539

VIEWS: 73 PAGES: 10

									                                 Basic Perl Tutorial
                                     Computer Taskforce
                                     Wed Feb 18th, 2004


    This is a basic Perl tutorial. It is meant as an introduction to the language, how it is
powerful, and its uses. It describes how it is different from common languages and the advn-
tages/disadvantages of these differences. Basicaly familiarity with imperative proramming is
assumed. It does not show *all* of Perl’s uses: in particular Object-Oriented Programming,
web-cgi, networking, graphics, etc. will not be discussed (but are all possible using Perl!).


1     History and the basics
I’ll begin with the basics. What about Perl shines? What sticks out?

1.1    Perl Facts
    • Perl stands for ”Practical Extract and Report Language”.

    • Perl is a stable, cross platform programming language.

    • It is used for mission critical projects in the public and private sectors.

    • Perl is Open Source software, licensed under its Artistic License, or the GNU General
      Public License.

    • Perl was created by Larry Wall.

    • Perl 1.0 was released to usenet’s alt.comp.sources in 1987

Perl is a programming/scripting language. Why is it considered almost both a programming
language and a scripting language? Because it combines the power of both: it is a bit more
higher-level than the typical shell-scripting languages in that it contains high-level structures
and concepts (eg. hashes, lists, references, libraries(modules) ...) but also strong low-level
capabilities such as easy calling of subprograms, capturing I/O, parsing, piping, and access to
the Operating System.

Perl is an interpreted language. It is essentially a wrapper around C function calls. Therefore,
every command in your Perl programs will be converted to an equivalent set of C function calls
that will be executed by the Perl interpreter (written in C). No compilation is ever done, the
interpreter simple interprets the Perl program and calls the routines directly associated with

                                                 1
each set of statements. This is also what makes Perl programs so portable. And generally, it is
much simpler to write a Perl program to perform a function than it is to write its equivalent
C program. However, since you are running an intepreter as a layer to the actual machine
function calls, the program will run slower than its equivalent compiled C program.

Perl has powerful parsing capabilities.

Perl has a huge database of contributed modules (CPAN) that make rapid-programming pos-
sible.

Perl has regular expressions built-in.

Perl has a very lax, easily abusable syntax. This makes Perl a very powerful language in
the sense that the least amount of writing possible will allow programmings to do amazng
things. However, it also encourages ugly/unreadable programs. It is therefore very important
to document your programs properly. All examples included below will train you to avoid
certain common Perl pitfalls.

1.2    Tutorial Format
The format for the tutorial will be as follows. The tutorial is broken down into sections. Each
section contains basic concepts which should be learned in sequence. The tutorial is example-
based: this should allow you to easily jump right in. This tutorial should also serve as a nice
first reference while learning the basics.


2     Documentation
A flood of man pages can be found by issuing the following command:

man perl

Documentation for existing modules, commands, etc. can be retrieved from the commandline
via:

perldoc

or via web pages such as:

    • http://www.perl.org/

    • http://www.perldoc.com/

    • http://www.cpan.org/




                                              2
3     A first Perl program
Let’s quickly get down and dirty. I will present to you a very first Perl program, and will
explain it line-by-line.

#!/usr/bin/perl
# Example1: hellotaskforce.pl
use strict;
use warnings;

print "Hello, Taskforce!\n";

Now let’s disect it.

The first line is actually a comment. It serves no purpose other than to tell the OS beneath
it where the Perl interpreter is found on the current system. This is only needed when run-
ning the program as a script via ./hellotaskforce.pl instead of perl hellotaskforce.pl.
This should be the first line of every Perl program, so we will no longer write it in our examples.

The second line is a comment that just names the program.

The 3rd and 4th lines are use directives. The use directives tell the Perl interpreter what mod-
ules to load before continuing. These modules are contained in special Perl directories or are
internal. We will always include these 2 special modules. The first one forces the programmer
to adhere to a strict(er) syntax when interpreting programs (errors are given before the program
is attempted to run if the syntax is not followed). The warnings tell the interpreter to print
out warning messages when it suspects something ”fishy”, such as using an unassigned variable.

The 6th line is quite obvious. It prints out the specified line.


4     Basic Types
There are 3 basic kinds of variables in Perl: scalars, lists/arrays, and hashes. These are im-
portant kinds that serve as a foundation to using variables in Perl, so let’s go through some
examples of variable usage.

Before we get started, we must explain something fundamental. Perl is strictly untyped or
dynamically typed. It is untyped from the user’s standpoint: a scalar does not have a type,
nor do lists or hashes. However, in the background, these variables actually are typed; type
conversions are done for the user dynamically behind the scenes. This mechanism takes away
a lot of the headaches involved in strongly-typed languages.

Each kind of variable has a symbol associated with it. Whenever you refer to those variables,
you must prepend the variable name by its symbol.



                                                3
4.1    Scalars
The basic kind/type(not to be confused with the normal programmatical definition of ’type’)
of variable is called the scalar. Scalar values are the basic data types. Scalars can contain
numbers (integers, single-precision floating point, double-precision floating point) and strings.
Characters are strings of length 1. Let’s see how these are used:


# Example 2:    Scalar variables.
my $scalar1=    34;
my $scalar2=    "Hello there";
my $scalar3;
$scalar3= ’,    #’;

print $scalar2 . $scalar3 . $scalar1 . "!\n";

There are a few things to note about this program. The first is that the symbol that represents
scalar variables is the dollar sign ($).

The first line declares a scalar variable and sets it equal to 34. The second declares a string
scalar and assigns it the value of Hello there. The third scalar is also a string with a value
of , #. You should also have noticed that variabkle declaration requires the my keyword. Also,
strings can be represented in single quotes or double quotes.
2 other important observations await us. You might be bothered by the fact that calling a
function (print) requires no brackets to enclose its arguments. Get used to this. In Perl,
arguments can be passed (separated by commas) without being enclosed in brackets. Every
function call takes 1 argement: a list of values. You could enclose your arguments in brackets
if you like, they’ll still be considered a list. You’ll see how lists are manipulated soon.

The other important operator used here is concatenation. The dot operator is used for this.
You should notice that some of the magic of Perl happening behind the scene: scalar1 was
dynamically converted to a string before being printed out. Time for another example:

# Example 3: Scalar variables cont’d.
my $num= 65;
my $float= 45.5;
my $string= "554";

my $sum= $num + $string;
my $sum2= $num + $float;
my $sum3= $float + $string;

print "$sum $sum2 $sum3\n";

   The first 3 lines are obvious: we’re declaring 3 scalars: an integer, a float, and a string. The
second set of 3 lines declares more scalars which are sums of the first 3. sum will convert the

                                               4
string ”554” to 554 and add it to 65 to give 719. sum2 will convert num to a float and add it
to 45.5 to give 100.5 (default is set to not lose precision in dynamic type converstions). In the
third sum, string will be converted to a float to give a sum of 599.5.

    Did you also notice that the code refered to variables inside the actual string that was being
printed? This is another cool thing about Perl which saves a bit of concatenation grief.

   But what if you had wanted to instead convert the float to an integer (lose precision), we
could have casted it like so:

# Example 3: Scalar variables cont’d.
my $num= 65;
my $float= 45.5;

my $sum= $num + int($float);

4.2    Lists and Arrays
Now for some more interesting variable ”types”. First of all, let’s say one thing: there is no
such thing as fixed-length arrays in Perl. Arrays grow dynamically. That’s why we Perl hackers
prefer to call them lists, because that’s what they are. Of course, we use them like arrays, so we
also sometimes call them arrays. Just remember that in Perl, arrays are lists. List are arrays.
And so on.

# Example 4: Lists or Arrays, that is the question.
my @list= ();
my @otherlist= (3, 4, 5);

my $x= $otherlist[0] + $otherlist[1] + $otherlist[2];
print $x;

Here, the first line declares an empty list. The second creates a list with 3 scalar elements.
Notice how we refer to the elements later, however. Array indicies start from 0. Did you notice
that we’re using a different sign for lists? The at symbol (@) is used to signify a list ”type”.
Except, when you refer to one of the elements in the list, you must prepend the variable name
with a $ since we’re actually referring to a scalar type inside the list. The number inside the [
] is the index of the element in the list/array.

Now, one big difference about Perl arrays/lists is that they are uni-dimensional, no matter
how much you’d like to make them 2-dimensional, they just get squashed. Let’s consider the
following example.

# Example 5: Lists or Arrays, that is the question.
my @list1= (3, 4, 5);
my @list2= (6, 7, 8);
my @list3= (9, 10, 11);

                                                5
my @list4= (1, 2, @list1, @list2, @list3);

# the result list is (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
# not (1, 2, (3, 4, 5), (6, 7, 8), (9, 10, 11))

A practical operation on arrays/lists is splicing.

# Example 6: Splicing
my @list= (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
my @splice= @list[2 .. 5];

# @splice is then (3, 4, 5, 6)

See the splice built-in function for advanced splicing, perldoc -f splice. We’ll see a lot more
useful operations on lists later.

4.3    Hashes
Hashes are by far the coolest basic types. Hashes are basically maps. Think of them as a list of
key value pairs. When first initializing a hash, you must pass in the values as a list containing
and even number of elements. These will be interpreted as a sequence of key-value pairs.

# Example 7
my %squares= (1,1,2,4,3,9);

print $squares{3};
# prints 9

is the same as:

# Example 8
my %squares= ();

$squares{1}= 1;
$squares{2}= 4;
$squares{3}= 9;

print $squares{3};

   The 3 basic operations on hashes: keys, values, delete. keys retrieves the list of keys from
a hash, values retrieves the list of values, and delete removes a key (with its associated value)
from a hash. We’ll see many examples of these used later, but here is a very simple example:

# Example 9
my %squares= (1,1,2,4,3,9);


                                                6
delete $squares{2};
my @keyset= keys(%squares);

# @keyset is (1,3)

Note that the keyset is not necessarily sorted in any order, as is implied here.

4.4    References
Let’s say you want to have a list of lists. This should be possible. Well, it is! However, you
must use references. References are scalar variables that refer to other types which can be
scalars or not.

# Example 10
my $num= 30;
my $ref= \$num;

print "$num $$ref\n";

Here, ref is a reference to the scalar num. The scalar value itself is contained in $ref but is
uninteresting. The value of the variable that the reference refers to is $$ref because the value
being referenced is a scalar. Let’s see how to manipulate references to lists.


# Example 10
my @list1= (1,2,3,4,5);
my @list2= (6,7,8,9,10);
my $listref= \@list2;
my %hash= (1,2,3,4);
my $hashref= \%hash;

my @listofrefs= (\@list1, \@list2);

print ${$listofrefs}[0]; # prints 1

print $listref->[0]; # prints 6

print $hashref->{1}# prints 2


    Note the different use in syntax. The -> operators directly refers to a value being referenced
to, in the case of lists and hashes, as seen above. The syntax can get quite complicated, and
the advanced use of references is beyond the scope of this tutorial, so we will omit it. Note
however, that passing references to functions is useful when you want to keep the modifications
when the function call has terminated.


                                                7
5   Built-in functions
# Example 11: Split
my $string= "Hello my big, fat, blue world!";
my @parts= split(’ ’, $string);
# @parts is ("Hello", "my", "big,", "fat,", "blue", "world!")

# Example 12: Chomp
my $string= "Hello there     ";
chomp($string);
# $string is now "Hello there"

# Example 13: Join
my @list= (1,2,3);
my $string= join(’:’, @list);
# $string is now "1:2:3"

# Example 14: Shift
my @list= (1,2,3);
my $element= shift @list;
# $element is 1, @list is (2,3)

# Example 15: Push
my @list= (2,3);
push (@list, 4);
# @list is (2,3,4);


6   Control Flow
# Example 16: if
my $num= 2; my $string= "bla";
if ($num == 2 && $string eq "bla")
{
   #this will be reached
}

# Example 17: while
my $num= 0;
while ($num < 17)
{
   print "$num ";
}
print "\n";




                                      8
# Example 18: for
for (my $i= 0; $i < 10; $i++)
{
   my $square= $i*$i;
   print "The square of $i is $square\n";
}


7    Basic I/O
# Example 19: Echo server
my $line= "";
while($line= <STDIN>) # wait until catch EOF
{
   chomp($line);
print "$line\n";
}
# Example 20: Copying a text file
my $line= "";
open(INFILE, ’<’, "in.txt") or die "Cannot open in.txt for reading\n";
open(OUTFILE, ’>’, "out.txt") or die "Cannot open out.txt for reading\n";
while ($line= <INFILE>) # wait until catch EOF
{
chomp($line);
print OUTFILE "$line\n";
}


8    CPAN
CPAN is the Comprehensive Perl Archive Network and is located at http://www.cpan.org.
It is a large collection of Perl modules that people have contributed and lie downloadable and
installable directly through a Perl interface thatcomes with Perl! There exists many mirrors
that hold all these modules.

To access the archive, at the command prompt type:

perl -MCPAN -e shell
You will have many questions to answer. When done, type install Bundle::CPAN which will
download, install, and possibly compile/make the required base modules for CPAN.

Once that’s done, all you need to do is download any desired module (eg. HTML::Parser) via
the same install command in the cpan shell: install HTML::iSimpleParse and you can now
use the module in your programs by adding a use modulename to the top of your programs,
like so:

                                              9
# Example 21: HTML parsing
use LWP::UserAgent;
use HTTP::Request;
use HTML::SimpleParse;

my $homepage;

my URL= "http://,tg.dynip.com/scripts/index.exe?page=900";
my $ua= LWP::UserAgent->new;
my $response= HTTP::Request->new(GET => $homepage);

my $html= $response->content;
my $parser= new HTML::SimpleParse($response->content);

my @parsetree= $parser->tree;

# ...

Note: documentation and examples for these modules are usually found from the CPAN page
directly.




                                          10

								
To top