Perl Variables
A variable is data stored in memory; creating a variable allocates a space in memory.
The interpreter determines the storage space in memory based on the variable's type, so you can assign different data types to a variable, such as integer, float, string, etc.
In the previous chapter, we introduced Perl's three basic data types: scalar, array, and hash.
Perl sets up a separate namespace for each variable type, so variables of different types can use the same name without you worrying about conflicts. For example, $foo and @foo are two different variables.
Create variables
Variables do not need an explicitly declared type; after a value is assigned to a variable, the interpreter automatically allocates the matching type space.
Variables are assigned values using the equals sign (=).
We can use the use strict statement in the program to require all variables to be explicitly declared.
The left side of the equals sign is the variable and the right side is the value, as shown in the following example:
$age = 25; # 整型
$name = "runoob"; # 字符串
$salary = 1445.50; # 浮点数
In the above code, 25, "runoob", and 1445.50 are assigned respectively to $age, $name and $salary variable.
Next, we will see the use of arrays and hashes.
Scalar Variables
A scalar is a single unit of data. The data can be an integer, a float, a character, a string, a paragraph, etc. Simply put, it can be anything. The following is a simple application of scalars:
Example
#!/usr/bin/perl
$age
=
25
;
Integer
$name
=
"
runoob
"
;
String
$salary
=
1445.50
;
Float (floating point number)
"
Age =
$age
\n
"
;
"
Name =
$name
\n
"
;
"
Salary =
$salary
\n
"
;
The output of the above program is:
Age = 25
Name = runoob
Salary = 1445.5
Array Variables
An array is a variable used to store an ordered list of scalar values.
An array starts with @.
To access an array variable, you can use the dollar sign ($) + variable name and specify an index to access it, as shown in the following example:
Example
#!/usr/bin/perl
@ages
=
(
25
,
30
,
40
)
;
@names
=
(
"
"
,
"
runoob
"
,
"
taobao
"
)
;
"
$
ages[0] =
$ages
[0]
\n
"
;
"
$
ages[1] =
$ages
[1]
\n
"
;
"
$
ages[2] =
$ages
[2]
\n
"
;
"
$
names[0] =
$names
[0]
\n
"
;
"
$
names[1] =
$names
[1]
\n
"
;
"
$
names[2] =
$names
[2]
\n
"
;
The output of the above program is:
$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = google
$names[1] = runoob
$names[2] = taobao
In the program, we used the escape character () before the $ sign so that the character $ can be output.
Hash Variables
A hash is a key/value collection of pairs.
A hash starts with %.
To access a hash value, you can use the $ + {key} format to access it:
Example
#!/usr/bin/perl
%data
=
(
'
'
,
45
,
'
runoob
'
,
30
,
'
taobao
'
,
40
)
;
"
$
data{'google'} =
$data
{'google'}
\n
"
;
"
$
data{'runoob'} =
$data
{'runoob'}
\n
"
;
"
$
data{'taobao'} =
$data
{'taobao'}
\n
"
;
The output of the above program is:
$data{'google'} = 45
$data{'runoob'} = 30
$data{'taobao'} = 40
Variable Context
The so-called context refers to the position where an expression appears.
Context is determined by the type of the variable on the left side of the equals sign; if the left side is a scalar, it is scalar context; if the left side is a list, it is list context.
The Perl interpreter determines the type of a variable based on the context. Example as follows:
Example
#!/usr/bin/perl
@names
=
(
'
'
,
'
runoob
'
,
'
taobao
'
)
;
@copy
=
@names
;
Copying an array
$size
=
@names
;
Assigning an array to a scalar returns the number of elements in the array
"
Names are : @copy
\n
"
;
"
Number of names is :
$size
\n
"
;
The output of the above program is:
名字为 : google runoob taobao
名字数为 : 3
In the code, @names is an array that is used in two different contexts. The first copies it to another array, so it outputs all the elements of the array. The second assigns the array to a scalar, which returns the number of elements in the array.
The following lists several different contexts:
| No. | Contexts and Descriptions |
|---|---|
| 1 | Scalar −
Assigned to a scalar variable, evaluated on the right side in scalar context |
| 2 | List −
Assigned to an array or hash, evaluated on the right side in list context. |
| 3 | Boolean −
Boolean context is a simple evaluation of an expression to see whether it is true or false. |
| 4 | Void −
This context does not care about what value is returned; generally no return value is needed. |
| 5 | Interpolation −
This context only occurs inside quotes. |