The Perl Tutorial: What's Perl? (2)
Running a Perl Program
Note: This will not teach you everything about Perl but it should give you a good idea of how the language works, and how to program in Perl.
Running a Perl program is relatively simple. Once you have a program written out, all you have to do is to invoke the Perl interpreter, along with the filename.
perl filename.pl
If you are running on a unix system, you can make the file executable using the chmod +rx function call. In order to do this, you must have the following line as the first line in your Perl program.
#! /usr/local/bin/perl
Where "/usr/local/bin/perl" is the path of your Perl interpreter. In order to find this path on UNIX just type:
which perl
on the command line.
Let's practice writing a small program at our console. Using any editor type the following code:
#! /usr/local/bin/perl
print "Hello World!\\n\";
Now save your program with a .pl extension. Let's call it hello.pl. From the command line then type:
perl hello.pl
You should then see the following output:
Hello World!
You have just written your first Perl program. Congratulations!
Literals
Perl literals are values that are represented as-is. The value is considered to be hard-coded into the program. Perl supports literals of the following two types:
Numeric literals represent numbers. Strings literals are used to identify names, text, or messages that are displayed in the program. String literals are usually enclosed in single quotes, which means that any variable names (more on variables below) appearing in them are not interpolated. Double quoted string literals are also supported, and they support what is called variable interpolation, meaning that variable names are substituted for their real values. Single backquoted string literals are also supported. These normally allow you to run command line options and return the output to your program.
Perl also supports comments in your code. A perl comment is signified by a # sign. When you see a # sign, it means that everything after that is a comment up to the end of the line.
Variables
All programming languages support variables. Variables are like string literals, except that they hold a specific value or values. Perl supports three sets of variables:
Scalars The scalar variables hold only one value, which may be
a numeric literal, or a string literal. Scalar variable names always begin with
a $ sign.
Arrays An array variable can hold a list, or an array of values. The values can be numbers or strings. A key or subscript value is automatically assigned, and the values are kept in the order entered. Array variable names start with an @ sign.
Associative Arrays Associative arrays or hashes as they're also known, are similar to arrays. But the major difference is that you, the programmer, assign the key and value to the hash, while an array automatically assigns a key. Hashes do not keep the entered items in the same order as they were entered. Hash variables always start with the % sign.
As mentioned above, scalar values are used to track a single pieces of information. Perl's most commonly used variable is $_. This special variable name is called the default variable for Perl's many functions, so become very familiar with it.
We can see a value being assigned to a scalar value in the following example along with interpolation.
$var="my Scalar Variable";
print "This is $var";
The output of the above code would be: This is my Scalar Variable
Arrays hold a list of variables and are handled in a variety of different ways. You can give the values to the array during initialization:
@array=('one','two','three');
The above array assigns three values into the array. The values are separated by commas. The first subscript of an array is always 0, unless you change it with special variable $[. This is generally not good practice, because you can confuse people reading your code. In order to read the value, you would refer to the array name and the subscript that you want. For example, if we wanted the value 'two' we would say:
print $array[1];
We say $array because we are referring to only one value, followed by an open bracket, the subscript that we want, and then the close bracket. This would return the second value in your array.
Hashes can be initialized in a similar manner to arrays.
%hash=('key1','value1','key2','value2','key3','value3');
The major difference is that you have to place the values in a key-value order. So the first value you type would be the first key; the second, the associated value; the third, the second key; and the fourth, the value associated with that key. The process of retrieving the value from a hash, is similar to an array, except that instead of using [ ] (square brackets) we would use { } (braces), and instead of the subscript number, we would use the key. If we wanted to retrieve 'value2', we would use the following:
print $hash{'key2'};
Be aware that Perl variables are case-sensitive, so $me, $Me, $mE and
$ME are four separate variables. Similarly, if you had a scalar variable called
$me, an array called @me, and a hash called %me, they would be interpreted as
three separate variables, not the same one.