提交 b013a7af 编写于 作者: W wizardforcel

2019-08-06 10:36:48

上级 fbd4a857
# Perl - 列表和数组
> 原文: [https://beginnersbook.com/2017/05/perl-lists-and-arrays/](https://beginnersbook.com/2017/05/perl-lists-and-arrays/)
在 Perl 中,人们可以交替使用术语列表和数组,但是存在差异。 **列表**是数据(标量值的有序集合),**数组**是保存列表的变量。
## 如何定义数组?
阵列以 **@** 符号为前缀。这就是你定义一个数组的方法 -
```
@friends = ("Ajeet", "Chaitanya", "Rahul");
```
这是包含三个字符串的字符串数组。另一种做法是:
```
@friends = qw(Ajeet Chaitanya Rahul); #same as above
```
**注意:** **qw** 代表引用的单词,通过使用 qw 你可以避免引号并输入更少。
学习 Perl 时,您可能会遇到几个示例,其中您会看到以下类型的数组定义:
```
@friends = qw/Ajeet Chaitanya Rahul/; #same as above
```
这是因为 Perl 允许您选择任何标点字符作为分隔符。
以下所有陈述均相同:
```
@friends = qw/Ajeet Chaitanya Rahul/;
@friends = qw!Ajeet Chaitanya Rahul!;
@friends = qw;
@friends = qw{Ajeet Chaitanya Rahul};
@friends = qw[Ajeet Chaitanya Rahul];
```
注意:**开始和结束分隔符必须相同。**
## 访问数组元素
您必须在另一个编程语言中使用过数组,如 C,C ++,Java 等。数组的基本概念在这里是相同的。让我们举一个例子来了解如何定义数组以及如何访问它的元素。
```
#!/usr/bin/perl
@friends = ("Ajeet", "Chaitanya", "Rahul");
print "\$friends[0] = $friends[0]\n";
print "\$friends[1] = $friends[1]\n";
print "\$friends[2] = $friends[2]\n";
```
**输出:**
```
$friends[0] = Ajeet
$friends[1] = Chaitanya
$friends[2] = Rahul
```
正如你在上面的程序中看到的那样,数组是用 **@符号**进行的。因为,单个数组元素只是标量,它们是 **$符号**的预设。
#### 范围运算符:
范围运算符由双点“..”表示。此运算符用于创建顺序列表。例如:
```
#!/usr/bin/perl
@num = (3..9); # same as (3, 4, 5, 6, 7, 8, 9)
foreach $temp (@num) {
print "$temp\n";
}
```
**Output:**
```
3
4
5
6
7
8
9
```
让我们再看几个例子来理解范围运算符:
```
(2.9..7.9) # same as (2, 3, 4, 5, 6, 7), values after decimal are truncated
(9..3) # empty list, only works in increasing order
(1, 3..6, 10, 12..14) # same as (1, 3, 4, 5, 6, 10, 12, 13, 14),
```
#### 操作员:弹出和推动
**pop 运算符**从数组中删除最后一个元素并返回它。让我们举个例子来了解 pop 运算符的工作原理:
```
#!/usr/bin/perl
@num = (3..7); # same as (3, 4, 5, 6, 7)
$n1 = pop(@num); # $n1 is 7, array is (3, 4, 5, 6)
$n2 = pop(@num); # $n2 is 6, array is (3, 4, 5)
print "\$n1 is: $n1\n";
print "\$n2 is: $n2\n";
print "array now has:\n";
foreach $temp (@num) {
print "$temp\n";
}
pop @num; # 5 is discarded, array is (3, 4)
print "array now has:\n";
foreach $temp (@num) {
print "$temp\n";
}
```
**Output:**
```
$n1 is: 7
$n2 is: 6
array now has:
3
4
5
array now has:
3
4
```
**push operator** 在数组末尾添加一个元素。
**示例:**
```
#!/usr/bin/perl
@num = (10..12); # same as (10, 11, 12)
push (@num, 9); # array is (10, 11, 12, 9)
push (@num, 6); # array is (10, 11, 12, 9, 6)
print "array now has:\n";
foreach $temp (@num) {
print "$temp\n";
}
@num2 = (11, 22, 33);
push (@num, @num2); # adding another array to the end
print "array now has:\n";
foreach $temp (@num) {
print "$temp\n";
}
```
**Output:**
```
array now has:
10
11
12
9
6
array now has:
10
11
12
9
6
11
22
33
```
#### 经营者:转移和不转移
正如我们在上面看到的那样,push 和 pop 操作符在数组末尾添加和删除。 shift 和 unshift 在数组的开头执行操作。
**移位运算符:**
移位运算符的工作方式类似于 pop 运算符,但与 pop 运算符不同,它在数组的开头执行运算。
```
#!/usr/bin/perl
@num = (3..7); # same as (3, 4, 5, 6, 7)
$n1 = shift(@num); # $n1 is 3, array is (4, 5, 6, 7)
$n2 = shift(@num); # $n2 is 4, array is (5, 6, 7)
print "\$n1 is: $n1\n";
print "\$n2 is: $n2\n";
print "array now has:\n";
foreach $temp (@num) {
print "$temp\n";
}
shift @num; # 5 is discarded, array is (6, 7)
print "array now has:\n";
foreach $temp (@num) {
print "$temp\n";
}
```
**Output:**
```
$n1 is: 3
$n2 is: 4
array now has:
5
6
7
array now has:
6
7
```
**unshift 运算符:**
unshift 运算符的工作方式类似于 push 运算符,但与 push 运算符不同,它在数组的开头执行运算。
```
#!/usr/bin/perl
@num = (10..12); # same as (10, 11, 12)
unshift (@num, 9); # array is (9, 10, 11, 12)
unshift (@num, 6); # array is (6, 9, 10, 11, 12)
print "array now has:\n";
foreach $temp (@num) {
print "$temp\n";
}
@num2 = (11, 22, 33);
unshift (@num, @num2); # adding another array to the beginning
print "array now has:\n";
foreach $temp (@num) {
print "$temp\n";
}
```
**输出:**
```
array now has:
6
9
10
11
12
array now has:
11
22
33
6
9
10
11
12
```
#### 拼接算子
在上一节中,我们学习了如何在数组上执行 push,pop,shift 和 unshift 操作。但是这些运算符存在限制,它们只在数组的开头或数组的末尾执行。如果我们想在阵列中间执行操作怎么办?这就是拼接操作员进入画面的地方。
**语法:**
```
splice @array_name, s, l, @another_array
```
splice 运算符最多可以包含四个参数。
**第一个参数**是数组名,这里我们指定我们正在执行操作的数组
**第二个参数**是起点,如上所述,你可以执行在数组中间的 opeartion。这指定了操作的起点。
**第三个参数**是长度
**第四个参数**是另一个列表或数组。
让我们举几个例子来理解这个:
#### 示例 1:拼接中只有两个参数
```
@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2;
# removes everything after Ajeet
# @myarray is qw(Rahul, Joe, Ajeet)
# @myvar is qw(Tim, Lisa)
```
splice 运算符的第三和第四个参数是可选的。在上面的例子中,我们只提供了两个参数,数组和起始点。与数组类似,splice 运算符的索引从 0 开始,这意味着 Ajeet 是上例中的起点。如果我们只提供两个参数,那么 splice 运算符会在起始点之后删除所有内容。
#### 示例 2:三个参数
第三个参数指定已删除元素列表的长度。在上面的例子中,我们没有指定任何长度,这就是为什么它在起始点之后删除了所有内容。现在,让我们看看当我们提供第三个参数时会发生什么。
```
@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2, 1;
# removes only one element after Ajeet
# @myarray is qw(Rahul, Joe, Ajeet, Lisa)
# @myvar is qw(Tim)
```
#### 例 3:第四个论点
第四个参数是另一个列表或我们要插入到数组中的数组。
```
@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2, 1, qw(Harsh, Alisha);
# removes only one element after Ajeet
# inserts the provided list at the same position
# @myarray is qw(Rahul, Joe, Ajeet, Harsh, Alisha, Lisa)
# @myvar is qw(Tim)
```
#### 示例 4:如果我们不想删除任何内容,只需添加即可
如果您不想删除任何内容,只需要在数组中间添加一些元素,然后可以将长度指定为 0。
```
@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2, 0, qw(Harsh, Alisha);
# removes nothing
# inserts the provided list at the starting point
# @myarray is qw(Rahul, Joe, Ajeet, Harsh, Alisha, Tim, Lisa)
# @myvar is qw()
```
#### 反向运算符
reverse 运算符将一个元素(或列表)数组作为输入,并以相反的顺序返回。
例如:
```
@myarray = 10..15; # same as (10, 11, 12, 13, 14, 15)
@myarray2 = reverse @myarray; # @myarray2 has (15, 14, 13, 12, 11, 10)
@myarray3 = reverse 5..9; # @myarray3 has (9, 8, 7, 6, 5)
```
假设您想要反转数组的元素并将其存储到同一个数组中:
```
@myarray = 10..15;
@myarray = reverse @myarray;
```
**注意:**如果你只是写下面的语句那么它就不行了。
```
reverse @myarray;
```
这不会做任何事情,因为反向运算符不会更改数组元素的顺序,它只是以相反的顺序返回需要分配给数组的列表。
\ No newline at end of file
# Perl 中的哈希
> 原文: [https://beginnersbook.com/2017/05/hashes-in-perl/](https://beginnersbook.com/2017/05/hashes-in-perl/)
**哈希**是一组键值对。哈希变量以“%”符号为前缀。让我们先举一个简单的例子,然后我们将详细讨论哈希。
```
#!/usr/bin/perl
%age = ('Chaitanya Singh', 29, 'Ajeet', 28, 'Lisa', 25);
print "\$age{'Lisa'}: $age{'Lisa'}\n";
print "\$age{'Chaitanya Singh'}: $age{'Chaitanya Singh'}\n";
print "\$age{'Ajeet'}: $age{'Ajeet'}\n";
```
**输出:**
```
$age{'Lisa'}: 25
$age{'Chaitanya Singh'}: 29
$age{'Ajeet'}: 28
```
在上面的例子中,我们创建了一个哈希并显示了它的元素。让我们详细了解每个部分:
## 创建哈希
**第一种方法:**这是我们在上面的例子中看到的 -
```
%age = ('Chaitanya Singh', 29, 'Ajeet', 28, 'Lisa', 25);
```
这里'Chaitanya Singh'是键 1,29 是值 1
'Ajeet'是键 2,28 是值 2
类似'Lisa',25 是第三键值对。
**第二种方法:**这是创建哈希的**首选方式**,因为它提高了可读性。在这种方法中,我们用'=>'符号分隔每对的键和值。
例如:
```
%age = ('Chaitanya Singh' => 29, 'Ajeet' => 28, 'Lisa' => 25);
```
#### 有用的哈希函数:
**1)键功能:**
键功能返回哈希中所有键的列表。
**例:**
```
#!/usr/bin/perl
%age = ('Chaitanya Singh' => 29, 'Ajeet' => 28, 'Lisa' => 25);
my @k = keys %age;
print "Keys: @k\n";
```
**Output:**
```
Keys: Ajeet Chaitanya Singh Lisa
```
**2)值函数:**
values 函数返回散列中所有值的列表。
**例:**
```
#!/usr/bin/perl
%age = ('Chaitanya Singh' => 29, 'Ajeet' => 28, 'Lisa' => 25);
my @k = values %age;
print "Values: @k\n";
```
**Output:**
```
Values: 28 29 25
```
**3)每个函数:**
每个函数用于迭代哈希,它通常用于 while 循环。
**例:**
```
#!/usr/bin/perl
%age = ('Chaitanya Singh' => 29, 'Ajeet' => 28, 'Lisa' => 25);
while ( ($key, $value) = each %age ) {
print "Key: $key, Value: $value\n";
}
```
**Output:**
```
Key: Lisa, Value: 25
Key: Chaitanya Singh, Value: 29
Key: Ajeet, Value: 28
```
\ No newline at end of file
# Perl 运营商 - 完整指南
> 原文: [https://beginnersbook.com/2017/02/perl-operators-complete-guide/](https://beginnersbook.com/2017/02/perl-operators-complete-guide/)
运算符是表示动作的字符,例如+是表示加法的算术运算符。
perl 中的运算符分为以下类型:
1)基本算术运算符
2)赋值运算符
3)自动递增和自动递减运算符
4)逻辑运算符
5)比较运算符
6)按位运算符
7)引用和引用类运算符
## 1)基本算术运算符
基本算术运算符是:+, - ,*,/,%,**
**+** 用于加法:$ x + $ y
**-** 用于减法:$ x - $ y
***** 用于乘法:$ x * $ y
**/** 用于划分:$ x / $ y
**%**用于模数:$ x%$ y
**注:**它返回余数,例如 10%5 将返回 0
****** 用于指数:$ x ** $ y
x 到幂 y
#### 例
```
#!/usr/local/bin/perl
$x = -4;
$y = 2;
$result = $x + $y;
print '+ Operator output: ' . $result . "\n";
$result = $x - $y;
print '- Operator output: ' . $result . "\n";
$result = $x * $y;
print '* Operator output: ' . $result . "\n";
$result = $x / $y;
print '/ Operator output: ' . $result . "\n";
$result = $x % $y;
print '% Operator output: ' . $result. "\n";
$result = $x ** $y;
print '** Operator output: ' . $result . "\n";
```
**输出:**
```
+ Operator output: -2
- Operator output: -6
* Operator output: -8
/ Operator output: -2
% Operator output: 0
** Operator output: 16
```
## 2)分配操作员
perl 中的赋值运算符是:=,+ =, - =,* =,/ =,%=,** =
**$ x = $ y** 会将变量 y 的值赋给变量 x
**$ x + = $ y** 等于$ x = $ x + $ y
**$ x - = $ y** 等于$ x = $ x- $ y
**$ x * = $ y** 等于$ x = $ x * $ y
**$ x / = $ y** 等于$ x = $ x / $ y
**$ x%= $ y** 等于$ x = $ x%$ y
**$ x ** = $ y** 等于$ x = $ x ** $ y
#### 例:
```
#!/usr/local/bin/perl
$x = 5;
$result = 10;
print "\$x= $x and \$result=$result\n";
$result = $x;
print '= Operator output: ' . $result . "\n";
print "\$x= $x and \$result=$result\n";
$result += $x;
print '+= Operator output: ' . $result . "\n";
print "\$x= $x and \$result=$result\n";
$result -= $x;
print '-= Operator output: ' . $result . "\n";
print "\$x= $x and \$result=$result\n";
$result *= $x;
print '*= Operator output: ' . $result . "\n";
print "\$x= $x and \$result=$result\n";
$result /= $x;
print '/= Operator output: ' . $result . "\n";
print "\$x= $x and \$result=$result\n";
$result %= $x;
print '%= Operator output: ' . $result . "\n";
#assigning different value to $result for this operator
$result =2;
print "\$x= $x and \$result=$result\n";
$result **= $x;
print '**= Operator output: ' . $result . "\n";
```
**Output:**
```
$x= 5 and $result=10
= Operator output: 5
$x= 5 and $result=5
+= Operator output: 10
$x= 5 and $result=10
-= Operator output: 5
$x= 5 and $result=5
*= Operator output: 25
$x= 5 and $result=25
/= Operator output: 5
$x= 5 and $result=5
%= Operator output: 0
$x= 5 and $result=2
**= Operator output: 32
```
## 3)自动递增和自动递减运算符
++和 -
**$ x ++** 相当于$ x = $ x + 1;
**$ x-** 相当于$ x = $ x-1;
#### Example:
```
#!/usr/local/bin/perl
$x =100;
$y =200;
$x++;
$y--;
print"Value of \$x++ is: $x\n";
print"Value of \$y-- is: $y\n";
```
**Output:**
```
Value of $x++ is: 101
Value of $y-- is: 199
```
## 4)逻辑运算符
逻辑运算符与二进制变量一起使用。它们主要用于条件语句和循环以评估条件。
perl 中的逻辑运算符是:&&,and,||,或者不是,!
**&&** “和”**和**“相同
$ x& y 如果 x 和 y 都为真,则返回 true,否则返回 false。
**||** “和”**或**“相同。如果 x 和 y 都为假,则
$ x || $ y 将返回 false,否则返回 true。
**!** “和”**不是**“是相同的。
!$ x 将返回 x 的反面,这意味着如果 x 为 false 则为 true,如果 x 为 true 则返回 false。
#### Example
```
#!/usr/local/bin/perl
$x = true;
$y = false;
$result = ($x and $y);
print"\$x and \$y: $result\n";
$result = ($x && $y);
print"\$x && \$y: $result\n";
$result = ($x or $y);
print"\$x or \$y: $result\n";
$result = ($x || $y);
print"\$x || \$y: $result\n";
#point to note is that not operator works
#with 0 and 1 only.
$x=0;
$result = not($x);
print"not\$x: $result\n";
$result = !($x);
print"\!\$x: $result\n";
```
**Output:**
```
$x and $y: false
$x && $y: false
$x or $y: true
$x || $y: true
not$x: 1
!$x: 1
```
## 5)比较(关系)运算符
它们包括:==,eq,!=,ne,>,gt,<,lt,> =,ge,< =,le
==如果左侧和右侧均为 eq 则返回 true side 是相等的
!=如果左侧不等于运算符的右侧,则 ne 返回 true。
**>如果左侧大于右侧,****gt** 将返回 true。
**<如果左侧小于右侧,****lt** 返回 true。
**> =****ge** 如果左侧大于或等于右侧则返回 true。
**< =****le** 如果左侧小于或等于右侧则返回真。
#### Example
```
#!/usr/local/bin/perl
$x = 3;
$y = 6;
if( $x == $y ){
print "\$x and \$y are equal\n";
}else{
print "\$x and \$y are not equal\n";
}
if( $x != $y ){
print "\$x and \$y are not equal\n";
}else{
print "\$x and \$y are equal\n";
}
if( $x > $y ){
print "\$x is greater than \$y\n";
}else{
print "\$x is not greater than \$y\n";
}
if( $x >= $y ){
print "\$x is greater than or equal to \$y\n";
}else{
print "\$x is less than \$y\n";
}
if( $x < $y ){
print "\$x is less than \$y\n";
}else{
print "\$x is not less than \$y\n";
}
if( $x <= $y){
print "\$x is less than or equal to \$y\n";
}else{
print "\$x is greater than \$y\n";
}
```
**Output:**
```
$x and $y are not equal
$x and $y are not equal
$x is not greater than $y
$x is less than $y
$x is less than $y
$x is less than or equal to $y
```
## 6)按位运算符
有六个按位运算符:&amp;,|,^,〜,&lt;&lt;&gt;&gt;&gt;
$ x = 11; #00001011
$ y = 22; #00010110
按位运算符执行逐位处理。
**$ x&amp; $ y** 比较 x 和 y 的相应位,如果两个位相等则生成 1,否则返回 0\.
在我们的例子中它将返回:2,这是 00000010,因为只有 x 和 y 的二进制形式倒数第二位是匹配的。
**$ x | $ y** 比较 x 和 y 的相应位,如果任一位为 1 则生成 1,否则返回 0\.
在我们的例子中它将返回 31,即 00011111
**$ x ^ $ y** 比较 x 和 y 的相应位,如果它们不相等则生成 1,否则返回 0\.
在我们的例子中它将返回 29,相当于 00011101
**〜$ x** 是一个补码运算符,只是将位从 0 更改为 1,1 更改为 0
在我们的示例中,它将返回-12,其签名为 8 位,相当于 11110100
**&lt;&lt;** 是左移位运算符,向左移动位,丢弃最左边的位,并将最右边的位赋值为 0\.
输出情况输出为 44,相当于 00101100
**注意:**在下面的示例中,我们在此移位运算符的右侧提供 2,这是位向左移动两个位置的原因。我们可以更改此数字,并且位将按运算符右侧指定的位数移动。同样适用于右侧操作员。
**&gt;&gt;** 是右移位运算符,将位向右移动,丢弃最右位,并将最左边的位指定为 0\.
在我们的情况下输出为 2,相当于 00000010
#### Example:
```
#!/usr/local/bin/perl
use integer;
$x = 11; #00001011
$y = 22; #00010110
$result = $x & $y;
print "\$x & \$y: $result\n";
$result = $x | $y;
print "\$x | \$y: $result\n";
$result = $x ^ $y;
print "\$x ^ \$y: $result\n";
$result = ~$x;
print "~\$x = $result\n";
$result = $x << 2;
print "\$x << 2 = $result\n";
$result = $x >> 2;
print "\$x >> 2 = $result\n";
```
**Output:**
```
$x & $y: 2
$x | $y: 31
$x ^ $y: 29
~$x = -12
$x << 2 = 44
$x >> 2 = 2
```
## 7)引用和引用类操作符
perl 中的运算符有几个引用,如 q {},qq {},qw {},m {},qr {},s {} {},tr {} {},y {} {}。
但是经常只使用其中两个:q {}用于单引号,qq {}用于双引号。
#### Example
q {欢迎使用 beginnersbook}将返回'欢迎使用初学者专辑'
qq {欢迎使用初学者专辑}将返回“欢迎使用初学者专辑”
\ No newline at end of file
# Perl 中的条件语句
> 原文: [https://beginnersbook.com/2017/02/conditional-statements-in-perl/](https://beginnersbook.com/2017/02/conditional-statements-in-perl/)
在任何编程语言中,条件语句都被认为是最有用的特性之一。当我们需要执行不同的操作时,使用条件语句,具体取决于指定的条件是否为 true 或 false。在 perl 中,我们有以下条件语句。单击下面的链接以示例的方式详细阅读声明。
* [if 语句](https://beginnersbook.com/2017/02/if-statement-in-perl/) - 如果语句包含条件,如果条件计算结果为 true,那么“if”中的语句执行 else 则不执行
* [if-else 语句](https://beginnersbook.com/2017/02/if-else-statement-in-perl/) - 如果语句有一个可选的 else 语句,如果 if 语句中指定的条件求值为 true,则“if”中的语句执行 else“else”中的语句执行
* [if-elsif-else 语句](https://beginnersbook.com/2017/02/if-elsif-else-statement-in-perl/) - 当我们需要检查多个条件时使用,这个结构中可以有多个“elsif”语句。
* [除非声明](https://beginnersbook.com/2017/02/unless-statement-in-perl/) - 这与“if 语句”的行为正好相反。当指定条件返回 **false** 时,将执行“除非”内的此语句。
* [除非 - else 语句](https://beginnersbook.com/2017/02/unless-else-statement-in-perl/) - 如果“除非”语句中指定的条件求值为 false,则“除非”内的语句执行 else“else”内的语句执行
* [除非-elsif-else 语句](https://beginnersbook.com/2017/02/unless-elsif-else-statement-in-perl/) - 用于多个条件检查
* [switch case 语句](https://beginnersbook.com/2017/02/switch-case-in-perl/) - 自 Perl 5.10 起不推荐使用
* [给定时默认语句](https://beginnersbook.com/2017/02/given-when-default-statement-in-perl/) - 它是 switch case 的替代品
\ No newline at end of file
# 如果在 Perl 中声明
> 原文: [https://beginnersbook.com/2017/02/if-statement-in-perl/](https://beginnersbook.com/2017/02/if-statement-in-perl/)
如果语句包含条件,则后跟语句或一组语句,如下所示:
```
if(condition){
Statement(s);
}
```
只有在给定条件为真时才会执行语句。如果条件为 false,那么 if 语句体内的语句将被完全忽略。
#### 例:
在以下示例中,我们将一个整数值赋给变量“num”。使用 if 语句,我们检查分配给 num 的值是否小于 100。
```
#!/usr/local/bin/perl
printf "Enter any number:";
$num = <STDIN>;
if( $num < 100 ){
# This print statement would execute,
# if the above condition is true
printf "num is less than 100\n";
}
```
**输出:**
```
Enter any number:78
num is less than 100
```
## 在 perl 中嵌套 if 语句
当在另一个 if 语句中有 if 语句时,它将被称为嵌套 if 语句。
嵌套的结构如下所示:
```
if(condition_1) {
Statement1(s);
if(condition_2) {
Statement2(s);
}
}
```
如果 condition_1 为 true,则执行 Statement1。只有条件(condition_1 和 condition_2)都为真时,Statement2 才会执行。
#### Example:
```
#!/usr/local/bin/perl
printf "Enter any number: ";
$num = <STDIN>;
if( $num < 100 ){
printf "num is less than 100\n";
if( $num > 90 ){
printf "num is greater than 90\n";
}
}
```
**Output:**
```
Enter any number: 99
num is less than 100
num is greater than 90
```
\ No newline at end of file
# 如果是 Perl 中的 else 语句
> 原文: [https://beginnersbook.com/2017/02/if-else-statement-in-perl/](https://beginnersbook.com/2017/02/if-else-statement-in-perl/)
[上一篇教程](https://beginnersbook.com/2017/02/if-statement-in-perl/)中,我们了解了 perl 中的 if 语句。在本教程中,我们将了解 if-else 条件语句。这是 if-else 语句的外观:
```
if(condition) {
Statement(s);
}
else {
Statement(s);
}
```
如果条件为真,则“if”内的语句将执行,如果条件为假,则“else”内的语句将执行。
#### 例:
```
#!/usr/local/bin/perl
printf "Enter any number:";
$num = <STDIN>;
if( $num >100 ) {
# This print statement would execute,
# if the above condition is true
printf "number is greater than 100\n";
}
else {
#This print statement would execute,
#if the given condition is false
printf "number is less than 100\n";
}
```
**输出:**
```
Enter any number:120
number is greater than 100
```
\ No newline at end of file
# perl 中的 if-elsif-else 语句
> 原文: [https://beginnersbook.com/2017/02/if-elsif-else-statement-in-perl/](https://beginnersbook.com/2017/02/if-elsif-else-statement-in-perl/)
当我们需要检查多个条件时使用 if-elsif-else 语句。在这个声明中,我们只有一个“if”和一个“else”,但是我们可以有多个“elsif”。这是它的样子:
```
if(condition_1) {
#if condition_1 is true execute this
statement(s);
}
elsif(condition_2) {
#execute this if condition_1 is not met and
#condition_2 is met
statement(s);
}
elsif(condition_3) {
#execute this if condition_1 & condition_2 are
#not met and condition_3 is met
statement(s);
}
.
.
.
else {
#if none of the condition is true
#then these statements gets executed
statement(s);
}
```
**注意:**这里需要注意的最重要的一点是,在 if-elsif-else 语句中,只要满足条件,就会执行相应的语句集,忽略 rest。如果没有满足条件,则执行“else”内的语句。
#### 例:
```
#!/usr/local/bin/perl
printf "Enter any integer between 1 & 99999:";
$num = <STDIN>;
if( $num <100 && $num>=1) {
printf "Its a two digit number\n";
}
elsif( $num <1000 && $num>=100) {
printf "Its a three digit number\n";
}
elsif( $num <10000 && $num>=1000) {
printf "Its a four digit number\n";
}
elsif( $num <100000 && $num>=10000) {
printf "Its a five digit number\n";
}
else {
printf "Please enter number between 1 & 99999\n";
}
```
**输出:**
```
Enter any integer between 1 & 99999:8019
Its a four digit number
```
\ No newline at end of file
# 除非在 Perl 中声明
> 原文: [https://beginnersbook.com/2017/02/unless-statement-in-perl/](https://beginnersbook.com/2017/02/unless-statement-in-perl/)
除非 perl 中的语句与 perl 中的 [if 语句正好相反。当给定条件为假时,它在其体内执行语句集。](https://beginnersbook.com/2017/02/if-statement-in-perl/)
```
unless(condition) {
statement(s);
}
```
除非正文在条件为假时执行,否则**内的语句。**
#### 例
```
#!/usr/local/bin/perl
printf "Enter any number:";
$num = <STDIN>;
unless($num>=100) {
printf "num is less than 100\n";
}
```
**输出:**
```
Enter any number:99
num is less than 100
```
\ No newline at end of file
# 在 Perl 中的 except-else 语句
> 原文: [https://beginnersbook.com/2017/02/unless-else-statement-in-perl/](https://beginnersbook.com/2017/02/unless-else-statement-in-perl/)
类似于[,除非声明](https://beginnersbook.com/2017/02/unless-statement-in-perl/),Perl 中的**除非 -** 语句与 [if-else 语句](https://beginnersbook.com/2017/02/if-else-statement-in-perl/)相反。在 except-else 中,除非条件为 false,否则执行内部语句,如果条件为真,则执行 else 内的语句。
```
unless(condition) {
#These statements would execute
#if the condition is false.
statement(s);
}
else {
#These statements would execute
#if the condition is true.
statement(s);
}
```
#### 例
```
#!/usr/local/bin/perl
printf "Enter any number:";
$num = <STDIN>;
unless($num>=100) {
#This print statement would execute,
#if the given condition is false
printf "num is less than 100\n";
}
else {
#This print statement would execute,
#if the given condition is true
printf "number is greater than or equal to 100\n";
}
```
**输出:**
```
Enter any number:100
number is greater than or equal to 100
```
\ No newline at end of file
# 除非 elsif 在 Perl 声明
> 原文: [https://beginnersbook.com/2017/02/unless-elsif-else-statement-in-perl/](https://beginnersbook.com/2017/02/unless-elsif-else-statement-in-perl/)
当我们需要检查多个条件时,使用 unless-elsif-else 语句。在这个声明中,我们只有一个“除非”和一个“其他”,但我们可以有多个“elsif”。这是它的样子:
```
unless(condition_1){
#These statements would execute if
#condition_1 is false
statement(s);
}
elsif(condition_2){
#These statements would execute if
#condition_1 & condition_2 are true
statement(s);
}
elsif(condition_3){
#These statements would execute if
#condition_1 is true
#condition_2 is false
#condition_3 is true
statement(s);
}
.
.
.
else{
#if none of the condition is met
#then these statements gets executed
statement(s);
}
```
#### 例
```
#!/usr/local/bin/perl
printf "Enter any number:";
$num = <STDIN>;
unless( $num == 100) {
printf "Number is not 100\n";
}
elsif( $num==100) {
printf "Number is 100\n";
}
else {
printf "Entered number is not 100\n";
}
```
**输出:**
```
Enter any number:101
Number is not 100
```
\ No newline at end of file
# 在 Windows,Mac,Linux 和 Unix 上安装 Perl
> 原文: [https://beginnersbook.com/2017/02/installing-perl-on-windows-mac-linux-and-unix/](https://beginnersbook.com/2017/02/installing-perl-on-windows-mac-linux-and-unix/)
在大多数情况下,您已经在系统上安装了它,因为多个操作系统已预安装它。要检查系统上是否有它,可以转到命令提示符(mac 中的终端)并输入不带引号的“perl -v”。如果你的系统上有它,那么你应该看到这样的消息:
```
This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)
Copyright 1987-2013, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
```
但是,如果您没有收到此消息,则可以免费安装 perl。步骤如下:
## 在 Windows 上安装 Perl
在 Windows 上安装 Perl 有两种​​方法(更具体地说是两种发行版)。一个是使用 ActivePerl 另一个是 StrawberryPerl,你可以选择其中之一。但是,如果你更关心你应该选择哪一个,你可以参考[这个讨论](https://stackoverflow.com/questions/3365518/should-i-choose-activeperl-or-strawberry-perl-for-windows)
**安装 Active Perl** :转到此链接: [http://www.activestate.com/activeperl](https://www.activestate.com/activeperl) 并下载安装文件,安装步骤不言自明。
**要安装 Strawberry Perl** :请访问此链接: [http://strawberryperl.com/](http://strawberryperl.com/) 并根据您的系统下载 32 位或 64 位版本。安装下载的文件。
## 在 Linux / Unix 上安装 Perl
转到此链接: [https://www.perl.org/get.html](https://www.perl.org/get.html) 点击 Linux / Unix 选项卡。您应该看到类似“下载最新的稳定源(5.24.1)”,点击它并下载压缩文件,在撰写本文时,5.24.1 是最新的稳定版本,您可能会发现它不同,不要担心只需下载文件。将它保存在您的主目录中,该目录应该是您当前的工作目录,使用以下 shell 命令安装 perl:
```
tar -xzf perl-5.24.1.tar.gz
cd perl-5.24.1
$./Configure -de
$make
$make test
$make install
```
注意:您可能需要根据已下载的源文件更改上述 shell 命令中的版本,例如:如果您已下载版本 5.10.1,那么命令集将是:
```
tar -xzf perl-5.10.1.tar.gz
cd perl-5.10.1
./Configure -de
make
make test
make install
```
## 在 Mac OS 上安装 Perl
在 Mac 上安装 perl 与 Linux / Unix 类似,请转到相同的链接 [https://www.perl.org/get.html](https://www.perl.org/get.html) 并单击 Mac OS X 选项卡。下载源文件(单击“下载最新的稳定源”)并在终端中运行以下命令。
首先更改终端中已下载压缩文件的目录。例如如果您已下载文件在 Downloads 目录中,则通过键入以下命令更改目录:
```
cd ~/Downloads
```
进入压缩文件的目录后,运行以下命令:
```
tar -xzf perl-5.24.1.tar.gz
cd perl-5.24.1
./Configure -de
make
make test
make install
```
请记住根据您下载的版本更改上述命令中的版本。对于例如如果您已经下载了 perl 5.10.1,那么在上面的命令中将 5.24.1 更改为 5.10.1。
\ No newline at end of file
# Perl 中的 Switch Case
> 原文: [https://beginnersbook.com/2017/02/switch-case-in-perl/](https://beginnersbook.com/2017/02/switch-case-in-perl/)
在 Perl 5 中不推荐使用**开关案例**。如果您想知道为什么它被弃用,这就是答案:
Switch 案例可能会在代码的其他部分产生语法错误。如果在 heredoc 中存在“case”,则在 perl 5.10.x 上可能会导致语法错误。一般来说,使用 given / when 代替。它是在 perl 5.10.0 中引入的。 Perl 5.10.0 于 2007 年发布。[来源](http://search.cpan.org/~chorny/Switch-2.17/Switch.pm#BUGS)
然而,三个新的关键字:给定,何时和默认在 Perl 5 中引入,提供类似于 switch case 的功能。这是它的样子:
```
given (argument) {
when (condition) { statement(s); }
when (condition) { statement(s); }
when (condition) { statement(s); }
.
.
.
default { statement(s); }
}
```
通过示例了解更多关于[给定时的默认情况。](https://beginnersbook.com/2017/02/given-when-default-statement-in-perl/)
\ No newline at end of file
# Perl 中的给定时默认语句
> 原文: [https://beginnersbook.com/2017/02/given-when-default-statement-in-perl/](https://beginnersbook.com/2017/02/given-when-default-statement-in-perl/)
正如我在[上一篇文章](https://beginnersbook.com/2017/02/switch-case-in-perl/)中所讨论的那样,转换案例在 Perl 5 中被弃用。三个新关键字:**给出,当****默认**在 Perl 5 中引入时提供功能类似于 switch case。
#### 句法:
```
given (argument) {
when (condition) { statement(s); }
when (condition) { statement(s); }
when (condition) { statement(s); }
.
.
.
default { statement(s); }
}
```
#### 例:
```
#!/usr/local/bin/perl
use v5.10;
no warnings 'experimental';
printf "Enter any number:";
$num = <STDIN>;
given($num){
when ($num>10) {
printf "number is greater than 10\n";
}
when ($num<10) {
printf "number is less than 10\n";
}
default {
printf "number is equal to 10\n";
}
}
```
**输出:**
```
Enter any number:10
number is equal to 10
```
\ No newline at end of file
# Perl 中的循环和循环控制语句
> 原文: [https://beginnersbook.com/2017/02/loops-and-loop-control-statements-in-perl/](https://beginnersbook.com/2017/02/loops-and-loop-control-statements-in-perl/)
循环用于重复执行一组语句,直到满足特定条件。在 perl 中,我们有以下循环结构。单击下面的链接,通过示例详细阅读它们。
* 循环的[:“For”循环用于重复一个语句或一组语句,已知次数。例如,如果我们想要显示从 1 到 10 的数字,那么我们可以使用执行 10 次的 for 循环。当事先不知道次数时,我们使用“While”循环。](https://beginnersbook.com/2017/02/for-loop-in-perl-with-example/)
* [while 循环](https://beginnersbook.com/2017/02/while-loop-in-perl-with-example/):如上所述,当我们事先不知道需要多少次重复循环时,我们更喜欢使用 while 循环。
* [do-while 循环](https://beginnersbook.com/2017/02/perl-do-while-loop-with-example/):它类似于 while 循环,但它们之间存在差异,而循环在评估条件后执行其中的语句集。而 do-while 首先执行语句集,然后评估条件。这就是为什么当我们需要至少执行一次语句时,我们更喜欢 do-while 循环。
* [foreach 循环](https://beginnersbook.com/2017/02/perl-foreach-loop-with-example/):当我们需要迭代数组时,通常使用此循环
* [直到循环](https://beginnersbook.com/2017/02/until-loop-in-perl-with-example/):此循环与 while 循环的行为正好相反。它执行语句块,直到给定条件返回 true。
\ No newline at end of file
# 对于 Perl 中的循环示例
> 原文: [https://beginnersbook.com/2017/02/for-loop-in-perl-with-example/](https://beginnersbook.com/2017/02/for-loop-in-perl-with-example/)
在本教程中,我们将学习如何在 Perl 中使用“for loop”。循环用于重复执行一组语句,直到满足特定条件。
#### for 循环的语法:
```
for(initialization; condition ; increment/decrement)
{
statement(s);
}
```
### for 循环的执行流程
当程序执行时,解释器总是跟踪将要执行的语句。我们将其称为控制流程或程序的执行流程。
第一步:在 for 循环中,初始化只发生一次,这意味着 for 循环的初始化部分只执行一次。
第二步:在每次迭代时评估 **for 循环**的条件,如果条件为真,则循环体内的语句被执行。一旦条件返回 false,for 循环中的语句就不会执行,并且控制在 for 循环后被转移到程序中的下一个语句。
第三步:每次执行 for 循环体后,for 循环的递增/递减部分执行。
第四步:第三步后,控制跳转到第二步,重新评估条件。
#### 例:
```
#!/usr/local/bin/perl
for( $num = 100; $num < 105; $num = $num + 1 ){
print "$num\n";
}
```
**输出:**
```
100
101
102
103
104
```
\ No newline at end of file
# 而 Perl 循环用例子
> 原文: [https://beginnersbook.com/2017/02/while-loop-in-perl-with-example/](https://beginnersbook.com/2017/02/while-loop-in-perl-with-example/)
在上一个教程中,我们在 Perl 中讨论了 [for 循环。在本教程中,我们将讨论 while 循环。如前一个教程中所讨论的,循环用于重复执行一组语句,直到满足特定条件。](https://beginnersbook.com/2017/02/for-loop-in-perl-with-example/)
#### while 循环的语法:
```
while(condition)
{
statement(s);
}
```
### while 循环的执行流程
在 while 循环中,首先计算条件,如果它返回 true,则 while 循环中的语句执行。当 condition 返回 false 时,控件退出循环并跳转到 while 循环后的下一个语句。
**注意:**使用 while 循环时要注意的重点是我们需要在 while 循环中使用递增或递减语句,以便循环变量在每次迭代时都会发生变化,并且在某些情况下返回 false。这样我们就可以结束 while 循环的执行,否则循环将无限期地执行。
#### 例
```
#!/usr/local/bin/perl
$num = 100;
while( $num > 95 ){
printf "$num\n";
$num--;
}
```
**输出:**
```
100
99
98
97
96
```
\ No newline at end of file
# Perl - do-while 循环示例
> 原文: [https://beginnersbook.com/2017/02/perl-do-while-loop-with-example/](https://beginnersbook.com/2017/02/perl-do-while-loop-with-example/)
在上一个教程中,我们讨论了 [while 循环](https://beginnersbook.com/2017/02/while-loop-in-perl-with-example/)。在本教程中,我们将讨论 Perl 中的 do-while 循环。 do-while 循环类似于 while 循环,但是它们之间存在差异:在 while 循环中,在执行循环体之前评估条件,但是在执行循环体之后评估 do-while 循环条件。
#### do-while 循环的语法:
```
do
{
statement(s);
} while(condition);
```
### 执行循环的流程
首先,循环内的语句执行,然后条件得到评估,如果条件返回 true,则控件转移到“do”,否则它会在 do-while 之后跳转到下一个语句。
#### 例
```
#!/usr/local/bin/perl
$num = 90;
do{
$num++;
printf "$num\n";
}while( $num < 100 );
```
**输出:**
```
91
92
93
94
95
96
97
98
99
100
```
\ No newline at end of file
# Perl - foreach 循环示例
> 原文: [https://beginnersbook.com/2017/02/perl-foreach-loop-with-example/](https://beginnersbook.com/2017/02/perl-foreach-loop-with-example/)
foreach 循环用于迭代数组/列表。
#### foreach 循环的语法:
```
foreach var (list) {
statement(s);
}
```
### foreach 循环的执行流程
在 foreach 循环中,循环继续执行,直到处理指定数组的所有元素。
#### 例
```
#!/usr/local/bin/perl
@friends = ("Ajeet", "Tom", "Steve", "Lisa", "Kev");
foreach $str (@friends){
print "$str\n";
}
```
**输出:**
```
Ajeet
Tom
Steve
Lisa
Kev
```
\ No newline at end of file
# 直到 Perl 中的循环为例
> 原文: [https://beginnersbook.com/2017/02/until-loop-in-perl-with-example/](https://beginnersbook.com/2017/02/until-loop-in-perl-with-example/)
直到循环行为与 perl 中的循环的[正好相反,while 循环继续执行,只要条件为真。在 until 循环中,只要条件为假,循环就会执行。](https://beginnersbook.com/2017/02/while-loop-in-perl-with-example/)
#### until 循环的语法:
```
until(condition)
{
statement(s);
}
```
### 执行直到循环的流程
首先评估条件,如果返回 false,则循环内的语句被执行。执行后,重新评估条件。这一直持续到条件返回 true。一旦条件返回 true,控件就会转到 next 循环之后的下一个语句。
#### 例
```
#!/usr/local/bin/perl
$num = 10;
until( $num >15 ){
printf "$num\n";
$num++;
}
```
**输出:**
```
10
11
12
13
14
15
```
\ No newline at end of file
# Perl 中的子程序
> 原文: [https://beginnersbook.com/2017/02/subroutines-and-functions-in-perl/](https://beginnersbook.com/2017/02/subroutines-and-functions-in-perl/)
Perl 允许您定义自己的函数,称为**子程序**。它们用于代码可重用性,因此您不必一次又一次地编写相同的代码。例如,如果您想在程序的多个位置从用户那里获取输入,那么您可以在子例程中编写代码并在需要输入的任何位置调用子例程。这样您就不必再次编写相同的代码,这也提高了代码的可读性。
#### 子程序的优点:
1)代码可重用性
2)提高代码可读性
### 句法:
#### 定义子程序
您需要做的第一件事是创建一个子例程。 **sub** 关键字用于定义 Perl 程序中的子程序。您可以选择任何有意义的子例程名称。
```
sub subroutine_name
{
statement(s);
return;
}
```
#### 调用子程序
通过使用前缀为“**&amp;的子程序名称来调用子程序。** “性格。您也可以在调用子例程时传递参数。
```
&subroutine_name; #calling without parameter
&subroutine_name(10);
```
### 子程序的简单例子
让我们举一个简单的例子来理解这个:
```
#!/usr/bin/perl
my $msg;
# defining three subroutines
sub ask_user {
printf "Please enter something: ";
}
sub get_input {
$msg = <STDIN>;
return $msg;
}
sub show_message {
printf "You entered: $msg";
}
#calling subroutines
&ask_user;
&get_input;
&show_message;
```
**输出:**
```
Please enter something: Welcome to beginnersbook
You entered: Welcome to beginnersbook
```
### 将参数传递给子例程
在上面的例子中,我们在调用子例程时没有传递任何参数,但是我们可以在调用子例程时传递各种参数。所有参数(通常称为参数)都存储在特殊数组( **@_** )中。让我们看看下面的例子来理解这个:
```
#!/usr/bin/perl
# defining subroutine
sub printparams {
printf "@_\n";
return;
}
#calling subroutine
&printparams("This", "is", "my", "blog");
```
**Output:**
```
This is my blog
```
在上面的例子中,我们使用特殊数组(@_)打印了所有参数。但是,如果需要,可以使用“$ _ [n]”显示所选参数,其中 n 是参数的索引,例如$ _ [0]将引用第一个参数,$ _ [1]将引用第二个参数。让我们举一个例子:
```
#!/usr/bin/perl
# defining subroutine
sub printparams {
printf "First Parameter: $_[0]\n";
printf "Fourth Parameter: $_[3]\n";
return;
}
#calling subroutine
&printparams("This", "is", "my", "blog");
```
**Output:**
```
First Parameter: This
Fourth Parameter: blog
```
### 将数组传递给子例程
在上面的例子中,我们在调用子例程时传递了一些字符串参数。我们也可以将数组传递给子程序。这是一个例子:
```
#!/usr/bin/perl
# defining subroutine
sub printparams {
printf "First Parameter: $_[0]\n";
printf "Third Parameter: $_[2]\n";
printf "Fourth Parameter: $_[3]\n";
printf "Sixth Parameter: $_[5]\n";
return;
}
@array1 = ("This", "is", "text");
$num = 100;
@array2 = ("Welcome", "here");
#calling subroutine
&printparams(@array1, @array2, $num);
```
**Output:**
```
First Parameter: This
Third Parameter: text
Fourth Parameter: Welcome
Sixth Parameter: 100
```
### 将哈希传递给子程序
```
#!/usr/bin/perl
sub DisplayMyHash{
#copying passed hash to the hash
my %hash = @_;
for my $key (keys %hash) {
print "Key is: $key and value is: $hash{$key}\n";
}
}
%hash = ('Item1', 'Orange', 'Item2', 'Apple', 'Item3', 'Banana');
# Function call with hash parameter
DisplayMyHash(%hash);
```
**Output:**
```
Key is: Item1 and value is: Orange
Key is: Item2 and value is: Apple
Key is: Item3 and value is: Banana
```
\ No newline at end of file
# Perl - 字符串
> 原文: [https://beginnersbook.com/2018/03/perl-strings/](https://beginnersbook.com/2018/03/perl-strings/)
字符串是一系列字符。正如我们在 Scalars 指南中所讨论的那样,字符串被认为是 Perl 编程语言中的标量变量。有两种方法可以使用'(单引号)和“(双引号)在 Perl 中定义字符串。
## 1\. Perl 中的字符串声明和初始化
由于字符串是标量,它们的名称以$
**开头例如:**
```
my $website = "BeginnersBook";
my $editor = "Chaitanya";
print "$editor works for $website";
```
输出:
```
Chaitanya works for BeginnersBook
```
## 2 字符串插值 - 单引号和双引号
我已经在 Perl 文章的 [Scalars 中介绍了这个主题。](https://beginnersbook.com/2017/05/scalars-in-perl/)
引号不是字符串的一部分,它们只是指定字符串的开头和结尾。您可能认为**单引号和双引号**都有相同的用途,但事实并非如此。它们用于不同的 2 例。要理解这两者之间的区别,让我们看一下下面的例子。
```
#!/usr/bin/perl
print "Welcome to\t Beginnersbook.com\n";
print 'Welcome to\t Beginnersbook.com\n';
This would produce the following output:
Welcome to Beginnersbook.com
Welcome to\t Beginnersbook.com\n
```
您可以清楚地看到双引号内插转义序列“\ t”和“\ n”的区别,但是单引号没有。
**注意:**我们将在下一个教程中详细讨论转义序列。
**例 2:单引号和双引号:**
```
#!/usr/bin/perl
$website = "Beginnersbook";
print "Website Name: $website\n";
print 'Website Name: $website\n';
```
**输出:**
```
Website Name: Beginnersbook
Website Name: $website\n
```
这是双引号的另一个优点,因为它们是**“可变插值”**。这意味着双引号内的变量名称将替换为其值。单引号字符串中不会发生这种情况。
### 2.1 单引号的使用
您可能正在考虑避免在 perl 程序中使用单引号,但在某些情况下您可能希望使用单引号。
例如如果你想在变量中存储**电子邮件地址**,那么双引号会引发错误,在这种情况下你需要使用单引号。对于例如
```
$email = "[email protected]"; would throw an error
$email = '[email protected]'; would work fine.
```
这里的问题是 **@gmail****内插为数组**。以@符号开头的变量被内插为数组。我们还可以通过使用 **\转义序列**避免双引号错误,我们已在下一节中讨论过。
### 2.2 Perl 中的反斜杠
反斜杠可以执行以下两个任务之一:
1)它消除了跟随它的角色的特殊含义。对于例如打印“\ $ myvar”;会产生$ myvar 输出而不是变量 myvar 值,因为$前面的反斜杠(\)消除了$的特殊含义
2)它是反斜杠或转义序列的开始。对于例如\ n 是用于换行的转义序列
#### 2.2.1 有什么用?
相信我,你会在 perl 中开发应用程序时经常使用它。假设您要打印包含双引号的字符串。对于例如如果我想打印:你好这是“我的博客”然后我需要使用以下逻辑:
```
#!/usr/bin/perl
$msg = "Hello This is \"My blog\"";
print "$msg\n";
```
**Output:**
```
Hello This is "My blog"
```
## 3.字符串操作
让我们看看我们可以对字符串执行的操作。
### 3.1 连接
要连接字符串,请使用点(。)运算符。在下面的例子中,我们连接三个字符串,$ str1,空格和$ str2。
```
$str1 = "Cherry";
$str2 = "Coco";
$mystr = $str1 . " " . $str2;
print "$mystr\n";
```
Output:
```
Cherry Coco
```
### 3.2 重复
要重复具有指定次数的字符串,请使用字符 x 后跟数字。在下面的示例中,我们使用了字符 x 之后的数字 4,这就是字符串在输出中重复四次的原因。
```
$str = "Cherry";
$mystr = $str x 4;
print "$mystr\n";
```
Output:
```
CherryCherryCherryCherry
```
### 3.3 获取 substring - substr()函数
substr()函数用于从给定字符串中获取子字符串。
```
use strict;
use warnings;
# This is our original string
my $str = "I know who you are, I will find you";
print "My original String is: $str\n";
# substring - starting from 8th char till the end of string
my $substr1 = substr($str, 7);
print "My First substring is: $substr1\n";
# substring - starting from 8th char and length of 11
my $substr2 = substr($str, 7, 11);
print "My Second substring is: $substr2\n";
```
Output:
```
My original String is: I know who you are, I will find you
My First substring is: who you are, I will find you
My Second substring is: who you are
```
该相同功能可用于用新内容替换字符串的一部分。让我们举个例子来理解这个:
```
use strict;
use warnings;
# This is our original string
my $str = "I know who you are, I will find you";
print "My original String is: $str\n";
my $newstr = substr($str, 7, 11, "you are the killer");
print "My updated String is: $str\n"
```
Output:
```
My original String is: I know who you are, I will find you
My updated String is: I know you are the killer, I will find you
```
### 3.4 在 Perl - length()函数中查找 String 的长度
要在 Perl 中查找字符串的长度,请使用 length()函数。此函数计算字符串中的字符数(包括空格)并返回它。
```
use strict;
use warnings;
my $str = "I know who you are, I will find you";
print "length of my string is:", length($str);
```
Output:
```
length of my string is:35
```
### 3.5 Perl 中的字符串比较
为了比较两个字符串,我们使用 Perl 中的 eq [运算符。](https://beginnersbook.com/2017/02/perl-operators-complete-guide/)
```
use strict;
use warnings;
my $str = "hello";
my $str2 = "hello";
if ($str eq $str2){
print "str and str2 are same\n";
}
```
Output:
```
str and str2 are same
```
\ No newline at end of file
# 第一个 Perl 计划
> 原文: [https://beginnersbook.com/2017/02/first-perl-program/](https://beginnersbook.com/2017/02/first-perl-program/)
在本教程中,我们将学习如何编写第一个 perl 程序并在各种操作系统上运行它。
## 第一个 Perl 计划:Hello World
这是一个打印 Hello World 的简单 perl 程序!屏幕上。
```
#!/usr/bin/perl
#this is a comment
print "Hello World!!";
```
这里第一条指令**#!/ usr / bin / perl** 告诉操作系统使用位于/ usr / bin / perl 的解释器运行该程序。
正如我在 perl 介绍中解释的那样 perl 是一种使用普通英语的高级语言,问题是机器不能理解这种高级语言,因此为了使这个程序可以被机器理解,我们必须将它解释为机器语言。解释器是将该程序解释为机器语言的原因。
第二条指令**#这是一条评论**是一条评论,无论你在哈希(#)之后写的是什么都被认为是评论
第三条指令**打印“Hello World !!”;** 打印 Hello World !!屏幕上。注意指令末尾的分号(;),你必须在每条指令的末尾加一个分号,这告诉解释器指令已经完成。
## 如何运行 perl 程序?
为了运行上面的程序,将代码复制到文本编辑器,如 Windows 的记事本,mac 的 textedit 等,并使用.pl 扩展名保存文件。假设我们将文件保存为 firstprogram.pl。您可以使用任何名称保存程序。 Perl 不会强迫您使用特殊类型的名称,您可以随意使用您想要的任何名称。
### 在 Windows 上
要在 Windows 上运行该程序,请打开命令提示符(cmd)并编写以下命令
```
perl c:\perl\bin\perl.exe firstprogram.pl
```
**注意:**上面的方法是使用命令提示符运行 perl 程序。或者,如果您使用 Strawberry Perl 或 Active Perl,则只需单击“运行”即可。
### 在 Linux / Unix / Mac OS 上
在 Linux,Unix 或 Mac OS 上运行程序。在终端上运行以下命令。
```
chmod +x firstprogram.pl
./firstprogram
```
**如何在脚本中指定 Perl 的版本?**
在某些情况下,我们要指定 Perl 的版本以使我们的脚本正常工作。例如,在 perl 5.10 或更高版本中,您可以使用“say”代替“print”。这是 **Perl 5.10** 中引入的新功能。
```
#!/usr/bin/perl
say "Hello World!!";
```
但是,如果您使用的是 5.10 之前的 Perl 版本,则无法使用。要解决此问题,您可以像这样重写上述程序:
```
#!/usr/bin/perl
use 5.010;
say "Hello World!!";
```
**的帮助下使用**语句我们指定了 Perl 的版本,这个程序只能在 Perl 5.10 或更高版本中使用。
注意:Perl 期望三位数的子版本,这就是我们提到 5.010 的原因,如果你使用 5.10 而不是 Perl 认为提到的版本是 5.100。同样,如果要指定版本 5.11,则在程序中使用 5.011 而不是 5.11。
\ No newline at end of file
# Perl String Escape 序列
> 原文: [https://beginnersbook.com/2018/03/perl-string-escape-sequences/](https://beginnersbook.com/2018/03/perl-string-escape-sequences/)
在上一个教程中,我们学习了[如何使用 Perl 中的字符串](https://beginnersbook.com/2018/03/perl-strings/)。在本指南中,我们将讨论**转义字符**,它将帮助我们在某些情况下实现所需的输出。
## 在 Perl 中显示电子邮件地址
字符@在 perl 中有特殊含义。正如我们已经知道的那样,当我们将特殊字符放在双引号字符串中时,perl 会尝试对其进行插值。在下面的示例中,如果我们不在@ then 之前放置反斜杠而不是显示电子邮件,则会抛出错误,因为它会将@gmail 视为[数组](https://beginnersbook.com/2017/05/perl-lists-and-arrays/)
在单引号的情况下,不需要使用转义序列,因为插值不会出现在单引号字符串中。
```
use strict;
use warnings;
my $email = "xyz\@gmail.com";
print "$email\n";
# no backslash needed as interpolation does not
# work in single quotes.
my $email2 = '[email protected]';
print "$email2\n";
```
输出:
```
[email protected]
[email protected]
```
## 转义双引号字符串中的$符号
我们已经知道美元符号会插入变量的值。如果你想逃避$符号并避免插值,请使用我们上面做的相同技巧 - 用反斜杠作为前缀。
```
use strict;
use warnings;
my $name = 'Negan';
my $msg = 'I am Negan';
# escaping the first dollar sign but not escaping the second
print "\$name: $name\n";
# escaping the first dollar sign but not escaping the second
print "\$msg: $msg\n";
```
Output:
```
$name: Negan
$msg: I am Negan
```
## 如何转义转义字符反斜杠(\)
在上面的例子中,我们使用反斜杠来转义特殊字符$和@。可能存在您希望在输出中显示反斜杠的情况。为此,你想要逃避反斜杠。让我们举个例子来理解这个:
```
use strict;
use warnings;
my $say = 'I do like to use backslash \\';
print "$say\n";
```
Output:
```
I do like to use backslash \
```
如您所见,我们在输出中显示了\。
## 转义字符串中的双引号
我们知道双引号内的文本在 Perl 中被视为字符串。让我们说我们想在 Perl 中显示一个 String,字符串本身有一个双引号。我们将使用相同的方法,使用\来转义引号。
```
use strict;
use warnings;
my $say = "I like to watch \"The Walking Dead\"";
print "$say\n";
```
Output:
```
I like to watch "The Walking Dead"
```
## 双 q 运算符 - qq
我们可以用双 q 运算符替换用于包含字符串的双引号。这样做的好处是我们不必担心使用双引号(“)和括号的转义序列。
```
use strict;
use warnings;
my $name = 'Chaitanya';
print qq(My name is "$name" and I like brackets ()\n);
```
Output:
```
My name is "Chaitanya" and I like brackets ()
```
请参阅我没有使用双引号和括号的转义序列。
## 单 q 运算符 - q
单 q 运算符就像单引号一样工作。其中存在的特殊字符不进行插值。
让我们采用上面在 double q 运算符中看到的相同示例。
```
use strict;
use warnings;
my $name = 'Chaitanya';
print q(My name is "$name" and I like brackets ()\n);
```
Output:
```
My name is "$name" and I like brackets ()\n
```
\ No newline at end of file
# Perl 语法
> 原文: [https://beginnersbook.com/2017/02/perl-syntax/](https://beginnersbook.com/2017/02/perl-syntax/)
我们在[上一篇教程中熟悉了 Perl 的基础知识:第一个 perl 程序](https://beginnersbook.com/2017/02/first-perl-program/)。在本教程中,我们将了解有关 Perl 语法和命名约定的几个要点。
### Perl 文件命名约定
Perl 文件名可以包含数字,符号,字母和下划线(_),但文件名中不允许使用空格。例如 hello_world.pl 是一个有效的文件名,但是 hello world.pl 是一个无效的文件名。
Perl 文件可以使用.pl 或.PL 扩展名保存。
### Perl 中单引号和双引号之间的区别
单引号和双引号在 Perl 程序中表现不同。要了解差异,让我们看看下面的代码:
```
#!/usr/bin/perl
print "Welcome to\t Beginnersbook.com\n";
print 'Welcome to\t Beginnersbook.com\n';
```
这将产生以下输出:
```
Welcome to Beginnersbook.com
Welcome to\t Beginnersbook.com\n
```
您可以清楚地看到双引号内插转义序列“\ t”和“\ n”的区别,但是单引号没有。
另一个例子:
```
#!/usr/bin/perl
$website = "Beginnersbook";
print "Website Name: $website\n";
print 'Website Name: $website\n';
```
**输出:**
```
Website Name: Beginnersbook
Website Name: $website\n
```
#### 使用单引号
您可能正在考虑避免在 perl 程序中使用单引号,但在某些情况下您可能希望使用单引号。
例如如果要将电子邮件地址存储在变量中,那么双引号会引发错误,在这种情况下需要使用单引号。对于例如
$ email =“ [[email protected]](/cdn-cgi/l/email-protection) ”;会抛出错误
$ email =' [[email protected]](/cdn-cgi/l/email-protection) ';会工作得很好。
### Perl 中的反斜杠
反斜杠可以执行以下两个任务之一:
1)它消除了跟随它的字符的特殊含义。对于例如打印“\ $ myvar”;会产生$ myvar 输出而不是变量 myvar 值,因为在$之前的反斜杠(\)消除了$
的特殊含义 2)它是反斜杠或转义序列的开始。对于例如\ n 是用于换行的转义序列
#### 有什么用?
相信我,你会在 perl 中开发应用程序时经常使用它。假设您要打印包含双引号的字符串。对于例如如果我想打印:你好这是“我的博客”然后我需要使用以下逻辑:
```
#!/usr/bin/perl
$msg = "Hello This is \"My blog\"";
print "$msg\n";
```
**Output:**
```
Hello This is "My blog"
```
\ No newline at end of file
# Perl 中的数据类型
> 原文: [https://beginnersbook.com/2017/02/data-types-in-perl/](https://beginnersbook.com/2017/02/data-types-in-perl/)
Perl 有三种数据类型:**标量****标量数组****哈希**(也称为关联数组,字典或地图)。在 perl 中,我们不需要指定数据类型,解释器会根据数据的上下文自动选择它。
对于例如在下面的代码中,我将一个整数和一个字符串分配给两个不同的变量,而不指定任何类型。解释器将选择 age 作为整数类型,并根据分配给它们的数据将其命名为字符串类型。
```
#!/usr/bin/perl
$age=29;
$name="Chaitanya";
```
#### 标量
标量是单个数据单元。它可以是数字,浮点数,字符,字符串等。在 perl 中,它们以美元符号($)为前缀。详细阅读 [](https://beginnersbook.com/2017/05/scalars-in-perl/)
例如:
```
$num1=51;
$str1="beginnersbook";
$num2=2.9;
$str2="hello";
```
#### 数组
它们是有序的标量列表,前缀为“@”符号。索引从 0 开始,这意味着访问数组的第一个元素,我们需要使用索引值为 0(零)。例如。
```
#!/usr/bin/perl
@pincode = (301019, 282005, 101010);
print "\$pincode[0] = $pincode[0]\n";
```
**输出:**
密码[0] = 301019
如果您想知道打印指令中使用的反斜杠(\),它用于避免首次出现 pincode [0]的插值。您可以在此处阅读有关数组[的更多信息](https://beginnersbook.com/2017/05/perl-lists-and-arrays/)
#### 哈希
它们是无序的键值对组。它们以百分号(%)为前缀。
```
#!/usr/bin/perl
%ages = ('Chaitanya', 29, 'Ajeet', 28, 'Tom', 40);
```
这里“年龄”是具有键值对的散列。其中 key 是名称,value 是 age。要访问 Ajeet 的年龄,我们使用$ age {'Ajeet'}指令。
在这里了解更多关于哈希的例子[](https://beginnersbook.com/2017/05/hashes-in-perl/)
\ No newline at end of file
# Perl 变量
> 原文: [https://beginnersbook.com/2017/02/perl-variables/](https://beginnersbook.com/2017/02/perl-variables/)
perl 中有三种类型的变量:标量,标量和散列数组。让我们在示例的帮助下逐一学习它们。
### 标量
[Scalars](https://beginnersbook.com/2017/05/scalars-in-perl/) 是单一数据单元。标量可以是整数,浮点数,字符串等。标量变量以“$”符号为前缀。让我们看看下面的 perl 脚本,其中我们有三个标量变量。
```
#!/usr/bin/perl
# Integer
$age = 29;
# String
$name = "Chaitanya Singh";
# Float
$height = 180.88;
print "Name: $name\n";
print "Age: $age\n";
print "Height: $height\n";
```
**输出:**
```
Name: Chaitanya Singh
Age: 29
Height: 180.88
```
### 数组
[数组](https://beginnersbook.com/2017/05/perl-lists-and-arrays/)是标量的有序列表,数组变量的前缀为“@”符号,如下例所示:
```
#!/usr/bin/perl
@friends = ("Ajeet", "Leo", "Rahul", "Dhruv");
print "\$friends[0] = $friends[0]\n";
print "\$friends[1] = $friends[1]\n";
print "\$friends[2] = $friends[2]\n";
print "\$friends[3] = $friends[3]\n";
```
**Output:**
```
$friends[0] = Ajeet
$friends[1] = Leo
$friends[2] = Rahul
$friends[3] = Dhruv
```
### 哈希(也称为关联数组)
[哈希](https://beginnersbook.com/2017/05/hashes-in-perl/)是一组键值对。哈希变量以“%”符号为前缀。让我们看看下面的例子:
```
#!/usr/bin/perl
%age = ('Chaitanya Singh', 29, 'Ajeet', 28, 'Lisa', 25);
print "\$age{'Lisa'}: $age{'Lisa'}\n";
print "\$age{'Chaitanya Singh'}: $age{'Chaitanya Singh'}\n";
print "\$age{'Ajeet'}: $age{'Ajeet'}\n";
```
**Output:**
```
$age{'Lisa'}: 25
$age{'Chaitanya Singh'}: 29
$age{'Ajeet'}: 28
```
\ No newline at end of file
# my keyword - Perl 中的本地和全局变量
> 原文: [https://beginnersbook.com/2017/05/my-keyword-local-and-global-variables-in-perl/](https://beginnersbook.com/2017/05/my-keyword-local-and-global-variables-in-perl/)
当我们谈论局部和全局变量时,我们实际上在讨论变量的范围。
## 局部变量
局部变量范围是局部的,它存在于这两个花括号(通常称为代码块)之间,在该块之外这个变量不存在。这些变量也称为**词汇变量**
这些变量可以在任何代码块中使用,它可以在任何控制结构块中,如 if,if-else 等,或任何循环块,如 for,while,do-while 等或任何子例程的块,它甚至可以出现在匿名区块中。例如 -
```
#!/usr/bin/perl
use strict;
use warnings;
if (1<2)
{
my $age = 29;
print "$age\n"; # prints 29
}
```
我知道这个例子没有任何意义,但我的观点是向您展示局部变量在程序中的外观。在上面的程序变量中,$ age 在 if 中被声明,因此这个变量只对这个块是本地的。如果我们尝试在 if 体外访问此变量,我们会收到错误。让我们看看这个在行动,运行以下程序。
```
#!/usr/bin/perl
use strict;
use warnings;
if (1<2)
{
my $age = 29;
print "$age\n"; # prints 29
}
print "$age\n";
```
**输出:**
```
Global symbol "$age" requires explicit package name at demo.pl line 9.
Execution of demo.pl aborted due to compilation errors.
```
**注意:**
1)使用我的关键字声明局部变量,如上面的程序所示。
2)由于局部变量的范围仅限于块,因此可以在不同的块中使用相同名称的局部变量而不会发生任何冲突。
3)在整个程序中可以访问在编译指示之后的程序开头使用 my 关键字声明的变量。例如,程序中可以访问此程序中的变量$ age。
```
#!/usr/bin/perl
use strict;
use warnings;
my $age =29;
if (1<2)
{
print "$age\n";
}
print "$age\n";
```
**Output:**
```
29
29
```
**关于我的关键字很少有重要的事情:**
直到现在,我们已经了解到我的关键字用于声明局部变量,让我们看一下关键词的重点。
1)使用 my 关键字声明多个变量时,必须使用括号,否则只声明一个局部变量。例如,
```
my $num1, $num2; # This would not declare $num2.
my ($num1, $num2); # This is the correct way. It declares both
```
2)除了变量之外,您还可以使用 my 关键字声明本地数组和本地哈希。
例如,此代码声明本地数组@friends。
```
my @friends;
```
## 全局变量:
这不需要任何介绍,因为我们在我们的程序中使用它们,因为我们启动了 perl 教程。在没有声明的情况下直接使用的所有变量(使用我的关键字)都可以从程序的每个部分访问。例如,
```
#!/usr/bin/perl
use warnings;
if (1<2)
{
$age =29; #no declaration using my keyword
print "$age\n";
}
print "$age\n"; #accessible outside the block
```
**Output:**
```
29
29
```
**注意:**在这个程序中,我们删除了 use strict pragma,因为它强制我们在使用之前使用 my 关键字声明所有变量。
\ No newline at end of file
# Perl 中的标量
> 原文: [https://beginnersbook.com/2017/05/scalars-in-perl/](https://beginnersbook.com/2017/05/scalars-in-perl/)
Perl 中的标量数据被认为是单数,它可以是数字或字符串。我们已经介绍了[变量](https://beginnersbook.com/2017/02/perl-variables/)[数据类型](https://beginnersbook.com/2017/02/data-types-in-perl/)教程中的标量。在本教程中,我们将讨论关于字符串的一些重要事项,我们会跳过数字,因为使用它们非常简单。
## 单引号字符串 vs 双引号字符串
引号不是字符串的一部分,它们只是指定字符串的开头和结尾。在 Perl 中,单引号和双引号的行为不同。要了解差异,让我们看看下面的代码:
```
#!/usr/bin/perl
print "Welcome to\t Beginnersbook.com\n";
print 'Welcome to\t Beginnersbook.com\n';
This would produce the following output:
Welcome to Beginnersbook.com
Welcome to\t Beginnersbook.com\n
```
您可以清楚地看到双引号内插转义序列“\ t”和“\ n”的区别,但是单引号没有。
**另一个例子:**
```
#!/usr/bin/perl
$website = "Beginnersbook";
print "Website Name: $website\n";
print 'Website Name: $website\n';
```
**输出:**
```
Website Name: Beginnersbook
Website Name: $website\n
```
这是双引号的另一个优点,因为它们是**“可变插值”**。这意味着双引号内的变量名称将替换为其值。单引号字符串中不会发生这种情况。
## 使用单引号
您可能正在考虑避免在 perl 程序中使用单引号,但在某些情况下您可能希望使用单引号。
例如如果要将电子邮件地址存储在变量中,那么双引号会引发错误,在这种情况下需要使用单引号。对于例如
```
$email = "[email protected]"; would throw an error
$email = '[email protected]'; would work fine.
```
## Perl 中的反斜杠
反斜杠可以执行以下两个任务之一:
1)它消除了跟随它的字符的特殊含义。对于例如打印“\ $ myvar”;会产生$ myvar 输出而不是变量 myvar 值,因为在$之前的反斜杠(\)消除了$
的特殊含义 2)它是反斜杠或转义序列的开始。对于例如\ n 是用于换行的转义序列
#### 有什么用?
相信我,你会在 perl 中开发应用程序时经常使用它。假设您要打印包含双引号的字符串。对于例如如果我想打印:你好这是“我的博客”然后我需要使用以下逻辑:
```
#!/usr/bin/perl
$msg = "Hello This is \"My blog\"";
print "$msg\n";
```
**Output:**
```
Hello This is "My blog"
```
## 字符串操作
#### 级联:
您可以使用点(。)运算符连接字符串。例如:
```
"Hello"."Chaitanya" # Equivalent to "HelloChaitanya"
"Hello"."Chaitanya"."\n" # Equivalent to "HelloChaitanya\n"
```
#### 字符串重复:
语法:MyString x MyNumber
这将通过“MyNumber”次重复字符串“MyString”。
这里 x 应该是小写的。
例如:
```
"book" x 4 # Same as "bookbookbookbook"
"Tom" x (1+2) # Same as "Tom" x 3 or "TomTomTom"
"Wow" x 2.7 # Same as "Wow" x 2 or "WowWow"
```
在第三个例子中,2.7 被认为是 2,这里没有数字四舍五入。
**注意:** Perl 将 x 运算符之前的部分视为字符串,将 x 之后的部分视为数字。换句话说,您可以说左操作数被视为字符串而右操作数被视为数字。
例如:
```
8 x 3 # Same as "8" x 3 or "888"
```
使用 x 运算符时必须记住这一点,以避免混淆。 8 x 3 不是 24 而是“888”。
\ No newline at end of file
# 在 Perl 中使用严格和使用警告
> 原文: [https://beginnersbook.com/2017/05/use-strict-and-use-warnings-in-perl/](https://beginnersbook.com/2017/05/use-strict-and-use-warnings-in-perl/)
几乎每个 perl 脚本中都可以找到以下几行。
```
use strict;
use warnings;
```
在本文中,我们将逐一讨论它们。
**注意:**您可能无法在本网站提供的某些脚本中找到这些编译指示,这是为了避免与初学者混淆。但是,您会在高级主题上找到它们。
开始吧。
## 用严格
use strict 语句称为 pragma,它可以放在脚本的开头,如下所示:
```
#!/usr/local/bin/perl
use strict;
```
**它做了什么?**
它会强制您正确编码以使您的程序不易出错。例如:它强制您在使用它们之前声明变量。您可以使用“my”关键字声明变量。 “my”关键字将变量的范围限制为 local。它使代码更易读,更不容易出错。
如果你没有使用我的关键字声明变量,那么创建的变量将是全局的,你应该避免,将变量的范围缩小到需要它的位置是一个很好的编程习惯。
#### 例:
如果使用“use strict”但不声明变量。
```
#!/usr/local/bin/perl
use strict;
$s = "Hello!\n";
print $s;
```
它会抛出这个错误:
```
Global symbol "$s" requires explicit package name at st.pl line 3.
Global symbol "$s" requires explicit package name at st.pl line 4.
Execution of st.pl aborted due to compilation errors.
```
要避免错误,您必须使用 my 关键字声明变量。
```
#!/usr/local/bin/perl
use strict;
my $s = "Hello!\n";
print $s;
```
**输出:**
```
Hello!
```
同样,您需要在使用它们之前声明数组和哈希值。
**注意:**从 Perl 5.12 开始,隐式启用此编译指示,这意味着如果您使用的是 Perl 5.12 或更高版本,则无需使用行**使用 strict** 作为编译指示默认情况下启用。
## 使用警告
这是另一个 pragma,它们一起使用如下:
```
#!/usr/local/bin/perl
use strict;
use warnings;
```
**注意:**使用警告 pragma 在 Perl 5.6 中引入,所以如果你使用的是 Perl 5.6 或更高版本,你就可以了。如果您使用的是旧版本,可以打开如下警告:将-w 放在'shebang'行。
```
#!/usr/local/bin/perl -w
```
即使在 Perl 5.6 或更高版本上,这也适用于任何地方。
**“使用警告”有什么用?**
它可以帮助您找到打字错误,它会在您看到程序出错时向您发出警告。它可以帮助您更快地找到程序中的错误。
**注意:**这里要注意的最重要的一点是“use strict”会在程序发现错误时中止程序的执行。另一方面,使用警告只会为您提供警告,它不会中止执行。
**结论:**
你应该总是在你的程序中使用这两个 pragma,因为它是一个很好的编程习惯。
\ No newline at end of file
+ [在 Windows,Mac,Linux 和 Unix 上安装 Perl](2.md)
+ [第一个 Perl 计划](3.md)
+ [Perl 语法](4.md)
+ [Perl 中的数据类型](5.md)
+ [Perl 变量](6.md)
+ [my keyword - Perl 中的本地和全局变量](7.md)
+ [Perl 中的标量](8.md)
+ [在 Perl 中使用严格和使用警告](9.md)
+ [Perl - 列表和数组](10.md)
+ [Perl 中的哈希](11.md)
+ [Perl 运营商 - 完整指南](12.md)
+ [Perl 中的条件语句](13.md)
+ [如果在 Perl 中声明](14.md)
+ [如果是 Perl 中的 else 语句](15.md)
+ [perl 中的 if-elsif-else 语句](16.md)
+ [除非在 Perl 中声明](17.md)
+ [在 Perl 中的 except-else 语句](18.md)
+ [除非 elsif 在 Perl 声明](19.md)
+ [Perl 中的 Switch Case](20.md)
+ [Perl 中的给定时默认语句](21.md)
+ [Perl 中的循环和循环控制语句](22.md)
+ [对于 Perl 中的循环示例](23.md)
+ [而 Perl 循环用例子](24.md)
+ [Perl - do-while 循环示例](25.md)
+ [Perl - foreach 循环示例](26.md)
+ [直到 Perl 中的循环为例](27.md)
+ [Perl 中的子程序](28.md)
+ [Perl - 字符串](29.md)
+ [Perl String Escape 序列](30.md)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册