Variables are data stored in memory. Creating a variable will open up a space in memory.
The interpreter will determine its 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 have introduced Perl's three basic data types: scalar, array, and hash.
Start with a scalar $, such as $a $b are two scalars.The array starts with @, such as @a @b are two arrays.
The hash % starts with %a %b being the two hashes.
Perl sets up an independent command space 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.
Variables do not need to explicitly declare their type. After the variable is assigned a value, the interpreter will automatically allocate a matching type space.
Variables are assigned values using the equal sign (=).
We can use the use strict statement in the program to force all variables to declare types.
The left side of the equal sign is the variable and the right side is the value. Examples are as follows:
$age = 25; # Integer $name = "codercto"; # String $salary = 1445.50; # Floating point number
In the above code, 25, "codercto" and 1445.50 are assigned to the $age , $name and $salary variables respectively.
Next we will see the use of arrays and hashes.
A scalar is a single unit of data. Data can be integers, floating point numbers, characters, strings, paragraphs, etc. Simply put it can be anything. Here is a simple application of scalars:
The output result of the execution of the above program is:
Age = 25Name = coderctoSalary = 1445.5
An array is a variable used to store an ordered scalar value.
Array @ starts.
To access the variables of the array, you can use the dollar sign ($) + the variable name and specify the subscript to access it. The example is as follows:
The output result of the execution of the above program is:
$ages[0] = 25$ages[1] = 30$ages[2] = 40$names[0] = google$names[1] = codercto$names[2] = taobao
In the program, we use the escape character () before the $ mark so that the character $ can be output.
A hash is a collection of key/value pairs.
Hash % starts.
If you want to access the hash value, you can use the $+{key} format to access it:
The output result of the execution of the above program is:
$data{'google'} = 45$data{'codercto'} = 30$data{'taobao'} = 40
The so-called context: refers to the location where the expression is located.
The context is determined by the type of the variable on the left side of the equal sign. If there is a scalar on the left side of the equal sign, it is a scalar context. If there is a list on the left side of the equal sign, it is a list context.
The Perl interpreter determines the variable type based on context. Examples are as follows:
The output result of the execution of the above program is:
The name is: google codercto taobao The number of names is: 3
@names in the code is an array, which is used in two different contexts. The first one copies it to another array, so it outputs all the elements of the array. Second we assign the array to a scalar, which returns the number of elements in the array.
A number of different contexts are listed below:
serial number | context and description |
---|---|
1 | scalar− Assigned to a scalar variable, evaluated on the right side of the scalar context |
2 | list− Assigns to an array or hash, evaluated on the right side of the list context. |
3 | Boolean− A Boolean context is a simple expression that evaluates to see whether it is true or false. |
4 | Void − This context does not need to relate to what value is returned, and generally does not require a return value. |
5 | interpolation− This context only occurs within quotes. |