Summary: in order to manipulate data, you use variables. Perl provides three types of variables: scalars, lists and hashes to help you manipulate the corresponding data types including scalars, lists and hashes. We are going to focus on the scalar variable in this tutorial.
Naming variables
Scalar variables are used to manipulate scalar data such as numbers and strings. A scalar variable starts with a dollar sign ( $), followed by a letter or underscore, after that any combination of numbers, letters and underscores are allowed, up to 255 characters. It is noticed that Perl is case-sensitive so $variable and $Variable are totally different variables.
Perl uses $ as a prefix of scalar variable because the $ looks like S in Scalar.
The following are examples of valid variables:
$gate = 10; $_port = 20;
However the following variables are invalid:
$4whatever = 20; # no letter or underscore found after dollar sign ($) $email-address = “zen@gmail.com”; # special character (-) found $home url = “http://www.zentut.com”; # space is not allowed
Declaring variables
Perl does not require you to declare a variable before using it. For example, you can introduce a variable in your program and use it right away as follows:
$a = 10; $b = 20; $c = $a + $b; print($c);
In some cases, using a variable without declaring it explicitly could lead to problems. Let’s take a look at the following example:
$color = 'red'; print "Your favorite color is " . $colour ."\n";
The expected output was “Your favorite color is red”. However, in this case we got “Your favorite color is”, because the $color and $colour are different variables. The mistake was made because of misspelling.
In order to prevent such cases, Perl provides a pragma called strict which requires you to declare variable explicitly before using it. In this case, if you use my keyword to declare a variable and try to run the script, Perl will issue an error message to indicate a compilation error occurred due to $colour must be declared explicitly.
#!/usr/bin/perl use strict; my $color = 'red'; print "Your favorite color is " . $colour ."\n"
A variable that is declared with my keyword is lexically scoped variable. It means the variable is only visible inside enclosing block or all block inside its block, in other words, the variable is local to the enclosing block. Now, we go into a very important concept in programming called variable scope.
Variable scope
Please take a look at the following example:
#!/usr/bin/perl
use warnings;
$color = 'red';
print("my favorite #1 color is " . $color . "\n");
# another block
{
my $color = 'blue';
print("my favorite #2 color is " . $color . "\n");
}
# for checking
print("my favorite #1 color is " . $color . "\n");In the example above:
- First, we declared a global variable called
$color. When we say global we means global inside the program. - Then, we displayed the favorite color by referring to the
$colorvariable. As expected, we get red color in this case. - Next we created a new block and declared a variable with the same name
$colorusingmykeyword. The $color variable is lexical. It is a local variable and only visible inside the enclosing block. - After that inside the block, we displayed the favorite color and we got
blue. The local variable takes priority in this case. - Finally after the block, we referred to
$colorvariable and we were now referring to the global variable $color again.
If you want to declare global variables that are visible throughout your program or from external packages, you can use keyword our as follows:
our $color = 'red';
You will learn more about package in the Perl package tutorial.
Variable interpolation
Perl interpolates variables in double-quoted strings. It means if you put a variable inside a double-quoted string, you will get the value of the variable instead of its name. Let’s take a look at the following example:
#!/usr/bin/perl use strict; use warnings; my $amount = 20; my $s = "The amount is $amount\n"; print($s);
Perl interpolates the value of $amount into string which is 20.
It is noticed that Perl only interpolates scalar variables and arrays, but not for hashes. And the interpolation is only applied to double-quoted string but not single-quoted string.