|
||||||
A Brief Introduction to Programming with PerlHow to Use Variables and Arrays in Perl ScriptsPerl 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:
And, the power and simplicity of Perl can be experienced just by examining the way in which it handles variables and arrays. Obtaining PerlBefore 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 VariablesA 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 ArraysPerl 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.
Comments
Aug 4, 2009 10:01 AM
Guest :
Aug 5, 2009 2:04 AM
Mark Alexander Bain :
2 Comments
|
||||||
|
|
||||||
|
|
||||||