A Brief Introduction to Programming with Perl

How to Use Variables and Arrays in Perl Scripts

© Mark Alexander Bain

Jul 11, 2009
Powered by Perl, The Perl Foundation
Perl is a used in many web sites, however it is a powerful programming languages in its own right. This is very evident in the way that it handles variables and arrays.

One of the most surprising things about Perl is just how long it's been around. It was originally written for Unix in 1988, moved across to Linux and is now available for Windows. So, apart from web site developers, why should the Windows programmer (or any other programmer) be interested in this 21 year old programming language?

The answers to that are the same as they've always been. Perl is:

  • open source and free
  • object oriented
  • light and fast
  • easy to extend
  • has flexible data types

And, the power and simplicity of Perl can be experienced just by examining the way in which it handles variables and arrays.

Obtaining Perl

Before they can start using Perl, the programmer must install it. This is easy for the Linux user because it comes already installed in some distros (or distributions), in others it's just a matter of using the distro's installation packages to download and install Perl. For example a Debian Linux user might use:

apt-get install perl

However, the Windows will have a slightly different route. They may install Perl as part of a Linux type environment such as Cygwin (see Cygwin: a Linux-like Environment for Windows) or they may choose a standalone application such as the excellent Strawberry Perl.

Once the programmer has installed Perl on their system then they're ready to try some Perl programming.

Perl and Variables

A Perl variable is identified by the $ (dollar) prefix:

$greeting = "Hello World";
print $greeting;

If this code is saved to a file (for example "helloworld.pl") then it can be run from the command line (as shown if figure 1 at the bottom of this article):

perl helloworld.pl

And the programmer does not have to worry about assigning data types. Perl will do all of that for them:

$version = 0.01;
$version_inc = 0.01;
$app_name = "HelloWorld";
print "$app_name $version \n";
$version += $version_inc; #Equivalent to $version = $version + $version_inc
print "$app_name $version \n";

The "\n" adds a carriage return at the end of each print (as can be seen in figure 2). Without that all of the outputs would be placed on the same line.

Perl and Arrays

Perl arrays are very versatile. They're identified by using the @ prefix, and can be created in a number of ways:

@days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
@planets = qw/Sun Moon Mars Mercury Jupiter Venus Saturn/;

Not only is the way in which they're created flexible, but the way in they're output is flexible as well. For example, the whole array can be output:

print "@days\n";

or individual elements:

print "@days[1]\n"; #Monday

or a combination of elements:

print "@planets[1,2]\n"; #Moon Mars
print "@days[1..5]\n"; #Print Monday to Friday

The outputs from these operations can be seen in figure 3, as can the results of using the arrays in for loops:

for(@days) {print "$_\n";}
for ($i=0; $i^lt;=6; $i++) {
print "@days[$i]'s planet is @planets[$i]\n";
}

With just those few lines of code it is possible to see that Perl gives the programmer much more than many programming languages and is, therefore, well worth the (small amount) of time and effort required to learn this language that has now come of age.


The copyright of the article A Brief Introduction to Programming with Perl in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish A Brief Introduction to Programming with Perl in print or online must be granted by the author in writing.


Powered by Perl, The Perl Foundation
Figure 1: Running a Perl Script, Mark Alexander Bain
Figure 2: Updating Variables with Perl, Mark Alexander Bain
Figure 3: Arrays and Perl, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Aug 4, 2009 10:01 AM
Guest :
Just wondering if it would be better to have background in Linux before taking Perl programming lessons. Thanks.
Aug 5, 2009 2:04 AM
Mark Alexander Bain :
There's no need to know anything about Linux when starting to develop with Perl. The important thing here is that Perl is a cross platform programming language. This means that anything written on a Linux computer will work on a Windows computer. That's assuming that:

- the correct packages have been installed
- any differences in directory structures have been taken into account

And there's a couple of good options for anyone wishing to start programming with Perl on Windows:

- Cygwin
- Stawberry Perl
- ActivePerl

All are very good, but personally I'm leaning towards ActivePerl, simply because of its Perl Package Manager.
2 Comments