Variables in Perl are used to store and manipulate data throughout the program. When a variable is created it occupies memory space. The data type of a variable helps the interpreter to allocate memory and decide what to be stored in the reserved memory. Therefore, variables can store integers, decimals, or strings with the assignment of different data types to the variables.
在 Perl 中,我们使用变量来存储和操作程序中的数据。当创建一个变量时,它会在内存中占据一定的空间。变量的数据类型可以帮助解释器分配内存,并决定在保留的内存中存储什么内容。因此,通过为变量赋予不同的数据类型,我们可以利用变量来存储整数、小数或字符串。
Naming of a Variable
变量的命名
A variable in Perl can be named anything with the use of a specific datatype. There are some rules to follow while naming a variable:
在 Perl 中,我们可以根据特定的数据类型给变量赋予任何名称。不过在命名变量时,我们需要遵循一些规则:
- Variables in Perl are case-sensitive.
- Perl 中的变量是区分大小写的。
Example:
示例:
$John and $john are two different variables
- It starts with $, @ or % as per the datatype required, followed by zero or more letters, underscores, and digits
- 它必须以 $、@ 或 % 开头(具体取决于所需的数据类型),后面跟零个或多个字母、下划线和数字。
- Variables in Perl cannot contain white spaces or any other special character except underscore.
- Perl 中的变量不能包含空格或除下划线以外的任何其他特殊字符。
Example:
示例:
$my-name = "John"; // Invalid
$my name = "John"; // Invalid
$my_name = "John"; // Valid
Declaration of a Variable
变量的声明
Variable Declaration is done on the basis of the datatype used to define the variable. These variables can be of three different datatypes:
变量的声明是根据定义该变量时所使用的数据类型来进行的。我们可以将这些变量分为三种不同的数据类型:
- Scalar Variables: It contains a single string or numeric value. It starts with $ symbol.
- 标量变量: 它包含单个字符串或数值。它以 $ 符号开头。
> Syntax: $var_name = value;
> 语法: $var_name = value;
Example:
示例:
$item = "Hello"
$item_one = 2
- Array Variables: It contains a randomly ordered set of values. It starts with @ symbol.
- 数组变量: 它包含一个有序的值列表(注:此处原文描述为randomly ordered,实际为有序列表)。它以 @ 符号开头。
> Syntax : @var_name = (val1, val2, val3, …..);
> 语法: @var_name = (val1, val2, val3, …..);
Example:
示例:
@price_list = (70, 30, 40);
@name_list = ("Apple", "Banana", "Guava");
- Hash Variables: It contains (key, value) pair efficiently accessed per key. It starts with % symbol.
- 哈希变量: 它包含(键,值)对,可以通过键进行高效访问。它以 % 符号开头。
> Syntax : %var_name = ( key1=>val1, key2=>val2, key3=>val3, …..);
> 语法: %var_name = ( key1=>val1, key2=>val2, key3=>val3, …..);
Example:
示例:
%item_pairs = ("Apple" =>2, "Banana‘=>3);
%pair_random = ("Hi" =>8, "Bye"=>9);
Modification of a Variable
修改变量
Perl allows modifying its variable values anytime after the variable declaration is done. There are various ways for the modification of a variable:
在 Perl 中,我们可以在变量声明后的任何时间修改变量的值。修改变量有多种方式:
- A scalar variable can be modified simply by redefining its value.
- 标量变量可以通过简单地重新定义其值来修改。
Example:
示例:
$name = "John";
# This can be modified by simply
# redeclaring the variable $name.
# 这可以通过简单地重新声明变量 $name 来完成。
$name = "Rahul";
- An element of an array can be modified by passing the index of that element to the array and defining a new value to it.
- 数组中的元素可以通过将该元素的索引传递给数组并为其定义一个新值来修改。
Example:
示例:
@array = ("A", "B", "C", "D", "E");
# If value of second variable is to
# be modified then it can be done by
# 如果要修改第二个变量的值,可以通过以下方式完成:
@array[2] = "4";
# $array[2] = "4"; is an alternate way of updating value in an array.
# 这是更新数组中值的另一种方式。
# This will change the array to,
# 这将把数组更改为:
# @array = ("A", "B", "4", "D", "E");
- A value in a hash can be modified by using its Key.
- 哈希中的值可以使用其键来修改。
Example:
示例:
%Hash = ("A", 10, "B", 20, "C", 30)
# This will modify the value
# assigned to Key ‘B‘
# 这将修改分配给键 ‘B‘ 的值
$Hash{"B"} = 46;
Variable Interpolation
变量插值
Perl provides various methods to define a String to a variable. This can be done with the use of single quotes, double quotes, using q-operator and double-q operator, etc.
Using single quotes and double quotes for writing strings is the same but there exists a slight difference between how they work. Strings that are written with the use of single quotes display the content written within it exactly as it is.
Perl 提供了多种方法来为变量定义字符串。我们可以通过使用单引号、双引号、q 运算符和双 q 运算符等来实现。虽然使用单引号和双引号编写字符串看起来很相似,但它们的工作方式存在细微差别。使用单引号编写的字符串会精确显示其中的内容,不做任何转换。
Example:
示例:
$name = "John"
print ‘Hi $name
How are you?‘
The above code will print:
上述代码将输出:
Hi $name
How are you?
Whereas strings written within double quotes replace the variables with their value and then display the string. It even replaces the escape sequences with their real use.
而在双引号内编写的字符串会将变量替换为其值,然后显示该字符串。它甚至会将转义序列替换为其实际效果。
Example:
示例:
$name = "John"
print "Hi $name
How are you?"
The above code will print:
上述代码将输出:
Hi John
How are you?
Example Code:
示例代码:
Perl
CODEBLOCK_c5c34994
Output:
输出:
Modified Array is G E F K S
Name is $name
Name is GeeksForGeeks$VAR1 = {
‘to‘ => 20,
‘Welcome‘ => 10,
‘Geeks‘ => 40
};