Perl Variable

Summary:  in this tutorial, we will introduce you to Perl variable and also discuss about variable scope and variable interpolation.

Perl VariableTo manipulate data in your program, 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

You use scalar variables 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.

Perl is case-sensitive so $variable and $Variable are different variables. Perl uses dollar sign ( $) as a prefix for the scalar variables because the $ looks like the character S in scalar. Use this tip to remember when you want to declare a scalar variable.

The following example illustrates valid Perl variables:

However, the following variables are invalid in Perl.

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:

In some cases, using a variable without declaring it explicitly may lead to problems. Let’s take a look at the following example:

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.

To prevent such cases, Perl provides a pragma called strict that 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 the  $colour variable must be declared explicitly.

A variable declared with my keyword is a lexically scoped variable. It means the variable is only visible inside enclosing block or all blocks nested inside the enclosing 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.

Perl variable scope

Let’s take a look at the following example:

In the example above:

  • First, we declared a global variable named  $color.
  • Then, we displayed the favorite color by referring to the $color variable. As expected, we get the red color in this case.
  • Next, we created a new block and declared a variable with the same name $color using the my keyword. 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 the blue color. The local variable takes priority in this case.
  • Finally, following the block, we referred to the $color variable and Perl referred to the  $color global variable.

If you want to declare global variables that are visible throughout your program or from external packages, you can use our  keyword as shown in following code:

Perl 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:

Perl interpolates the value of $amount into string which is 20.

It is noticed that Perl only interpolates scalar variables and arrays, not hashes.  In addition, the interpolation is only applied to double-quoted string, but not single-quoted string.

In this tutorial, we have covered Perl variable including naming and declaring scalar variables. We also discussed about the variable scope and variable interpolation in Perl.