Perl Variables
Variables are data stored in memory. Creating a variable opens up a space in memory.
The interpreter decides the storage space in memory based on the type of the variable, so you can assign different data types to variables, such as integer, floating-point, string, etc.
In the previous chapter, we introduced Perl's three basic data types: scalars, arrays, and hashes.
- Scalars start with $, for example, $a and $b are two scalars.Arrays start with @, for example, @a and @b are two arrays.Hashes start with %, for example, %a and %b are two hashes.
Perl sets up an independent namespace for each variable type, so variables of different types can use the same name without worrying about conflicts. For example, $foo and @foo are two different variables.
Creating Variables
Variables do not need to explicitly declare a type. After a value is assigned to a variable, the interpreter automatically allocates a matching type space.
Variables are assigned values using the equal sign (=).
We can use the following in a program:use strictstatement to require all variables to be explicitly declared.
The left side of the equal sign is the variable, and the right side is the value. Example:
$age = 25; # 整型 $name = "example"; # 字符串 $salary = 1445.50; # 浮点数
In the above code, 25, "example", and 1445.50 are assigned to$age, $nameand$salaryvariables.
Next we will see the use of arrays and hashes.
Scalar Variables
A scalar is a single data unit. The data can be integers, floating-point numbers, characters, strings, paragraphs, etc. Simply put, it can be anything. The following is a simple application of scalars:
Example
The output of the above program is:
Age = 25 Name = example Salary = 1445.5
Array Variables
An array is a variable used to store an ordered list of scalar values.
Arrays start with @.
To access array variables, you can use the dollar sign ($) plus the variable name, and specify an index to access. Example:
Example
The output of the above program is:
$ages[0] = 25 $ages[1] = 30 $ages[2] = 40 $names[0] = google $names[1] = example $names[2] = taobao
In the program, we used the escape character (\) before the $ mark so we could output the $ character.
Hash Variables
A hash is akey/valuecollection of pairs.
Hashes start with %.
To access hash values, you can use the$ + {key}format to access:
Example
The output of the above program is:
$data{'google'} = 45
$data{'example'} = 30
$data{'taobao'} = 40
Variable Context
The so-called context: refers to the location of the expression.
The context is determined by the type of the variable on the left side of the equal sign. If the left side is a scalar, it is a scalar context; if the left side is a list, it is a list context.
The Perl interpreter determines the type of the variable based on the context. Examples:
Example
The output of the above program is:
名字为 : google example taobao 名字数为 : 3
In the code, @names is an array, and it is used in two different contexts. The first copies it to another array, so it outputs all elements of the array. In the second, we assign the array to a scalar, which returns the number of elements in the array.
The following lists a variety of different contexts:
| No. | Context and Description |
|---|---|
| 1 | Scalar −
Assignment to a scalar variable, evaluated on the right side in scalar context. |
| 2 | List −
Assignment to an array or hash, evaluated on the right side in list context. |
| 3 | Boolean −
Boolean context is a simple expression evaluation, checking whether it is true or false. |
| 4 | Void − This context does not care about what value is returned, and generally no return value is needed. |
| 5 | Interpolation −
This context only occurs within quotes. |