A description of the variable
A variable is a data item whose value can change as the program runs.
A variable can be anywhere in the program, and you do not need to declare it as a type of feature, as in a high-level language.
The value of a variable can be a string, a number, or a combination of them.
Second, the type of Perl variable
A. Scalar type
$ variable = value;
B. Array type
@ array = (' value 1 ', ' Value 2 ', ' Value 3 ');
C. Associative array (often called hash "hash")
%hash= (' scalar 1 ' => ' value 1 ',
' Scalar 2 ' => ' value 2 ',);
Third, the scope of the variable
The scope of the variable is global by default. For local variables, you need to use the relevant local variables function our,my,local set
Iv. how the variable is assigned
The assignment operator is the equals sign (=) and often assigns the value on the right to the variable on the left. Any assigned value represents a named storage space called the Left value (lvalue)
The statement ends with a semicolon (;).
Example
#! / usr / bin / perl
$ day = 12; #variable
@months = ('Mar', 'Apr', 'may'); #Array
% states = (#hash
'CA' => 'California',
'ME' => 'Maine',
'MT' => 'Montana',
'NM' => 'New Mexico',
);
print "$ dayn"; #n newline
print "@monthsn";
print "$ months [0], $ months [1], $ months [2] n]; #Print the values in the array
Print "$ states {'CA'}, $ states {'NM'} n"; #Print the values in the hash
Print $ x + 3, "n"; #Unassigned is empty
Print "* * * * $ name *** n"; # is empty for assignment
The results of the operation are as follows