Perl variables
Variables are data stored in memory. Creating a variable will open up a space in the 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.
Scalar $ starts, such as $a $b are two scalars.
Array @ starts, such as @a @b are two arrays.
- ##Hash % starts, %a %b are two hashes.
Creating variablesVariables do not need to explicitly declare the type. After the variable is assigned a value, the interpreter will automatically allocate a matching type space. Variables use the equal sign (=) to assign values.
We can use theThe left side of the equal sign is the variable, and the right side is the value. The example is as follows:use strict statement in the program to force all variables to declare their types.
$age = 25; # 整型 $name = "php"; # 字符串 $salary = 1445.50; # 浮点数In the above code, 25, "php" and 1445.50 are assigned to
$age respectively. , $name and $salary variables.
Next we will see the use of arrays and hashes.Scalar variableA scalar is a single unit of data. 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:
#!/usr/bin/perl $age = 25; # 整型 $name = "php"; # 字符串 $salary = 1445.50; # 浮点数 print "Age = $age\n"; print "Name = $name\n"; print "Salary = $salary\n";The execution output of the above program is:
Age = 25 Name = php Salary = 1445.5
Array variableThe array is used to store an ordered scalar value variable. Array @ Start. To access the variables of the array, you can use the dollar sign ($) + variable name and specify the subscript to access. The example is as follows:
#!/usr/bin/perl @ages = (25, 30, 40); @names = ("google", "php", "taobao"); print "$ages[0] = $ages[0]\n"; print "$ages[1] = $ages[1]\n"; print "$ages[2] = $ages[2]\n"; print "$names[0] = $names[0]\n"; print "$names[1] = $names[1]\n"; print "$names[2] = $names[2]\n";The output result of the above program execution is:
$ages[0] = 25 $ages[1] = 30 $ages[2] = 40 $names[0] = google $names[1] = php $names[2] = taobaoIn the program, we use the escape character (\) before the $ mark so that the character $ can be output.
Hash variableA hash is a collection of
key/value pairs.
Hash % Start. If you want to access the hash value, you can use the$ + {key} format to access:
#!/usr/bin/perl %data = ('google', 45, 'php', 30, 'taobao', 40); print "$data{'google'} = $data{'google'}\n"; print "$data{'php'} = $data{'php'}\n"; print "$data{'taobao'} = $data{'taobao'}\n";The output result of the above program execution is:
$data{'google'} = 45 $data{'php'} = 30 $data{'taobao'} = 40
Variable contextThe so-called context: refers to the location of the expression. The context is determined by the variable type on the left side of the equal sign. If the left side of the equal sign is a scalar, it is a scalar context. If the left side of the equal sign is a list, it is a list context. The Perl interpreter determines the type of the variable based on context. The example is as follows:
#!/usr/bin/perl @names = ('google', 'php', 'taobao'); @copy = @names; # 复制数组 $size = @names; # 数组赋值给标量,返回数组元素个数 print "名字为 : @copy\n"; print "名字数为 : $size\n";The execution output of the above program is:
名字为 : google php taobao 名字数为 : 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 variety 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, calculated on the right side of the list context. |
3 | Boolean − The Boolean context is a simple expression evaluation to see if it is true or false. |
4 | Void − This kind of context does not need to know what value is returned, and generally does not require a return value. |
5 | Interpolation − This context occurs only within quotation marks. |