Perl Variables

Perl Variables with the techniques of handling them are an important part of the Perl language. As a language-type script, Perl was designed to handle huge amounts of data text.

Working with variables is fairly straightforward given that it is not necessary to define and allocate them, so no sophisticated techniques for the release of memory occupied by them.

As general information, to note that the names of Perl variables contain alphabetic characters, numbers and the underscore (_) character and are case sensitive.

A specific language feature is that variables have a non-alphabetical prefix that fashion somewhat cryptic the language.

This, however, presents the advantage that provides immediate information on the type of variable and what can be done with it. Thus, according to the first name that begins with a variable, we have:

  • scalar variables – starting with $
  • array variables – starting with @
  • hashes or associative arrays indicated by %

The $, @ and % characters actually predefine the variable type in Perl. Perl language also offers some built-in predefined variables that facilitate and shorten the programming code.

Below are the most important characteristics of the three types of Perl variables and some examples of their use.

Scalar Perl variables.

Are simple variables that can contain a single element: a string, a number or a reference to an object. Strings are sequences of characters including any symbol, letter or number. The numbers may contain exponents, integers or decimal values. The reference is a scalar value that contains a memory address where a scalar, array or hash variable is stored. The reference type variable is preceded by the character.

Below you can see some examples of code on how to use the scalar variable type.

$pi = 3.14;
$height = "20 cm";
$message = "Hello World!\n";
print $message;
$message = "Welcome!\n";
print $message;
print $$ref_a;

Array Perl variables.

Arrays are ordered list of scalars, where the first element of the list begins with the index 0. An array can hold un unlimited number of elements and its name begins with the character @. An element of the array is written with the $ prefix followed by the array name and its index placed in square brackets.

Below you can see an example of an array variable:

@timeUnits = ("hours", "minutes", "seconds","miliseconds");

Taking the example above, by @timeUnits we mean the entire array and by $timeUnits[0] we mean the first element of the array @timeUnits, in our case "hours". In order to print all the elements of the @timeUnits array, we could use a code snippet like this:

foreach $i (@timeUnits) 
  print "$i\n";

where $i indicates the current loop iterator.

Hash Perl variables

Hashes or associative arrays consists of a group of pairs of elements – a key and a data value. Meanwhile the arrays are indexed by numbers, hashes are indexed by strings. Perl hash names are prefixed by % character. Let’s look below for a hash variable example:

%animalColors = ("bear",    "brown", "mouse", "gray", 
                 "panther", "black", "panda", "white");

If you want to refer the first element of the %animalColors hash, you must use the $ scalar symbol and curly brackets as in $animalColors{"bear"}.

If you want to print the %animalColors hash, you could use a little code like this:

%animalColors = ("bear",    "brown", "mouse", "gray", 
                 "panther", "black", "panda", "white");
foreach $item (keys %animalColors) {
    print "The $item is $animalColors{$item}.\n";   

And if you will run it you’ll get as result something like this:

The bear is brown.
The mouse is gray.
The panda is white.
The panther is black.

Please note that keys %animalColors will return the keys in random order, so if you want to print the hash in the exact order, you must specify the keys and rewrite the foreach loop like in the example below:

foreach $item ("bear", "mouse", "panther", "panda") {
    print "The $item is $animalColors{$item}.\n";   

And you’ll get as result:

The bear is brown.
The mouse is gray.
The panther is black.
The panda is white.

But enough about hashes for now. Let’s speak a bit about:

Predefined Perl variables

The Perl language has a lot of special variables you must know. You can modify the values of these variables, except the variables that are read only. Of course, the predefined variables are of scalar, array or hash type. One of the most important is the scalar variable $_ which is used by default by many operators and functions as in the follows example:

$_= "Hello World!\n";

If you will run the code, this will print the text "Hello World!".

If you want more information about these predefined variables, you can search the link page:

Predefined Perl variables

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order

return to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!