1. Perl
  2. Variable

Perl variable

A description of Perl variable. Explains variable declarations and variable types.

Variable declaration

Describes Perl variable declarations.

Declaration of "a lexical variable" by "my"

You can use "my" to declare a "a lexical variable".

# Declaration of a scalar variable
my $num;

# Declaration of a array variable
my @nums;

# Hash variable declaration
my %score;

For a detailed explanation of "declaration of a lexical variable" using "my", see the following article.

Declaration of "a package variable" by "our"

You can use "our" to declare "a package variable".

Use our to declare a package variable.

# Declaration of a scalar variable
our $NUM;

# Declaration of a array variable
our @NUMS;

# Hash variable declaration
our%SCORE;

See the following article for "declaring a package variable" using "our".

Saving and restoring values with "local"

You can use "local" to temporarily save a package variable and restore it at the end of scope.

our $NUM = 5;
{
  local $NUM = 3;
}
# Here, "$NUM" returns to "5" which is the "original value"

For "save and restore values" by "local", see the following article.

Perl variable type

Perl has three "variable types": "a scalar variable", "a array variable", and "a hash variable". Please note that it does not indicate the type of a number like "int type" and "double type" like C language and Java.

"Scalar variable" - stores one value

A "a scalar variable" can contain only one value within a variable. A scalar variable is represented by prefixing the variable name with "$".

Typical scalar value

Typical scalar values are as follows.

    • String
    • Numerical value
    • reference
    • Undefined value (undef)

Example of assignment of scalar value to a scalar variable

my $scalar;
$scalar = "one"; # string
$scalar = 2; # number
$scalar = [3, 4, 5]; # Reference to array
$scalar = {x => 6}; # Reference to hash
$scalar = sub {return 7}; # Reference to subroutine
$scalar = undef; # undefined value

"Array variable" - stores multiple values

An "a array variable" can contain multiple values. Only a scalar variable can be included in the "elements" of an a array variable.

An a array variable is represented by prefixing the variable with "@". Only "list ()" can be assigned to an a array variable.

# Array variable can have scalars as elements
my @elements = (
  "one",
  2,
  [3, 4, 5],
  {x => 6, y => 7},
  sub {return 8},
  undef
)

Array nesting results in array concatenation

In Perl, array nesting is an array concatenation.

my @list_in_list = ((1, 2), (3, 4));
# It will be the same as this.
my @list = (1, 2, 3, 4);

"Hash variable" - stores key/value pairs

The hash has the following characteristics.

Includes multiple values that are a set of "key and value". Only a scalar variable can be included in the "elements" of a hash variable.

A hash variable is represented by prefixing the variable with "%". Only "list ()" can be assigned to a hash variable.

my %scores = (math => 70, engligh => 80);

Array-type to hash-type conversion

Both a array variable and a hash variable have a list as their content, so they can be converted to each other.

@array = %hash;
%hash = @array;

Variable and scope

Another thing to know about variable is their scope and lifetime . Scope is a term that describes where a variable is visible. Lifespan is the length of time a variable stores a value.

For example, for a lexical variable declared with my, the scope is from "{" to "}".

{
  # Declare here
  my $num = 3;
}
# I can't see from here

The value saved in "$num" disappears from the memory when the scope by "{...}" ends, and the variable itself called $num disappears.

See the following articles for more information on variable scope and lifetime.

Related Informatrion