提交 fdde4492 编写于 作者: W wizardforcel

2019-10-16 22:18:40

上级 5454efb6
......@@ -8,13 +8,13 @@
数组以`@`符号为前缀。这就是你定义一个数组的方法:
```
```perl
@friends = ("Ajeet", "Chaitanya", "Rahul");
```
这是包含三个字符串的字符串数组。另一种做法是:
```
```perl
@friends = qw(Ajeet Chaitanya Rahul); #same as above
```
......@@ -22,7 +22,7 @@
学习 Perl 时,您可能会遇到几个示例,其中您会看到以下类型的数组定义:
```
```perl
@friends = qw/Ajeet Chaitanya Rahul/; #same as above
```
......@@ -30,7 +30,7 @@
以下所有语句均相同:
```
```perl
@friends = qw/Ajeet Chaitanya Rahul/;
@friends = qw!Ajeet Chaitanya Rahul!;
@friends = qw;
......@@ -44,7 +44,7 @@
您必须在另一个编程语言中使用过数组,如 C,C++,Java 等。数组的基本概念在这里是相同的。让我们举一个例子来了解如何定义数组以及如何访问它的元素。
```
```perl
#!/usr/bin/perl
@friends = ("Ajeet", "Chaitanya", "Rahul");
......@@ -56,7 +56,7 @@ print "\$friends[2] = $friends[2]\n";
**输出:**
```
```perl
$friends[0] = Ajeet
$friends[1] = Chaitanya
$friends[2] = Rahul
......@@ -68,7 +68,7 @@ $friends[2] = Rahul
范围运算符由双点`..`表示。此运算符用于创建顺序列表。例如:
```
```perl
#!/usr/bin/perl
@num = (3..9); # same as (3, 4, 5, 6, 7, 8, 9)
......@@ -79,7 +79,7 @@ foreach $temp (@num) {
**输出:**
```
```perl
3
4
5
......@@ -91,7 +91,7 @@ foreach $temp (@num) {
让我们再看几个例子来理解范围运算符:
```
```perl
(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),
......@@ -101,7 +101,7 @@ foreach $temp (@num) {
`pop`运算符从数组中删除最后一个元素并返回它。让我们举个例子来了解`pop`运算符的工作原理:
```
```perl
#!/usr/bin/perl
@num = (3..7); # same as (3, 4, 5, 6, 7)
......@@ -122,7 +122,7 @@ foreach $temp (@num) {
**输出:**
```
```perl
$n1 is: 7
$n2 is: 6
array now has:
......@@ -138,7 +138,7 @@ array now has:
**示例:**
```
```perl
#!/usr/bin/perl
@num = (10..12); # same as (10, 11, 12)
......@@ -158,7 +158,7 @@ foreach $temp (@num) {
**输出:**
```
```perl
array now has:
10
11
......@@ -184,7 +184,7 @@ array now has:
`shift`运算符的工作方式类似于`pop`运算符,但与`pop`运算符不同,它在数组的开头执行运算。
```
```perl
#!/usr/bin/perl
@num = (3..7); # same as (3, 4, 5, 6, 7)
......@@ -205,7 +205,7 @@ foreach $temp (@num) {
**输出:**
```
```perl
$n1 is: 3
$n2 is: 4
array now has:
......@@ -221,7 +221,7 @@ array now has:
`unshift`运算符的工作方式类似于`push`运算符,但与`push`运算符不同,它在数组的开头执行运算。
```
```perl
#!/usr/bin/perl
@num = (10..12); # same as (10, 11, 12)
......@@ -241,7 +241,7 @@ foreach $temp (@num) {
**输出:**
```
```perl
array now has:
6
9
......@@ -265,7 +265,7 @@ array now has:
**语法:**
```
```perl
splice @array_name, s, l, @another_array
```
......@@ -279,7 +279,7 @@ splice @array_name, s, l, @another_array
#### 示例 1:`splice`中只有两个参数
```
```perl
@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2;
# removes everything after Ajeet
......@@ -293,7 +293,7 @@ splice @array_name, s, l, @another_array
第三个参数指定已删除元素列表的长度。在上面的例子中,我们没有指定任何长度,这就是为什么它在起始点之后删除了所有内容。现在,让我们看看当我们提供第三个参数时会发生什么。
```
```perl
@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2, 1;
# removes only one element after Ajeet
......@@ -305,7 +305,7 @@ splice @array_name, s, l, @another_array
第四个参数是另一个列表或我们要插入到数组中的数组。
```
```perl
@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2, 1, qw(Harsh, Alisha);
# removes only one element after Ajeet
......@@ -318,7 +318,7 @@ splice @array_name, s, l, @another_array
如果您不想删除任何内容,只需要在数组中间添加一些元素,然后可以将长度指定为 0。
```
```perl
@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2, 0, qw(Harsh, Alisha);
# removes nothing
......@@ -331,7 +331,7 @@ splice @array_name, s, l, @another_array
`reverse`运算符将一个元素(或列表)数组作为输入,并以相反的顺序返回。例如:
```
```perl
@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)
......@@ -339,14 +339,14 @@ splice @array_name, s, l, @another_array
假设您想要反转数组的元素并将其存储到同一个数组中:
```
```perl
@myarray = 10..15;
@myarray = reverse @myarray;
```
**注意**:如果你只是写下面的语句那么它就不行了。
```
```perl
reverse @myarray;
```
......
......@@ -4,7 +4,7 @@
**哈希**是一组键值对。哈希变量以`%`符号为前缀。让我们先举一个简单的例子,然后我们将详细讨论哈希。
```
```perl
#!/usr/bin/perl
%age = ('Chaitanya Singh', 29, 'Ajeet', 28, 'Lisa', 25);
......@@ -16,7 +16,7 @@ print "\$age{'Ajeet'}: $age{'Ajeet'}\n";
**输出:**
```
```perl
$age{'Lisa'}: 25
$age{'Chaitanya Singh'}: 29
$age{'Ajeet'}: 28
......@@ -28,7 +28,7 @@ $age{'Ajeet'}: 28
**第一种方法**:这是我们在上面的例子中看到的:
```
```perl
%age = ('Chaitanya Singh', 29, 'Ajeet', 28, 'Lisa', 25);
```
......@@ -38,7 +38,7 @@ $age{'Ajeet'}: 28
例如:
```
```perl
%age = ('Chaitanya Singh' => 29, 'Ajeet' => 28, 'Lisa' => 25);
```
......@@ -50,7 +50,7 @@ $age{'Ajeet'}: 28
**例:**
```
```perl
#!/usr/bin/perl
%age = ('Chaitanya Singh' => 29, 'Ajeet' => 28, 'Lisa' => 25);
......@@ -60,7 +60,7 @@ print "Keys: @k\n";
**输出:**
```
```perl
Keys: Ajeet Chaitanya Singh Lisa
```
......@@ -69,7 +69,7 @@ Keys: Ajeet Chaitanya Singh Lisa
`values`函数返回散列中所有值的列表。
**例:**
```
```perl
#!/usr/bin/perl
%age = ('Chaitanya Singh' => 29, 'Ajeet' => 28, 'Lisa' => 25);
......@@ -79,7 +79,7 @@ print "Values: @k\n";
**输出:**
```
```perl
Values: 28 29 25
```
......@@ -89,7 +89,7 @@ Values: 28 29 25
**例:**
```
```perl
#!/usr/bin/perl
%age = ('Chaitanya Singh' => 29, 'Ajeet' => 28, 'Lisa' => 25);
......@@ -100,7 +100,7 @@ while ( ($key, $value) = each %age ) {
**输出:**
```
```perl
Key: Lisa, Value: 25
Key: Chaitanya Singh, Value: 29
Key: Ajeet, Value: 28
......
......@@ -42,7 +42,7 @@ x 到的 y 次幂
#### 例
```
```perl
#!/usr/local/bin/perl
$x = -4;
$y = 2;
......@@ -68,7 +68,7 @@ print '** Operator 输出: ' . $result . "\n";
**输出:**
```
```perl
+ Operator 输出: -2
- Operator 输出: -6
* Operator 输出: -8
......@@ -91,7 +91,7 @@ perl 中的赋值运算符是:`=, +=, -=, *=, /=, %=, **=`
#### 例:
```
```perl
#!/usr/local/bin/perl
$x = 5;
......@@ -130,7 +130,7 @@ print '**= Operator 输出: ' . $result . "\n";
**输出:**
```
```perl
$x= 5 and $result=10
= Operator 输出: 5
$x= 5 and $result=5
......@@ -157,7 +157,7 @@ $x= 5 and $result=2
#### 示例:
```
```perl
#!/usr/local/bin/perl
$x =100;
......@@ -170,7 +170,7 @@ print"Value of \$y-- is: $y\n";
**输出:**
```
```perl
Value of $x++ is: 101
Value of $y-- is: 199
```
......@@ -189,7 +189,7 @@ perl 中的逻辑运算符是:`&&`,`||`,`!`
#### 示例
```
```perl
#!/usr/local/bin/perl
$x = true;
......@@ -216,7 +216,7 @@ print"\!\$x: $result\n";
**输出:**
```
```perl
$x and $y: false
$x && $y: false
$x or $y: true
......@@ -238,7 +238,7 @@ not$x: 1
#### 示例
```
```perl
#!/usr/local/bin/perl
$x = 3;
......@@ -283,7 +283,7 @@ if( $x <= $y){
**输出:**
```
```perl
$x and $y are not equal
$x and $y are not equal
$x is not greater than $y
......@@ -296,7 +296,7 @@ $x is less than or equal to $y
有六个按位运算符:`&, |, ^, ~, <<, >>`
```
```perl
$x = 11; #00001011
$y = 22; #00010110
```
......@@ -320,7 +320,7 @@ $y = 22; #00010110
#### 示例:
```
```perl
#!/usr/local/bin/perl
use integer;
......@@ -348,7 +348,7 @@ print "\$x >> 2 = $result\n";
**输出:**
```
```perl
$x & $y: 2
$x | $y: 31
$x ^ $y: 29
......
......@@ -4,7 +4,7 @@
`if`语句包含条件,后跟一个语句或一组语句,如下所示:
```
```perl
if(condition){
Statement(s);
}
......@@ -16,7 +16,7 @@ if(condition){
在以下示例中,我们将一个整数值赋给变量`num`。使用`if`语句,我们检查分配给`num`的值是否小于 100。
```
```perl
#!/usr/local/bin/perl
printf "Enter any number:";
......@@ -30,7 +30,7 @@ if( $num < 100 ){
**输出:**
```
```perl
Enter any number:78
num is less than 100
```
......@@ -40,7 +40,7 @@ num is less than 100
当在另一个`if`语句中有`if`语句时,它将被称为嵌套`if`语句。
嵌套的结构如下所示:
```
```perl
if(condition_1) {
Statement1(s);
......@@ -54,7 +54,7 @@ if(condition_1) {
#### 示例:
```
```perl
#!/usr/local/bin/perl
printf "Enter any number: ";
......@@ -70,7 +70,7 @@ if( $num < 100 ){
**输出:**
```
```perl
Enter any number: 99
num is less than 100
num is greater than 90
......
......@@ -4,7 +4,7 @@
[上一篇教程](https://beginnersbook.com/2017/02/if-statement-in-perl/)中,我们了解了 perl 中的`if`语句。在本教程中,我们将了解`if-else`条件语句。这是`if-else`语句的外观:
```
```perl
if(condition) {
Statement(s);
}
......@@ -17,7 +17,7 @@ else {
#### 例:
```
```perl
#!/usr/local/bin/perl
printf "Enter any number:";
......@@ -36,7 +36,7 @@ else {
**输出:**
```
```perl
Enter any number:120
number is greater than 100
```
\ No newline at end of file
......@@ -4,7 +4,7 @@
当我们需要检查多个条件时使用`if-elsif-else`语句。在这个声明中,我们只有一个`if`和一个`else`,但是我们可以有多个`elsif`。这是它的样子:
```
```perl
if(condition_1) {
#if condition_1 is true execute this
statement(s);
......@@ -33,7 +33,7 @@ else {
#### 例:
```
```perl
#!/usr/local/bin/perl
printf "Enter any integer between 1 & 99999:";
......@@ -57,7 +57,7 @@ else {
**输出:**
```
```perl
Enter any integer between 1 & 99999:8019
Its a four digit number
```
\ No newline at end of file
......@@ -4,7 +4,7 @@
perl 中的`unless`语句与 perl 中的[`if`语句正好相反。当给定条件为假时,它执行其体内的语句集](https://beginnersbook.com/2017/02/if-statement-in-perl/)
```
```perl
unless(condition) {
statement(s);
}
......@@ -14,7 +14,7 @@ unless(condition) {
#### 例
```
```perl
#!/usr/local/bin/perl
printf "Enter any number:";
......@@ -26,7 +26,7 @@ unless($num>=100) {
**输出:**
```
```perl
Enter any number:99
num is less than 100
```
\ No newline at end of file
......@@ -4,7 +4,7 @@
类似于[`unless`语句](https://beginnersbook.com/2017/02/unless-statement-in-perl/),Perl 中的`unless-else`语句与[`if-else`语句](https://beginnersbook.com/2017/02/if-else-statement-in-perl/)相反。在`except-else`中,如果条件为`false`,执行内部语句,如果条件为`true`,则执行`else`内的语句。
```
```perl
unless(condition) {
#These statements would execute
#if the condition is false.
......@@ -19,7 +19,7 @@ else {
#### 例
```
```perl
#!/usr/local/bin/perl
printf "Enter any number:";
......@@ -38,7 +38,7 @@ else {
**输出:**
```
```perl
Enter any number:100
number is greater than or equal to 100
```
\ No newline at end of file
......@@ -4,7 +4,7 @@
当我们需要检查多个条件时,使用`unless-elsif-else`语句。在这个声明中,我们只有一个`unless`和一个`else`,但我们可以有多个`elsif`。这是它的样子:
```
```perl
unless(condition_1){
#These statements would execute if
#condition_1 is false
......@@ -35,7 +35,7 @@ else{
#### 例
```
```perl
#!/usr/local/bin/perl
printf "Enter any number:";
......@@ -53,7 +53,7 @@ else {
**输出:**
```
```perl
Enter any number:101
Number is not 100
```
\ No newline at end of file
......@@ -4,7 +4,7 @@
在大多数情况下,您已经在系统上安装了它,因为多个操作系统已预安装它。要检查系统上是否有它,可以转到命令提示符(mac 中的终端)并输入不带引号的`perl -v`。如果你的系统上有它,那么你应该看到这样的消息:
```
```perl
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)
......@@ -32,7 +32,7 @@ Internet, point your browser at http://www.perl.org/, the Perl Home Page.
转到此链接: [https://www.perl.org/get.html](https://www.perl.org/get.html) 点击 Linux / Unix 选项卡。您应该看到类似“下载最新的稳定源(5.24.1)”,点击它并下载压缩文件,在撰写本文时,5.24.1 是最新的稳定版本,您可能会发现它不同,不要担心只需下载文件。将它保存在您的主目录中,该目录应该是您当前的工作目录,使用以下 shell 命令安装 perl:
```
```perl
tar -xzf perl-5.24.1.tar.gz
cd perl-5.24.1
$./Configure -de
......@@ -43,7 +43,7 @@ $make install
注意:您可能需要根据已下载的源文件更改上述 shell 命令中的版本,例如:如果您已下载版本 5.10.1,那么命令集将是:
```
```perl
tar -xzf perl-5.10.1.tar.gz
cd perl-5.10.1
./Configure -de
......@@ -58,13 +58,13 @@ make install
首先更改终端中已下载压缩文件的目录。例如如果您已下载文件在 Downloads 目录中,则通过键入以下命令更改目录:
```
```perl
cd ~/Downloads
```
进入压缩文件的目录后,运行以下命令:
```
```perl
tar -xzf perl-5.24.1.tar.gz
cd perl-5.24.1
./Configure -de
......
......@@ -7,7 +7,7 @@
然而,三个新的关键字:`given``when``default`在 Perl 5 中引入,提供类似于`switch case`的功能。这是它的样子:
```
```perl
given (argument) {
when (condition) { statement(s); }
when (condition) { statement(s); }
......
......@@ -6,7 +6,7 @@
#### 语法:
```
```perl
given (argument) {
when (condition) { statement(s); }
when (condition) { statement(s); }
......@@ -20,7 +20,7 @@ given (argument) {
#### 例:
```
```perl
#!/usr/local/bin/perl
use v5.10;
no warnings 'experimental';
......@@ -42,7 +42,7 @@ given($num){
**输出:**
```
```perl
Enter any number:10
number is equal to 10
```
\ No newline at end of file
......@@ -6,7 +6,7 @@
#### `for`循环的语法:
```
```perl
for(initialization; condition ; increment/decrement)
{
statement(s);
......@@ -26,7 +26,7 @@ for(initialization; condition ; increment/decrement)
#### 例:
```
```perl
#!/usr/local/bin/perl
for( $num = 100; $num < 105; $num = $num + 1 ){
......@@ -36,7 +36,7 @@ for( $num = 100; $num < 105; $num = $num + 1 ){
**输出:**
```
```perl
100
101
102
......
......@@ -6,7 +6,7 @@
#### `while`循环的语法:
```
```perl
while(condition)
{
statement(s);
......@@ -21,7 +21,7 @@ while(condition)
#### 例
```
```perl
#!/usr/local/bin/perl
$num = 100;
......@@ -34,7 +34,7 @@ while( $num > 95 ){
**输出:**
```
```perl
100
99
98
......
......@@ -6,7 +6,7 @@
#### `do-while`循环的语法:
```
```perl
do
{
statement(s);
......@@ -19,7 +19,7 @@ do
#### 例
```
```perl
#!/usr/local/bin/perl
$num = 90;
......@@ -31,7 +31,7 @@ do{
**输出:**
```
```perl
91
92
93
......
......@@ -6,7 +6,7 @@
#### `foreach`循环的语法:
```
```perl
foreach var (list) {
statement(s);
}
......@@ -18,7 +18,7 @@ foreach var (list) {
#### 例
```
```perl
#!/usr/local/bin/perl
@friends = ("Ajeet", "Tom", "Steve", "Lisa", "Kev");
......@@ -30,7 +30,7 @@ foreach $str (@friends){
**输出:**
```
```perl
Ajeet
Tom
Steve
......
......@@ -6,7 +6,7 @@
#### `until`循环的语法:
```
```perl
until(condition)
{
statement(s);
......@@ -19,7 +19,7 @@ until(condition)
#### 例
```
```perl
#!/usr/local/bin/perl
$num = 10;
......@@ -32,7 +32,7 @@ until( $num >15 ){
**输出:**
```
```perl
10
11
12
......
......@@ -16,7 +16,7 @@ Perl 允许您定义自己的函数,称为**子程序**。它们用于代码
您需要做的第一件事是创建一个子程序。`sub`关键字用于定义 Perl 程序中的子程序。您可以选择任何有意义的子程序名称。
```
```perl
sub subroutine_name
{
statement(s);
......@@ -28,7 +28,7 @@ sub subroutine_name
通过使用前缀为`&`的子程序名称来调用子程序。您也可以在调用子程序时传递参数。
```
```perl
&subroutine_name; #calling without parameter
&subroutine_name(10);
```
......@@ -37,7 +37,7 @@ sub subroutine_name
让我们举一个简单的例子来理解这个:
```
```perl
#!/usr/bin/perl
my $msg;
......@@ -60,7 +60,7 @@ sub show_message {
**输出:**
```
```perl
Please enter something: Welcome to beginnersbook
You entered: Welcome to beginnersbook
```
......@@ -69,7 +69,7 @@ You entered: Welcome to beginnersbook
在上面的例子中,我们在调用子程序时没有传递任何参数,但是我们可以在调用子程序时传递各种参数。所有参数(通常称为参数)都存储在特殊数组(`@_`)中。让我们看看下面的例子来理解这个:
```
```perl
#!/usr/bin/perl
# defining subroutine
......@@ -83,13 +83,13 @@ sub printparams {
**输出:**
```
```perl
This is my blog
```
在上面的例子中,我们使用特殊数组(`@_`)打印了所有参数。但是,如果需要,可以使用`$_[n]`显示所选参数,其中`n`是参数的索引,例如`$_[0]`将引用第一个参数,`$_[1]`将引用第二个参数。让我们举一个例子:
```
```perl
#!/usr/bin/perl
# defining subroutine
......@@ -104,7 +104,7 @@ sub printparams {
**输出:**
```
```perl
First Parameter: This
Fourth Parameter: blog
```
......@@ -113,7 +113,7 @@ Fourth Parameter: blog
在上面的例子中,我们在调用子程序时传递了一些字符串参数。我们也可以将数组传递给子程序。这是一个例子:
```
```perl
#!/usr/bin/perl
# defining subroutine
......@@ -134,7 +134,7 @@ $num = 100;
**输出:**
```
```perl
First Parameter: This
Third Parameter: text
Fourth Parameter: Welcome
......@@ -143,7 +143,7 @@ Sixth Parameter: 100
### 将哈希传递给子程序
```
```perl
#!/usr/bin/perl
sub DisplayMyHash{
......@@ -162,7 +162,7 @@ DisplayMyHash(%hash);
**输出:**
```
```perl
Key is: Item1 and value is: Orange
Key is: Item2 and value is: Apple
Key is: Item3 and value is: Banana
......
......@@ -8,7 +8,7 @@
由于字符串是标量,它们的名称以`$`开头。**例如:**
```
```perl
my $website = "BeginnersBook";
my $editor = "Chaitanya";
print "$editor works for $website";
......@@ -16,7 +16,7 @@ print "$editor works for $website";
输出:
```
```perl
Chaitanya works for BeginnersBook
```
......@@ -26,7 +26,7 @@ Chaitanya works for BeginnersBook
引号不是字符串的一部分,它们只是指定字符串的开头和结尾。您可能认为**单引号和双引号**都有相同的用途,但事实并非如此。它们用于不同的 2 例。要理解这两者之间的区别,让我们看一下下面的例子。
```
```perl
#!/usr/bin/perl
print "Welcome to\t Beginnersbook.com\n";
......@@ -43,7 +43,7 @@ Welcome to\t Beginnersbook.com\n
**例 2:单引号和双引号:**
```
```perl
#!/usr/bin/perl
$website = "Beginnersbook";
......@@ -53,7 +53,7 @@ print 'Website Name: $website\n';
**输出:**
```
```perl
Website Name: Beginnersbook
Website Name: $website\n
```
......@@ -65,7 +65,7 @@ Website Name: $website\n
您可能正在考虑避免在 perl 程序中使用单引号,但在某些情况下您可能希望使用单引号。
例如如果你想在变量中存储**电子邮件地址**,那么双引号会引发错误,在这种情况下你需要使用单引号。对于例如
```
```perl
$email = "[email protected]"; # would throw an error
$email = '[email protected]'; # would work fine.
......@@ -85,7 +85,7 @@ $email = '[email protected]'; # would work fine.
相信我,你会在 perl 中开发应用时经常使用它。假设您要打印包含双引号的字符串。对于例如如果我想打印:`Hello This is "My blog"`然后我需要使用以下逻辑:
```
```perl
#!/usr/bin/perl
$msg = "Hello This is \"My blog\"";
......@@ -94,7 +94,7 @@ print "$msg\n";
**输出:**
```
```perl
Hello This is "My blog"
```
......@@ -106,7 +106,7 @@ Hello This is "My blog"
要连接字符串,请使用点(`.`)运算符。在下面的例子中,我们连接三个字符串,`$str1`,空格和`$str2`
```
```perl
$str1 = "Cherry";
$str2 = "Coco";
$mystr = $str1 . " " . $str2;
......@@ -115,7 +115,7 @@ print "$mystr\n";
输出:
```
```perl
Cherry Coco
```
......@@ -123,7 +123,7 @@ Cherry Coco
要重复具有指定次数的字符串,请使用字符`x`后跟数字。在下面的示例中,我们使用了字符`x`之后的数字 4,这就是字符串在输出中重复四次的原因。
```
```perl
$str = "Cherry";
$mystr = $str x 4;
print "$mystr\n";
......@@ -131,7 +131,7 @@ print "$mystr\n";
输出:
```
```perl
CherryCherryCherryCherry
```
......@@ -139,7 +139,7 @@ CherryCherryCherryCherry
`substr()`函数用于从给定字符串中获取子字符串。
```
```perl
use strict;
use warnings;
# This is our original string
......@@ -155,7 +155,7 @@ print "My Second substring is: $substr2\n";
输出:
```
```perl
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
......@@ -163,7 +163,7 @@ My Second substring is: who you are
该相同功能可用于用新内容替换字符串的一部分。让我们举个例子来理解这个:
```
```perl
use strict;
use warnings;
# This is our original string
......@@ -175,7 +175,7 @@ print "My updated String is: $str\n"
输出:
```
```perl
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
```
......@@ -184,7 +184,7 @@ My updated String is: I know you are the killer, I will find you
要在 Perl 中查找字符串的长度,请使用`length()`函数。此函数计算字符串中的字符数(包括空格)并返回它。
```
```perl
use strict;
use warnings;
my $str = "I know who you are, I will find you";
......@@ -193,7 +193,7 @@ print "length of my string is:", length($str);
输出:
```
```perl
length of my string is:35
```
......@@ -201,7 +201,7 @@ length of my string is:35
为了比较两个字符串,我们使用 Perl 中的[`eq`运算符](https://beginnersbook.com/2017/02/perl-operators-complete-guide/)
```
```perl
use strict;
use warnings;
my $str = "hello";
......@@ -213,6 +213,6 @@ if ($str eq $str2){
输出:
```
```perl
str and str2 are same
```
\ No newline at end of file
......@@ -8,7 +8,7 @@
这是一个打印`Hello World`的简单 perl 程序!屏幕上。
```
```perl
#!/usr/bin/perl
#this is a comment
print "Hello World!!";
......@@ -30,7 +30,7 @@ print "Hello World!!";
要在 Windows 上运行该程序,请打开命令提示符(cmd)并编写以下命令
```
```perl
perl c:\perl\bin\perl.exe firstprogram.pl
```
......@@ -40,7 +40,7 @@ perl c:\perl\bin\perl.exe firstprogram.pl
在 Linux,Unix 或 Mac OS 上运行程序。在终端上运行以下命令。
```
```perl
chmod +x firstprogram.pl
./firstprogram
```
......@@ -49,14 +49,14 @@ chmod +x firstprogram.pl
在某些情况下,我们要指定 Perl 的版本以使我们的脚本正常工作。例如,在 perl 5.10 或更高版本中,您可以使用`say`代替`print`。这是 **Perl 5.10** 中引入的新功能。
```
```perl
#!/usr/bin/perl
say "Hello World!!";
```
但是,如果您使用的是 5.10 之前的 Perl 版本,则无法使用。要解决此问题,您可以像这样重写上述程序:
```
```perl
#!/usr/bin/perl
use 5.010;
say "Hello World!!";
......
......@@ -10,7 +10,7 @@
在单引号的情况下,不需要使用转义序列,因为插值不会出现在单引号字符串中。
```
```perl
use strict;
use warnings;
......@@ -25,7 +25,7 @@ print "$email2\n";
输出:
```
```perl
[email protected]
[email protected]
```
......@@ -34,7 +34,7 @@ print "$email2\n";
我们已经知道美元符号会插入变量的值。如果你想转义`$`符号并避免插值,请使用我们上面做的相同技巧 - 用反斜杠作为前缀。
```
```perl
use strict;
use warnings;
my $name = 'Negan';
......@@ -49,7 +49,7 @@ print "\$msg: $msg\n";
输出:
```
```perl
$name: Negan
$msg: I am Negan
```
......@@ -58,7 +58,7 @@ $msg: I am Negan
在上面的例子中,我们使用反斜杠来转义特殊字符$和@。可能存在您希望在输出中显示反斜杠的情况。为此,你想要逃避反斜杠。让我们举个例子来理解这个:
```
```perl
use strict;
use warnings;
my $say = 'I do like to use backslash \\';
......@@ -67,7 +67,7 @@ print "$say\n";
输出:
```
```perl
I do like to use backslash \
```
......@@ -77,7 +77,7 @@ I do like to use backslash \
我们知道双引号内的文本在 Perl 中被视为字符串。让我们说我们想在 Perl 中显示一个字符串,字符串本身有一个双引号。我们将使用相同的方法,使用`\`来转义引号。
```
```perl
use strict;
use warnings;
my $say = "I like to watch \"The Walking Dead\"";
......@@ -86,7 +86,7 @@ print "$say\n";
输出:
```
```perl
I like to watch "The Walking Dead"
```
......@@ -94,7 +94,7 @@ I like to watch "The Walking Dead"
我们可以用双`q`运算符替换用于包含字符串的双引号。这样做的好处是我们不必担心使用双引号(`"`)和括号的转义序列。
```
```perl
use strict;
use warnings;
my $name = 'Chaitanya';
......@@ -103,7 +103,7 @@ print qq(My name is "$name" and I like brackets ()\n);
输出:
```
```perl
My name is "Chaitanya" and I like brackets ()
```
......@@ -115,7 +115,7 @@ My name is "Chaitanya" and I like brackets ()
让我们采用上面在双`q`运算符中看到的相同示例。
```
```perl
use strict;
use warnings;
my $name = 'Chaitanya';
......@@ -124,6 +124,6 @@ print q(My name is "$name" and I like brackets ()\n);
输出:
```
```perl
My name is "$name" and I like brackets ()\n
```
\ No newline at end of file
......@@ -14,7 +14,7 @@ Perl 文件可以使用`.pl`或`.PL`扩展名保存。
单引号和双引号在 Perl 程序中表现不同。要了解差异,让我们看看下面的代码:
```
```perl
#!/usr/bin/perl
print "Welcome to\t Beginnersbook.com\n";
......@@ -23,7 +23,7 @@ print 'Welcome to\t Beginnersbook.com\n';
这将产生以下输出:
```
```perl
Welcome to Beginnersbook.com
Welcome to\t Beginnersbook.com\n
```
......@@ -32,7 +32,7 @@ Welcome to\t Beginnersbook.com\n
另一个例子:
```
```perl
#!/usr/bin/perl
$website = "Beginnersbook";
......@@ -42,7 +42,7 @@ print 'Website Name: $website\n';
**输出:**
```
```perl
Website Name: Beginnersbook
Website Name: $website\n
```
......@@ -52,7 +52,7 @@ Website Name: $website\n
您可能正在考虑避免在 perl 程序中使用单引号,但在某些情况下您可能希望使用单引号。
例如如果要将电子邮件地址存储在变量中,那么双引号会引发错误,在这种情况下需要使用单引号。例如
```
```perl
$ email = "xyz@gmail.com"; # 会抛出错误
$ email = 'xyz@gmail.com'; # 会工作得很好
```
......@@ -69,7 +69,7 @@ $ email = 'xyz@gmail.com'; # 会工作得很好
相信我,你会在 perl 中开发应用时经常使用它。假设您要打印包含双引号的字符串。对于例如如果我想打印:`Hello This is "My blog"`然后我需要使用以下逻辑:
```
```perl
#!/usr/bin/perl
$msg = "Hello This is \"My blog\"";
......@@ -78,6 +78,6 @@ print "$msg\n";
**输出:**
```
```perl
Hello This is "My blog"
```
\ No newline at end of file
......@@ -6,7 +6,7 @@ Perl 有三种数据类型:**标量**,**标量数组**和**哈希**(也称
对于例如在下面的代码中,我将一个整数和一个字符串分配给两个不同的变量,而不指定任何类型。解释器将选择`age`作为整数类型,并根据分配给它们的数据将其命名为字符串类型。
```
```perl
#!/usr/bin/perl
$age=29;
$name="Chaitanya";
......@@ -18,7 +18,7 @@ $name="Chaitanya";
例如:
```
```perl
$num1=51;
$str1="beginnersbook";
$num2=2.9;
......@@ -29,7 +29,7 @@ $str2="hello";
它们是有序的标量列表,前缀为`@`符号。索引从 0 开始,这意味着访问数组的第一个元素,我们需要使用索引值为 0(零)。例如。
```
```perl
#!/usr/bin/perl
@pincode = (301019, 282005, 101010);
print "\$pincode[0] = $pincode[0]\n";
......@@ -37,7 +37,7 @@ print "\$pincode[0] = $pincode[0]\n";
**输出:**
```
```perl
pincode[0] = 301019
```
......@@ -47,7 +47,7 @@ pincode[0] = 301019
它们是无序的键值对组。它们以百分号(`%`)为前缀。
```
```perl
#!/usr/bin/perl
%ages = ('Chaitanya', 29, 'Ajeet', 28, 'Tom', 40);
......
......@@ -8,7 +8,7 @@ perl 中有三种类型的变量:标量,标量和散列数组。让我们在
[标量](https://beginnersbook.com/2017/05/scalars-in-perl/)是单一数据单元。标量可以是整数,浮点数,字符串等。标量变量以`$`符号为前缀。让我们看看下面的 perl 脚本,其中我们有三个标量变量。
```
```perl
#!/usr/bin/perl
# Integer
......@@ -27,7 +27,7 @@ print "Height: $height\n";
**输出:**
```
```perl
Name: Chaitanya Singh
Age: 29
Height: 180.88
......@@ -37,7 +37,7 @@ Height: 180.88
[数组](https://beginnersbook.com/2017/05/perl-lists-and-arrays/)是标量的有序列表,数组变量的前缀为`@`符号,如下例所示:
```
```perl
#!/usr/bin/perl
@friends = ("Ajeet", "Leo", "Rahul", "Dhruv");
......@@ -50,7 +50,7 @@ print "\$friends[3] = $friends[3]\n";
**输出:**
```
```perl
$friends[0] = Ajeet
$friends[1] = Leo
$friends[2] = Rahul
......@@ -61,7 +61,7 @@ $friends[3] = Dhruv
[哈希](https://beginnersbook.com/2017/05/hashes-in-perl/)是一组键值对。哈希变量以`%`符号为前缀。让我们看看下面的例子:
```
```perl
#!/usr/bin/perl
%age = ('Chaitanya Singh', 29, 'Ajeet', 28, 'Lisa', 25);
......@@ -73,7 +73,7 @@ print "\$age{'Ajeet'}: $age{'Ajeet'}\n";
**输出:**
```
```perl
$age{'Lisa'}: 25
$age{'Chaitanya Singh'}: 29
$age{'Ajeet'}: 28
......
......@@ -10,7 +10,7 @@
这些变量可以在任何代码块中使用,它可以在任何控制结构块中,如`if``if-else`等,或任何循环块,如`for``while``do-while`等或任何子例程的块,它甚至可以出现在匿名区块中。例如:
```
```perl
#!/usr/bin/perl
use strict;
use warnings;
......@@ -23,7 +23,7 @@ if (1<2)
我知道这个例子没有任何意义,但我的观点是向您展示局部变量在程序中的外观。在上面的程序变量中,`$age``if`中被声明,因此这个变量只对这个块是本地的。如果我们尝试在`if`体外访问此变量,我们会收到错误。让我们看看这个在行动,运行以下程序。
```
```perl
#!/usr/bin/perl
use strict;
use warnings;
......@@ -37,7 +37,7 @@ print "$age\n";
**输出:**
```
```perl
Global symbol "$age" requires explicit package name at demo.pl line 9.
Execution of demo.pl aborted due to compilation errors.
```
......@@ -50,7 +50,7 @@ Execution of demo.pl aborted due to compilation errors.
3)在整个程序中可以访问在编译指示之后的程序开头使用`my`关键字声明的变量。例如,程序中可以访问此程序中的变量`$age`
```
```perl
#!/usr/bin/perl
use strict;
use warnings;
......@@ -64,7 +64,7 @@ print "$age\n";
**输出:**
```
```perl
29
29
```
......@@ -75,7 +75,7 @@ print "$age\n";
1)使用`my`关键字声明多个变量时,必须使用括号,否则只声明一个局部变量。例如,
```
```perl
my $num1, $num2; # This would not declare $num2.
my ($num1, $num2); # This is the correct way. It declares both
```
......@@ -84,7 +84,7 @@ my ($num1, $num2); # This is the correct way. It declares both
例如,此代码声明本地数组`@friends`
```
```perl
my @friends;
```
......@@ -92,7 +92,7 @@ my @friends;
这不需要任何介绍,因为我们在我们的程序中使用它们,因为我们启动了 perl 教程。在没有声明的情况下直接使用的所有变量(使用`my`关键字)都可以从程序的每个部分访问。例如,
```
```perl
#!/usr/bin/perl
use warnings;
if (1<2)
......@@ -105,7 +105,7 @@ print "$age\n"; #accessible outside the block
**输出:**
```
```perl
29
29
```
......
......@@ -8,7 +8,7 @@ Perl 中的标量数据被认为是单数,它可以是数字或字符串。我
引号不是字符串的一部分,它们只是指定字符串的开头和结尾。在 Perl 中,单引号和双引号的行为不同。要了解差异,让我们看看下面的代码:
```
```perl
#!/usr/bin/perl
print "Welcome to\t Beginnersbook.com\n";
......@@ -23,7 +23,7 @@ Welcome to\t Beginnersbook.com\n
**另一个例子:**
```
```perl
#!/usr/bin/perl
$website = "Beginnersbook";
......@@ -33,7 +33,7 @@ print 'Website Name: $website\n';
**输出:**
```
```perl
Website Name: Beginnersbook
Website Name: $website\n
```
......@@ -45,7 +45,7 @@ Website Name: $website\n
您可能正在考虑避免在 perl 程序中使用单引号,但在某些情况下您可能希望使用单引号。
例如如果要将电子邮件地址存储在变量中,那么双引号会引发错误,在这种情况下需要使用单引号。对于例如
```
```perl
$email = "xyz@gmail.com"; # would throw an error
$email = 'xyz@gmail.com'; # would work fine.
......@@ -63,7 +63,7 @@ $email = 'xyz@gmail.com'; # would work fine.
相信我,你会在 perl 中开发应用时经常使用它。假设您要打印包含双引号的字符串。对于例如如果我想打印`Hello This is "My blog"`然后我需要使用以下逻辑:
```
```perl
#!/usr/bin/perl
$msg = "Hello This is \"My blog\"";
......@@ -72,7 +72,7 @@ print "$msg\n";
**输出:**
```
```perl
Hello This is "My blog"
```
......@@ -82,7 +82,7 @@ Hello This is "My blog"
您可以使用点(`.`)运算符连接字符串。例如:
```
```perl
"Hello"."Chaitanya" # Equivalent to "HelloChaitanya"
"Hello"."Chaitanya"."\n" # Equivalent to "HelloChaitanya\n"
```
......@@ -97,7 +97,7 @@ Hello This is "My blog"
例如:
```
```perl
"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"
......@@ -109,7 +109,7 @@ Hello This is "My blog"
例如:
```
```perl
8 x 3 # Same as "8" x 3 or "888"
```
......
......@@ -4,7 +4,7 @@
几乎每个 perl 脚本中都可以找到以下几行。
```
```perl
use strict;
use warnings;
```
......@@ -18,7 +18,7 @@ use warnings;
`use strict`语句称为`pragma`,它可以放在脚本的开头,如下所示:
```
```perl
#!/usr/local/bin/perl
use strict;
```
......@@ -33,7 +33,7 @@ use strict;
如果使用`use strict`但不声明变量。
```
```perl
#!/usr/local/bin/perl
use strict;
$s = "Hello!\n";
......@@ -42,7 +42,7 @@ print $s;
它会抛出这个错误:
```
```perl
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.
......@@ -50,7 +50,7 @@ Execution of st.pl aborted due to compilation errors.
要避免错误,您必须使用`my`关键字声明变量。
```
```perl
#!/usr/local/bin/perl
use strict;
my $s = "Hello!\n";
......@@ -59,7 +59,7 @@ print $s;
**输出:**
```
```perl
Hello!
```
......@@ -71,7 +71,7 @@ Hello!
这是另一个`pragma`,它们一起使用如下:
```
```perl
#!/usr/local/bin/perl
use strict;
use warnings;
......@@ -79,7 +79,7 @@ use warnings;
**注意**`use warnings pragm`在 Perl 5.6 中引入,所以如果你使用的是 Perl 5.6 或更高版本,就可以了。如果您使用的是旧版本,可以打开如下警告:将`-w`放在`shebang`行。
```
```perl
#!/usr/local/bin/perl -w
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册