Perl Data Types

There are three built-in Perl Data Types: scalarsarrays and hash or associative arrays, that make the Perl language a powerful tool for text manipulation.

scalar

The first type of Perl data types is the scalar which is Perl’s data fundamental unit and it can be a single string, a number or a reference to a specific object.

When we speak about scalars, we have in view two types:

  1. scalar literals (or constants) that don’t change over the life of a program – one simple example is the value of pi
  2. scalar variables which let you hold data and manipulate them; each variable is associated with a name that enables you to refer to the data and the address of a chunk of memory where the variable value is stored, the variable value can be changed during the execution of the program.

Scalar literals can be numbers or strings

  • Numbers can be integers or floating point decimal numbers expressed in different notations: 145.23, 22., 2.7E-2.
  • Strings are sequences of characters and can contain any kind of data including simple ASCII text and binary data. There is no real limit for the size of a string literal. String literals are usually included between single or double quotes: ‘12.5’, “Hello World!”

Scalar variables are used for storing scalar literals

We represent a scalar variable with the dollar sign $ followed by the name of the variable: $Company, $country, $count, $x.

The name of a variable can contain any alphabetic characters, numbers or underscore. Note that the first character of a scalar variable name can’t be a number and the variable names are case sensitive, which means there is a significant difference between upper and lowercase characters. In Perl language it’s not necessary to declare a scalar variable, you just name and use it like in this scalar variable assignment example:

$pi = 3.14;
In a scalar variable we can store the memory address of a chunk of memory, too. We call reference the scalar value that contains a memory address. Perl allocates and deallocates automatically the memory for references. Look at the following code snippet to see how you can use a reference variable:

$a=”Hello World!”;

$ref_a=\$a;

print $$ref_a;
In the first line of code, the variable named $a stores the string “Hello World”. In the second line, the scalar variable ref_a is assigned with a reference to the variable $a (note that $a is preceded by the character \). The third line of the snippet is an example of how to print the string stored in the variable $a using a reference – we say that we dereference the scalar variable $ref_a before print. It’s a good practice to name a scalar reference beginning with the substring ref_, because this will tell you that this is a reference variable and if you want to use it, you must dereference it before.

Another kind of constants are literal lists which are used to initialize an array or a hash. To create a literal list it is very easy, just put a set of parentheses enclosing scalar values as in the following example:

(1, ‘hello’, ‘world’, $a)
where we can see a list with 4 elements.

array

The second basic type of the Perl data types is the array which is indexed by a number. For creating an array you simple must put something into it. It is not necessary to declare or specify its dimension. The name of an array begins with the character @ and in the example below we will assign the array @things with the literal list presented above:

@things = (1, ‘hello’, ‘world’, $a);

hash

The third basic type of the Perl data types is the hash or associative array, which, like the array, contains a number of scalars. Hashes are indexed by strings and each hash has two parts: a key that identifies each element of the hash and a value which is the data associated with the key. The name of a hash structure begins with the character %. In order to assign a hash key, we’ll write it like in the following code line:

$hash{$key}=value;
Here we have a short example of a hash structure called %NotebooksPrice:

# We first assign some elements:

 

$NotebookPrices{“Toshiba”}=650;

$NotebookPrices{“HP”}=550;

$NotebookPrices{“Acer”}=750;

 

# we print now the keys of the hash %NotebookPrices

foreach $item (keys %NotebookPrices) {

  print “$item\n”;

}
You can combine the three Perl data types enumerated above to get more complex data structures (like array of arrays, array of hashes, and so on).

We tried above to enumerate some of the most important aspects of Perl data types, if you need more details, please check on CPAN or our link:

Perl Books

Leave a Reply

Your email address will not be published. Required fields are marked *