SoFunction
Updated on 2025-04-07

Perl syntax parsing Perl variable usage

This article will focus on the concept of Perl variables in Perl syntax. Perl variables are divided into three categories: scalar variables, array variables, and related relationship array variables. Perl variables are sensitive to detailed writing, such as: An, AN, and an are three divergent variables, but variables in divergent paradigms can use the same name.

Introduction to Perl Syntax

variable

1. Perl variable classification in syntax

Perl variables are divided into three categories: scalar variables, array variables, and related array variables. Perl variables are sensitive to nuances, such as: An, AN, and an are three divergent variables, but variables with divergent paradigms can use a unified name, such as: VAR can be a scalar variable, and there can also be an array variable that is also VAR. This is because Perl accepts the independent name space for each paradigm variable. Other Perl variables can store data from any paradigm without declaring variables like C. Data paradigms will be actively converted. At the same time, Perl variables are also divided into global and process variables, and the default is global variables.

1. Variables

1.2.1 Scalar variable (scalar variable) in Perl syntax

Scalar variables can only store one value. The scalar variable names in Perl always start with the character $. The following Perl statement assigns the value 9 to the scalar variable $nine. Assigns "BATI" to the scalar variable $name. Then prints it out using the PRINT statement.

Copy the codeThe code is as follows:

$nine=9; 
$name='BATI'; 
print($name,'is',$nine);


Save the above statement as a file, and then run it in DOS (WIN9X's MS-DOS system is also OK):
C:\Perl5>(Enter)
BATIis9 (What? Very familiar? Yes, Perl is so similar to our commonly used C)

1.2.2 Array variables in Perl syntax

An array is a table that can store multiple variables at once. Its assignment method is as follows:

@weekdays=('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

print(@weekdays);#The output is: SunMonTueWedThrFriSat
print($weekdays[1]);# output is:Sun
@work=@weekdays[1..5];#At this time, the value of the array work is ('Mon','Tue",...,'Fri');
@none=();#Hint to empty array

The array variable name begins with @, and the subscript value of the array is in [], and the subscript value starts from 0. This is still very similar to C.
Similarly, in Perl, if only the group name is rare and the subscript is missing, the entire output will be output like C, such as:
The first output statement.

But please pay attention to when outputting Sun's sentence, when we refer to a certain value in the array, we no longer use @, but use $ as the beginning of the variable. Since as far as a single value is concerned, it is a scalar variable, so (this is different from C). Of course, you still have to give the subscript value.
In the statement to assign values ​​to the array work, we use SLICE to initialize the array. You don’t have to consider what SLICE is. You just need to remember this situation. In fact, SLICE is a part of the table and an official hint.
There are many kinds of assignments to arrays. What we saw before were all assigned values ​​to arrays. Similarly, you can also use variables to approximate the value of another array to assign values ​​to arrays, such as:

@name=($firstname,@lastname);
@say=('Hesaid',@saysomething);

Here is an example of SLICE:

Copy the codeThe code is as follows:

@weekend=@weekdays[0,6];#The value of the array weekend is ('Sun','Sat')
print(@weekdays[1..5,0,6]);#The output is 'MonTueWedThuFriSunSat'

Perl also supports a special mechanism character $#var, which is used to return the index value at the end of the array. For example, the following statement manipulates $[Mechanism determines the first index value of the destiny group, uses $#var to determine the index value at the end of the destiny group, and then expresses the entire array:

for($i=$[;$i<=$#buffer;$i++){print$buffer[$i];}
The output of the above statement is the same as print@buffer;.

1.2.3 Scalar and array variables in Perl syntax

The table organ character (,) is very similar to the order value operator (,). Therefore, which operator Perl misappropriates depends on the detailed environment when the order file is running, that is, the file is using the array or the scalar value. Perl misappropriates the table layout character in the array expression and the order value operator in the order value. Please consider the following two expressions:

@an_array=(1,2,3,4,5);
$a_scalar=(1,2,3,4,5);

The first statement initializes an array, the second statement sets the value of the $a_scalar variable to 5, and cancels the assignment of the first 4 elements
Value effect.
Let’s take a look at another example:

Copy the codeThe code is as follows:

print$assoc{1,2}; 
@print$assoc{1,2};

The first sentence prints an element value of a two-dimensional relationship array, while the second sentence prints two element values ​​of a one-dimensional array.

1.2.4 Contact relational array variables in Perl syntax

Relational array variables are very similar to array variables, and they can store tables of scalar variables. The difference is that array variables must refer to array elements through integer subscripts, while related array variables can reach the target of waiting for array elements through any value as subscripts. We call the subscript of related arrays the key value (Key), which is an index value. Let's understand it through an example below:

Copy the codeThe code is as follows:

$ages{'Bob'}=35; 
$ages{'Mary'}=25; 
$,=''; 
print@ages{'Bob','Mary'}; 
printkeys(%ages); 
for$name(keys(%ages)) 

print"$nameis$ages{$keys}\n"; 
}


The French method assigns a value to the '$,' variable, so that the output of the future print statement will be affected. Regarding the special variable '$,' we will introduce in the future. When Perl misappropriates the contact relationship array variable, it uses curly braces {} to enclose the key value.

@ages{'Bob','Mary'} gives key values ​​in curly braces, implying that an element is referenced. There are two key values ​​in this statement, implying that a part of the array is referenced. The results should be the results of (35,25) and ($ages{'Bob'},$ages{'Mary'}) statements.

printkeys(%ages) uses the keys operator. The result will return all key values ​​of the contact relationship array, forming a table. %ages imply that the entire contact relationship array is referenced.

Please pay attention to the print statement in the chamomile statement. Here we see the usage of inserting variables in "" (double quotes). This is very effective. At the time of outputting the result, the variable will be replaced with the value of the variable as the ultimate result of the output. This is the so-called interpolation method. However, Perl does not allow inserting variables in "' (single quotes)!!!