Perl 简明教程
Perl - Variables
变量是存储值的保留内存位置。这意味着当你创建一个变量时,你就预留了一些内存空间。
Variables are the reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
基于变量的数据类型,解释器分配内存并决定存储在保留内存中的内容。因此,通过将不同的数据类型分配给变量,你可以在这些变量中存储整数、小数或字符串。
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or strings in these variables.
我们已经了解到 Perl 具有以下三种基本数据类型 −
We have learnt that Perl has the following three basic data types −
-
Scalars
-
Arrays
-
Hashes
相应地,我们将在 Perl 中使用三种类型的变量。 scalar 变量将以美元符号 ($) 为前缀,它可以存储数字、字符串或引用。 array 变量将以符号 @ 为前缀,它将存储标量的有序列表。最后, Hash 变量将以符号 % 为前缀,并且将用于存储成对键/值集。
Accordingly, we are going to use three types of variables in Perl. A scalar variable will precede by a dollar sign ($) and it can store either a number, a string, or a reference. An array variable will precede by sign @ and it will store ordered lists of scalars. Finaly, the Hash variable will precede by sign % and will be used to store sets of key/value pairs.
Perl 以单独的命名空间维护每种变量类型。因此,你可以使用相同的名称将一个标量变量、一个数组或一个哈希用于变量,而不必担心冲突。这意味着 $foo 和 @foo 是两个不同的变量。
Perl maintains every variable type in a separate namespace. So you can, without fear of conflict, use the same name for a scalar variable, an array, or a hash. This means that $foo and @foo are two different variables.
Creating Variables
Perl 变量不必显式声明来保留内存空间。当你为变量指定一个值时,声明就会自动发生。等号 (=) 用于将值赋值给变量。
Perl variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
请注意,如果我们在程序中使用 use strict 语句,在使用变量之前必须声明该变量。
Keep a note that this is mandatory to declare a variable before we use it if we use use strict statement in our program.
运算符左边操作数是变量名,= 运算符右边操作数是存储在变量中的值。例如 −
The operand to the left of the = operator is the name of the variable, and the operand to the right of the = operator is the value stored in the variable. For example −
$age = 25; # An integer assignment
$name = "John Paul"; # A string
$salary = 1445.50; # A floating point
这里 25、“John Paul”和 1445.50 分别分配给 $age、$name 和 $salary 变量的值。我们很快将看到如何将值分配给数组和哈希。
Here 25, "John Paul" and 1445.50 are the values assigned to $age, $name and $salary variables, respectively. Shortly we will see how we can assign values to arrays and hashes.
Scalar Variables
标量是单个数据单元。该数据可以是整数、浮点数、字符、字符串、段落或整个网页。简单来说它可以是任何东西,但只是一种。
A scalar is a single unit of data. That data might be an integer number, floating point, a character, a string, a paragraph, or an entire web page. Simply saying it could be anything, but only a single thing.
下面是使用标量变量的一个简单示例−
Here is a simple example of using scalar variables −
#!/usr/bin/perl
$age = 25; # An integer assignment
$name = "John Paul"; # A string
$salary = 1445.50; # A floating point
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
这会产生以下结果 −
This will produce the following result −
Age = 25
Name = John Paul
Salary = 1445.5
Array Variables
数组是存储标量值的已排序列表的变量。数组变量前面有“at”(@) 符号。若要引用数组的单个元素,您需在变量名称后加上美元符号 ($),再跟上方括号中的元素索引。
An array is a variable that stores an ordered list of scalar values. Array variables are preceded by an "at" (@) sign. To refer to a single element of an array, you will use the dollar sign ($) with the variable name followed by the index of the element in square brackets.
下面是使用数组变量的一个简单示例:
Here is a simple example of using array variables −
#!/usr/bin/perl
@ages = (25, 30, 40);
@names = ("John Paul", "Lisa", "Kumar");
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";
在这里,我们在 $ 符号之前使用转义符号 (\) 只是为了打印它。其他 Perl 会将其理解为变量,并会打印其值。执行后,将生成以下结果:
Here we used escape sign (\) before the $ sign just to print it. Other Perl will understand it as a variable and will print its value. When executed, this will produce the following result −
$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = John Paul
$names[1] = Lisa
$names[2] = Kumar
Hash Variables
哈希是一组 key/value 对。哈希变量前面有百分号 (%) 符号。若要引用哈希的单个元素,您需在变量名称后加上用花括号括起来的与该值相关联的“键”。
A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name followed by the "key" associated with the value in curly brackets.
下面是使用哈希变量的一个简单示例:
Here is a simple example of using hash variables −
#!/usr/bin/perl
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
print "\$data{'John Paul'} = $data{'John Paul'}\n";
print "\$data{'Lisa'} = $data{'Lisa'}\n";
print "\$data{'Kumar'} = $data{'Kumar'}\n";
这会产生以下结果 −
This will produce the following result −
$data{'John Paul'} = 45
$data{'Lisa'} = 30
$data{'Kumar'} = 40
Variable Context
在 Perl 中,变量的处理方式因上下文而异,即变量的使用场景。我们来看一下以下示例 −
Perl treats same variable differently based on Context, i.e., situation where a variable is being used. Let’s check the following example −
#!/usr/bin/perl
@names = ('John Paul', 'Lisa', 'Kumar');
@copy = @names;
$size = @names;
print "Given names are : @copy\n";
print "Number of names are : $size\n";
这会产生以下结果 −
This will produce the following result −
Given names are : John Paul Lisa Kumar
Number of names are : 3
在此示例中,@name 是一个数组,它已在两个不同的上下文中使用。首先,我们把它复制到另一个数组中,即列表,因此它返回了所有元素,假设上下文是列表上下文。接下来,我们使用了相同的数组并尝试将此数组存储为标量,因此在这种情况下,它只返回了此数组中的元素数量,假设上下文是标量上下文。下表列出了不同的上下文 −
Here @names is an array, which has been used in two different contexts. First we copied it into anyother array, i.e., list, so it returned all the elements assuming that context is list context. Next we used the same array and tried to store this array in a scalar, so in this case it returned just the number of elements in this array assuming that context is scalar context. Following table lists down the various contexts −
Sr.No. |
Context & Description |
1 |
Scalar Assignment to a scalar variable evaluates the right-hand side in a scalar context. |
2 |
List Assignment to an array or a hash evaluates the right-hand side in a list context. |
3 |
Boolean Boolean context is simply any place where an expression is being evaluated to see whether it’s true or false. |
4 |
Void This context not only doesn’t care what the return value is, it doesn’t even want a return value. |
5 |
Interpolative This context only happens inside quotes, or things that work like quotes. |