Variables and Data Types

Learn how you can use variables to store data and understand the differences among various data types available in Perl.

Variables

A variable in any programming language is a named piece of computer memory to hold some program data. Variables are an essential part of a computer program.

You can declare a variable in Perl using a $ sign followed by its name, e.g., $myVar.

Consider the following Perl code where we store data in variables and display them on the screen.

Press + to interact
$string = "This is a string."; # stores string
$int = 5; # stores an integer
$float = 5.7; # stores a floating point type value
$char = 'a'; # stores character type value
print $string, "\n";
print "An integer type: ", $int, "\n";
print "A float type: ", $float, "\n";
print "A character type: ", $char, "\n";

Escape characters

We use escape characters for a particular purpose in Perl to achieve a specific functionality, e.g., in the above code snippet, for instance, \n does not represent the character ‘n’, but a new line or line break. We can use many other escape characters using the \ operator in our code.Similarly, there are many escape characters which are given below:

Press + to interact
print "Hello World!\n"; # \n inserts a new line
print "Hello\tWorld!\n"; # \t adds a tab space
print "\"Double quotes\"\n"; # use \ to add double quotes
print '\'Single quotes\''; # use \ to add single quotes

Data types

There are different data types available in Perl for different purposes. Some of them have been presented below:

  • Boolean
  • Integer
  • Float
  • Array
  • String

Unlike other languages such as C++ or Java, Perl does not require defining variables along with a specific data type. The type of a variable is picked based on the value assigned to it.

Here is a brief overview of the types:

Boolean

Boolean data type is used to store true or false values. The numeric value 0 is used to represent false, whereas any other numeric value represents true. Let’s look at the code below:

Press + to interact
$false = 0; # reutrns false
$true = 1; # any values greater or less than 0 returns true

Integer

An integer is a positive or negative whole number. Perl allows you to assign integer constants in decimal, hexadecimal, octal or binary numbering systems. Consider the following code:

Press + to interact
$negative = -3; # negative
$zero = 0; # zero (can also be false, if used as a Boolean
$positive = 123; # positive decimal
$zeroPos = 0123; #0 prefix is used to sepcify octal - octal value = 83 decimal
$hex = 0xAB; #0x prefix is used to specify hexadecimal - hexadecimal value = 171 decimal
$bin = 0b1010; # 0b prefix is used to specify binary - binary value = 10 decimal
print $negative," " ,$zero," " , $positive," " , $zeroPos," " , $hex," " , $bin;

Here the variable values are interspersed with the constant text in the print statement.

Float

Floating point numbers, doubles or simply called floats are decimal numbers.

Press + to interact
$float1 = 1.23;
$float2 = 10.0000001;
print $float1," ",$float2;

Array

An array is like a list of values (similar or of different data types). The simplest form of an array is indexed by an integer, and ordered by the index, with the first element lying at index 0. We use @ to initiate an array with values enclosed in () pair of parenthesis. Look at the following code:

Press + to interact
@intarray = (1, 2, 3); # An array of integers
print "@intarray \n";
@floatarray = (1.123, 2.356, 19.76); # An array of floats
print "@floatarray \n";
@chararray = ('a', 'b','c'); # An array of characters
print "@chararray \n";
@mixed = (1, 2, 3, 'a', 'b', 'c'); #contains both characters and numbers
print "@mixed";

We will explore arrays in more detail in the chapter, Arrays.

String

A string is an array of characters. We can declare a string using either single quotes (') or double quotes (").

Press + to interact
$string1 = "A quick brown fox jumps over the lazy dog";
print $string1;

We will explore strings in more detail in the chapter, Strings.

This whole process of declaring variables and storing data in these variables is summed up in the following slide: