免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 6724 | 回复: 22
打印 上一主题 下一主题

2个半小时学会Perl [摘要] [复制链接]

论坛徽章:
30
水瓶座
日期:2014-08-22 21:06:3415-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:4715-16赛季CBA联赛之天津
日期:2019-01-11 01:11:44
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-03 23:52 |只看该作者 |倒序浏览
本帖最后由 shijiang1130 于 2013-03-07 23:04 编辑
  1. 学习Perl用约2小时30分钟

  2. --- 考虑到自己的水平问题,写翻译实在是对不起大众,就改成摘要了,省略了面向对象那一章和其它自认为是翻不出来的内容,要是你有兴趣还是去看原版英文吧,http://qntm.org/files/perl/perl.html。 --

  3. 在两个小时30分钟内了解 Perl

  4. 作者 Sam Hughes

  5. Perl语言是一门高级解释型动态语言,它的许多数据类型是运行时才决定的,并且经常和PHP和Python相提并论。Perl 从古老的Shell脚本语言中借鉴了许多语法特性,因为被过度使用的各种奇怪符号而声名狼藉,而且大部分代码借助Google的搜索功能都不能明白。Perl语言因其来自Shell的语言特性使之成为一门好用的胶水语言: 将其他语言和各种脚本连接在一起共同工作。  

  6. Perl 语言非常适合处理文本,并生成更多的文本。Perl语言应用广泛,流行,可移植性极佳,而且有良好的社区支持。Perl 语言的设计哲学是:"每个问题都有许多解决方式"(TMTOWTDI)( 与之相反,Python 的设计哲学是:每个问题应该只有一种,而且只有一种明确的,最好的解决方式)。

  7. Perl 有令人沮丧的地方,但同时也有许多奇妙的特性。从这一点来看,它同其他任何一种曾经有过的编程语言一样。

  8. 这篇文章只是提供一些知识,并不是什么宣传广告,目标读者只是针对像我这样的人: 厌恶 http://perl.org 上面学术性的文档,那些东西只会长篇累牍的讲述一些永远用不到的边缘问题。我只想通过一些通用规则和实例快速了解那些从Larry Wall的角度永远都不关心的基础编程问题,并学些能帮我找到一份工作的基础知识。

  9. 这篇文档的目的是用短的不能再短的形式来讲:


  10. 研究初探

  11. 以下声明针对整个文档:“这并不是绝对的,实际情况要复杂的多”。你如果发现一个严重错误,请告诉我。但我保留对孩子们说错话的权利。

  12. 在本文档中,我在实例中打印状态,输出数据,但没有明确的追加换行符。这样做是为了避免我发狂,让我能集中精力在实例中字符串的输出,我相信这是更重要的。在许多例子中,实际的输出可能是 "alotofwordsallsmusheduptogetherononeline". 请不要在意。

  13. Hello world

  14. 一个 Perl 脚本就是一个后缀为 .pl 的文本文件。

  15. 这就是 helloworld.pl 的全文:

  16.   use strict;
  17.   use warnings;

  18.   print "Hello world";

  19. Perl 脚本被 Perl 的解释器来解释运行:

  20.   perl helloworld.pl [arg0 [arg1 [arg2 ...]]]

  21. 说实话,Perl 的语法非常宽容,他可以替你预测一些模糊的不知所云的代码的行为,我实在不想讲这些东西,因为你们应当竭力避免这么做。

  22. 避免这么做的方法就是将 'use strict; use warnings' 放置在每一个脚本或模块的最前面。'use foo' 这样的语句是编译提示,编译提示是给 Perl.exe 的信号,在程序运行前,对解释器初始化语法验证规则施加影响。在代码运行的时候,解释器将忽略这些东西。

  23. '#' 符号是一个注释的开始。注释的结束是到行的末尾。Perl 没有针对块的注释语法。

  24. 变量

  25. Perl 的变量有三种类型:标量,数组和散列。每种类型都有自己的前置符号:$, @ 和 % 分别代表这三种类型。变量使用 my 来声明,有效范围在闭合块范围内或到文件的末尾。

  26. 标量变量

  27. 一个标量变量可以包含:

  28. undef (相当于 Python 中的 None, PHP 中的 null)

  29. 一个数字( Perl 并不区分一个整型数字和浮点数字)

  30. 一个字符串

  31. 一个指向别的变量的引用

  32.   my $undef = undef;
  33.   print $undef; # prints the empty string "" and raises a warning

  34.   # implicit undef:
  35.   my $undef2;
  36.   print $undef2; # prints "" and raises exactly the same warning
  37.   my $num = 4040.5;
  38.   print $num; # "4040.5"
  39.   my $string = "world";
  40.   print $string; # "world"

  41. (稍后介绍引用)

  42. 字符串使用 '.' 进行连接(和PHP一样)

  43.   print "Hello ".$string; # "Hello world"

  44. 布尔值

  45. Perl 没有布尔数据类型。只有如下几种值才能在 if 判断语句中返回 'false' :
  46. undef

  47. 数字 0

  48. 字符串 ""

  49. 字符串 "0"

  50. Perl 文档中经常提到函数在某些情况下会返回 'true' 或 'false'. 实际上,函数通常用 'return 1' 来返回真,用返回空字符串 '' 来表示返回假。

  51. 弱类型

  52. 它是不可能确定一个标量包含一个“数字”或“字符串”。更准确地说,它不应该有必要这样做。一个标量是否像一个数字或者字符串取决于操作者的使用它。当作为一个字符串、一个标量会表现得像一个字符串。当作为一个数字,一个标量会表现得像一个号码(增加一个警告如果这是不可能的):

  53. my $str1 = "4G";
  54. my $str2 = "4H";
  55. print $str1 . $str2; # "4G4H"
  56. print $str1 + $str2; # "8" with two warnings
  57. print $str1 eq $str2; # "" (empty string, i.e. false)
  58. print $str1 == $str2; # "1" with two warnings
  59. # The classic error

  60. #经典的错误

  61. print "yes" == "no"; # "1" with two warnings; both values evaluate to 0 when used as numbers

  62. 两个值用作数字评估为0

  63. 这个教训是总是使用正确的操作符来比较标量数字和标量字符串比较:
  64. #数值运算符:<、>、< =、> =、= =、!=、< = >,+,*
  65. #字符串运算符: lt, gt, le, ge, eq, ne, cmp, . , x

  66. 数组变量
  67. 一个数组变量是一个标量的列表索引从0开始的整数。在Python中这被称为一个列表,在PHP这被称为数组。一个数组声明使用parenthesised标量的列表:

  68. my @array = (
  69. "print",
  70. "these",
  71. "strings",
  72. "out",
  73. "for",
  74. "me", # trailing comma is okay
  75. );

  76. 你必须使用美元符号来访问一个值从一个数组,因为要取得的值不是一个数组但标量:

  77. print $array[0]; # "print"
  78. print $array[1]; # "these"
  79. print $array[2]; # "strings"
  80. print $array[3]; # "out"
  81. print $array[4]; # "for"
  82. print $array[5]; # "me"
  83. print $array[6]; # returns undef, prints "" and raises a warning


  84. 您可以使用负索引检索条目从结局开始和向后的工作:

  85. print $array[-1]; # "me"
  86. print $array[-2]; # "for"
  87. print $array[-3]; # "out"
  88. print $array[-4]; # "strings"
  89. print $array[-5]; # "these"
  90. print $array[-6]; # "print"
  91. print $array[-7]; # returns undef, prints "" and raises a warning

  92. 标量$ var和包含一个标量条目$ var[0] 的@var数组。有可能被读者困惑,所以避免这个。

  93. 获取一个数组的长度:
  94. print "This array has ". (scalar @array). "elements"; # "This array has 6 elements"

  95. 最后的下标是5”
  96. print "The last populated index is ". $#array; # "The last populated index is 5"

  97. The arguments with which the original Perl script was invoked are stored in the built-in array variable @ARGV.
  98. Perl脚本原来的参数调用存储在内置数组@ argv里面。
  99. 变量可以插入到字符串:
  100. print "Hello $string"; # "Hello world"
  101. print "@array"; # "print these strings out for me"

  102. 谨慎。有一天你会把某人的电子邮件地址在一个字符串,“[email]jeff@gmail.com[/email]”。这将导致Perl寻找一个数组变量称为@gmail来插入到字符串,并没有发现它,这会导致运行时错误。插值可以避免在两个方面:通过反斜杠转义特殊符号,或者用单引号代替双引号。
  103. print "Hello \$string"; # "Hello $string"
  104. print 'Hello $string'; # "Hello $string"
  105. print "\@array"; # "@array"
  106. print '@array'; # "@array"

  107. 散列变量
  108. 一个散列变量是一个字符串索引列表的标量。在Python中这被称为一个字典,在PHP中,这被称为一个数组。
  109. my %scientists = (
  110. "Newton" => "Isaac",
  111. "Einstein" => "Albert",
  112. "Darwin" => "Charles",
  113. );

  114. 注意这是一个类似声明数组声明。事实上,双箭头符号= >被称为“胖逗号“,因为它只是一个逗号分隔的同义词。一个散列声明使用列表与偶数个元素,偶数编号的元素(0、2、……)都作为字符串。
  115. 你必须使用美元符号来访问一个值从一个散列,因为要取得的值不是一个散列但标量:
  116. print $scientists{"Newton"}; # "Isaac"
  117. print $scientists{"Einstein"}; # "Albert"
  118. print $scientists{"Darwin"}; # "Charles"
  119. print $scientists{"Dyson"}; # returns undef, prints "" and raises a warning
  120. 注意:$ var和包含一个标量条目$ var {“foo”}的散列 % var与上面的数组包含标量。

  121. 你可以直接转换一个散列与两倍条目的数组,交替键和值(和反向同样是容易的):
  122. my @scientists = %scientists;
  123. 然而,不像一个数组,将返回任意顺序。这个在编程的时候有注意!
  124. 回顾一下,你必须使用方括号来检索一个值从一个数组,但是你必须使用括号来检索一个值从一个散列。方括号实际上是一个数值算子和括号都有效的字符串操作符。

  125. my $data = "orange";
  126. my @data = ("purple");
  127. my %data = ( "0" => "blue");
  128. print $data; # "orange"
  129. print $data[0]; # "purple"
  130. print $data["0"]; # "purple"
  131. print $data{0}; # "blue"
  132. print $data{"0"}; # "blue"

复制代码

评分

参与人数 1可用积分 +10 收起 理由
兰花仙子 + 10 支持楼主

查看全部评分

论坛徽章:
2
技术图书徽章
日期:2013-11-23 01:55:57白羊座
日期:2013-12-08 15:48:16
2 [报告]
发表于 2013-03-04 02:41 |只看该作者
学习Perl用约2小时30分钟

光这几个字, 唉, 我就不想看翻译的了(虽然我的英语很差很差, 差到看不懂).

论坛徽章:
0
3 [报告]
发表于 2013-03-04 08:06 |只看该作者
本帖最后由 Perlvim 于 2013-03-04 08:56 编辑

支持此计划,建议尝试用并行开发思路解决。

将此任务拆分成几个部分,允许别人认领,然后合并结果。

太多在名字上引人侧目的华而不实的文章,使类似速成,几天,几小时掌握什么的名字,让人厌烦。
同时也让许多精辟短小的好文章被人遗忘。

这篇是带领人入门的好文章,不能说掌握,只能是了解。建议中文名字起一个平实的名字。大家可以各抒已见。

论坛徽章:
30
水瓶座
日期:2014-08-22 21:06:3415-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:4715-16赛季CBA联赛之天津
日期:2019-01-11 01:11:44
4 [报告]
发表于 2013-03-04 23:13 |只看该作者
列表
一个列表在Perl是一种不同的东西再从要么数组或散列。你刚刚看到几个列表:
  1. (
  2. "print",
  3. "these",
  4. "strings",
  5. "out",
  6. "for",
  7. "me",
  8. )
  9. (
  10. "Newton" => "Isaac",
  11. "Einstein" => "Albert",
  12. "Darwin" => "Charles",
  13. )
复制代码
一个列表不是一个变量。列表是一个短暂的价值,可分配给一个数组或散列变量。这就是为什么语法来声明数组和散列变量是相同的。在许多情况下,术语“列表”和“数组”可以互换使用,但有同样很多,列表和数组显示略有不同和极其混乱的行为。

好吧。记住,= >只是在伪装,然后看这个例子:
  1. ("one", 1, "three", 3, "five", 5)
  2. ("one" => 1, "three" => 3, "five" => 5)
复制代码
使用= >暗示这些列表是一个数组声明和其他是一个散列声明。但在他们自己的,他们两个都不是声明的任何东西。他们只是列表。相同的列表。
()
这里甚至没有暗示。这个列表可以用来声明一个空数组或一个空哈希和perl解释器显然没有告诉无论哪种方式。一旦你理解了这个奇怪的方面的Perl,你也会明白为什么以下事实必须真实:列表值不能嵌套。试一试:
  1. my @array = (
  2. "apples",
  3. "bananas",
  4. (
  5. "inner",
  6. "list",
  7. "several",
  8. "entries",
  9. ),
  10. "cherries",
  11. );
复制代码
Perl无法知道是否("inner", "list", "several", "entries")被认为是一种内在的数组或一种内在的散列。因此,Perl假定它既不是和平滑的列表一个长长的名单:
  1. print $array[0]; # "apples"
  2. print $array[1]; # "bananas"
  3. print $array[2]; # "inner"
  4. print $array[3]; # "list"
  5. print $array[4]; # "several"
  6. print $array[5]; # "entries"
  7. print $array[6]; # "cherries"

  8. 相同的是真正的是否使用=>
  9. my %hash = (
  10. "beer" => "good",
  11. "bananas" => (
  12. "green" => "wait",
  13. "yellow" => "eat",
  14. ),
  15. );

  16. #以上提出了一个警告,因为散列被宣布使用7元素列表
  17. print $hash{"beer"}; # "good"
  18. print $hash{"bananas"}; # "green"
  19. print $hash{"wait"}; # "yellow";
  20. print $hash{"eat"}; # undef, so prints "" and raises a warning
复制代码
当然,这确实使它容易连接多个数组在一起:
  1. my @bones = ("humerus", ("jaw", "skull"), "tibia");
  2. my @fingers = ("thumb", "index", "middle", "ring", "little");
  3. my @parts = (@bones, @fingers, ("foot", "toes"), "eyeball", "knuckle");
  4. print @parts;
复制代码

论坛徽章:
30
水瓶座
日期:2014-08-22 21:06:3415-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:4715-16赛季CBA联赛之天津
日期:2019-01-11 01:11:44
5 [报告]
发表于 2013-03-04 23:29 |只看该作者
  1. 上下文
  2. Perl最鲜明的特征是,它的代码是上下文敏感的。每个表达式在Perl是评估无论是在标量上下文语境或列表,根据是否将产生一个标量或列表。许多Perl表达式和内置函数显示截然不同的行为取决于上下文的评估。
  3. 一个标量作业如$scalar =评估它的表达式在标量上下文。在这种情况下,该表达式是"Mendeleev"和返回的值是相同的标量值"Mendeleev":
  4. my $scalar = "Mendeleev";
  5. 一个数组或散列赋值如@array =或%hash =评估其表达上下文列表。一个列表值列表上下文中计算得到的返回列表
  6. my @array = ("Alpha", "Beta", "Gamma", "Pie");
  7. my %hash = ("Alpha" => "Beta", "Gamma" => "Pie");

  8. 到目前为止没有惊喜。
  9. 一个标量表达式列表上下文中计算得到的变成一个单元素列表:
  10. my @array = "Mendeleev"; # same as 'my @array = ("Mendeleev");'

  11. 表达式列表评估在标量上下文返回最终的标量在列表中:
  12. my $scalar = ("Alpha", "Beta", "Gamma", "Pie"); # Value of $scalar is now "Pie"
  13. 一个数组表达式(数组是不同的从一个列表,还记得吗?)评估在标量上下文返回数组的长度:
  14. my @array = ("Alpha", "Beta", "Gamma", "Pie");
  15. my $scalar = @array; # Value of $scalar is now 4
  16. 打印内置函数评估它的所有参数在上下文列表。事实上,打印接受无限的参数列表,并打印每个一个接一个,这意味着它可以用来直接打印数组:
  17. my @array = ("Alpha", "Beta", "Goo");
  18. my $scalar = "-X-";
  19. print @array; # "AlphaBetaGoo";
  20. print $scalar, @array, 98; # "-X-AlphaBetaGoo98";
  21. 你可以迫使任何表达式计算在标量上下文使用标量内置函数。事实上,这就是为什么我们使用标量来检索一个数组的长度。
  22. 你没有语法来返回标量值在标量上下文,也没有返回值在列表上下文。如上所述,Perl是完全有能力为你捏造结果。
复制代码

论坛徽章:
30
水瓶座
日期:2014-08-22 21:06:3415-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:4715-16赛季CBA联赛之天津
日期:2019-01-11 01:11:44
6 [报告]
发表于 2013-03-04 23:44 |只看该作者
  1. 引用和嵌套的数据结构
  2. 以同样的方式列出不能包含列表元素,数组和散列不能包含其他元素数组和散列。他们只能包含标量。看看会发生什么:
  3. my @outer = ("Sun", "Mercury", "Venus", undef, "Mars");
  4. my @inner = ("Earth", "Moon");
  5. $outer[3] = @inner;
  6. print $outer[3]; # "2"
  7. $@outer是一个标量,所以它需要一个标量值。当你试图分配一个数组值像@inner,@inner评估在标量上下文。这是一样的标量@inner分配,这是@inner数组的长度,这是2。
  8. 然而,一个标量变量可能包含一个引用到任何变量,包括一个数组变量或一个散列变量。这是在PERL中创建更复杂的数据结构。
  9. 创建一个引用是使用一个反斜杠。
  10. my $colour = "Indigo";
  11. my $scalarRef = \$colour;
  12. 任何时间你将使用一个变量的名字,您可以只放一些括号,括号内,将一个引用变量来代替。
  13. print $colour; # "Indigo"
  14. print $scalarRef; # e.g. "SCALAR(0x182c180)"
  15. print ${ $scalarRef }; # "Indigo"

  16. 只要结果是不模糊,你可以省略括号:
  17. print $$scalarRef; # "Indigo"
  18. If your reference is a reference to an array or hash variable, you can get data out of it using braces or using the more popular arrow operator, ->:
  19. 如果你的引用是引用一个数组或散列变量,您可以用括号或使用更受欢迎的箭头操作符 - >得到数据
  20. my @colours = ("Red", "Orange", "Yellow", "Green", "Blue");
  21. my $arrayRef = \@colours;
  22. print $colours[0]; # direct array access
  23. print ${ $arrayRef }[0]; # use the reference to get to the array
  24. print $arrayRef->[0]; # exactly the same thing
  25. my %atomicWeights = ("Hydrogen" => 1.008, "Helium" => 4.003, "Manganese" => 54.94);
  26. my $hashRef = \%atomicWeights;
  27. print $atomicWeights{"Helium"}; # direct hash access
  28. print ${ $hashRef }{"Helium"}; # use a reference to get to the hash
  29. print $hashRef->{"Helium"}; # exactly the same thing - this is very common

  30. 声明一个数据结构
  31. 这里有四个例子,但在实践中最后一个是最有益的。
  32. my %owner1 = (
  33. "name" => "Santa Claus",
  34. "DOB" => "1882-12-25",
  35. );
  36. my $owner1Ref = \%owner1;
  37. my %owner2 = (
  38. "name" => "Mickey Mouse",
  39. "DOB" => "1928-11-18",
  40. );
  41. my $owner2Ref = \%owner2;
  42. my @owners = ( $owner1Ref, $owner2Ref );
  43. my $ownersRef = \@owners;
  44. my %account = (
  45. "number" => "12345678",
  46. "opened" => "2000-01-01",
  47. "owners" => $ownersRef,
  48. );

  49. 这显然是不必要的辛苦,因为你可以简化成:
  50. my %owner1 = (
  51. "name" => "Santa Claus",
  52. "DOB" => "1882-12-25",
  53. );
  54. my %owner2 = (
  55. "name" => "Mickey Mouse",
  56. "DOB" => "1928-11-18",
  57. );
  58. my @owners = ( \%owner1, \%owner2 );
  59. my %account = (
  60. "number" => "12345678",
  61. "opened" => "2000-01-01",
  62. "owners" => \@owners,
  63. );
  64. 也可以使用不同的符号声明匿名数组和散列。使用方括号为一个匿名数组和括号为匿名散列。返回的值在每个案例是一个参考的匿名数据结构问题。仔细看:
  65. #括号表示一个匿名散列
  66. my $owner1Ref = {
  67. "name" => "Santa Claus",
  68. "DOB" => "1882-12-25",
  69. };
  70. my $owner2Ref = {
  71. "name" => "Mickey Mouse",
  72. "DOB" => "1928-11-18",
  73. };

  74. #方括号表示一个匿名数组
  75. my $ownersRef = [ $owner1Ref, $owner2Ref ];
  76. my %account = (
  77. "number" => "12345678",
  78. "opened" => "2000-01-01",
  79. "owners" => $ownersRef,
  80. );

  81. 或者, (你应该实际使用复杂数据结构时声明内联):
  82. my %account = (
  83. "number" => "31415926",
  84. "opened" => "3000-01-01",
  85. "owners" => [
  86. {
  87. "name" => "Philip Fry",
  88. "DOB" => "1974-08-06",
  89. },
  90. {
  91. "name" => "Hubert Farnsworth",
  92. "DOB" => "2841-04-09",
  93. },
  94. ],
  95. );
复制代码

论坛徽章:
0
7 [报告]
发表于 2013-03-05 17:04 |只看该作者
本帖最后由 Perlvim 于 2013-03-06 12:55 编辑

大家看看有什么地方翻译的有问题,请随时提出,我会继续翻译直到翻译完成:

=encoding utf8

en: =head1 Learn Perl in about 2 hours 30 minutes
cn: =head1 在两个小时30分钟内了解 Perl

en: =head2 By Sam Hughes
cn: =head2 作者 Sam Hughes

en: Perl is a dynamic, dynamically-typed, high-level, scripting (interpreted) language most comparable with PHP and Python. Perl's syntax owes a lot to ancient shell scripting tools, and it is famed for its overuse of confusing symbols, the majority of which are impossible to Google for. Perl's shell scripting heritage makes it great for writing glue code: scripts which link together other scripts and programs.
cn: Perl语言是一门高级解释型动态语言,它的许多数据类型是运行时才决定的,并且经常和PHP和Python相提并论。Perl 从古老的Shell脚本语言中借鉴了许多语法特性,因为被过度使用的各种奇怪符号而声名狼藉,而且大部分代码借助Google的搜索功能都不能明白。Perl语言因其来自Shell的语言特性使之成为一门好用的胶水语言: 将其他语言和各种脚本连接在一起共同工作。  

en: Perl is ideally suited for processing text data and producing more text data. Perl is widespread, popular, highly portable and well-supported. Perl was designed with the philosophy "There's More Than One Way To Do It" (TMTOWTDI) (contrast with Python, where "there should be one - and preferably only one - obvious way to do it").
cn: Perl 语言非常适合处理文本,并生成更多的文本。Perl语言应用广泛,流行,可移植性极佳,而且有良好的社区支持。Perl 语言的设计哲学是:"每个问题都有许多解决方式"(TMTOWTDI)( 与之相反,Python 的设计哲学是:每个问题应该只有一种,而且只有一种明确的,最好的解决方式)。

en: Perl has horrors, but it also has some great redeeming features. In this respect it is like every other programming language ever created.
cn: Perl 有令人沮丧的地方,但同时也有许多奇妙的特性。从这一点来看,它同其他任何一种曾经有过的编程语言一样。

en: This document is intended to be informative, not evangelical. It is aimed at people who, like me: dislike the official Perl documentation at http://perl.org/ for being intensely technical and giving far too much space to very unusual edge cases learn new programming languages most quickly by "axiom and example" wish Larry Wall would get to the point already know how to program in general terms don't care about Perl beyond what's necessary to get the job done.
cn: 这篇文章只是提供一些知识,并不是什么宣传广告,目标读者只是针对像我这样的人: 厌恶 http://perl.org 上面学术性的文档,那些东西只会长篇累牍的讲述一些永远用不到的边缘问题。我只想通过一些通用规则和实例快速了解那些从Larry Wall的角度永远都不关心的基础编程问题,并学些能帮我找到一份工作的基础知识。

en: This document is intended to be as short as possible, but no shorter.
cn: 这篇文档的目的是用短的不能再短的形式来讲:

en: =head2 Preliminary notes
cn: =head2 研究初探

en: The following can be said of almost every declarative statement in this document: "that's not, strictly speaking, true; the situation is actually a lot more complicated". If you see a serious lie, point it out, but I reserve the right to preserve certain critical lies-to-children.
cn: 以下声明针对整个文档:“这并不是绝对的,实际情况要复杂的多”。你如果发现一个严重错误,请告诉我。但我保留对孩子们说错话的权利。

en: Throughout this document I'm using example print statements to output data but not explicitly appending line breaks. This is done to prevent me from going crazy and to give greater attention to the actual string being printed in each case, which is invariably more important. In many examples, this results in alotofwordsallsmusheduptogetherononeline if the code is run in reality. Try to ignore this.
cn: 在本文档中,我在实例中打印状态,输出数据,但没有明确的追加换行符。这样做是为了避免我发狂,让我能集中精力在实例中字符串的输出,我相信这是更重要的。在许多例子中,实际的输出可能是 "alotofwordsallsmusheduptogetherononeline". 请不要在意。

en: =head2 Hello world
cn: =head2 Hello world

en: A Perl script is a text file with the extension .pl.
cn: 一个 Perl 脚本就是一个后缀为 .pl 的文本文件。

en: Here's the full text of helloworld.pl:
cn: 这就是 helloworld.pl 的全文:

  use strict;
  use warnings;

  print "Hello world";

en: Perl scripts are interpreted by the Perl interpreter, perl or perl.exe:
cn: Perl 脚本被 Perl 的解释器来解释运行:

  perl helloworld.pl [arg0 [arg1 [arg2 ...]]]

en: A few immediate notes. Perl's syntax is highly permissive and it will allow you to do things which result in ambiguous-looking statements with unpredictable behaviour. There's no point in me explaining what these behaviours are, because you want to avoid them.
cn: 说实话,Perl 的语法非常宽容,他可以替你预测一些模糊的不知所云的代码的行为,我实在不想讲这些东西,因为你们应当竭力避免这么做。

en: The way to avoid them is to put use strict; use warnings; at the very top of every Perl script or module that you create. Statements of the form use foo; are pragmas. A pragma is a signal to perl.exe, which takes effect when initial syntactic validation is being performed, before the program starts running. These lines have no effect when the interpreter encounters them at run time.
cn: 避免这么做的方法就是将 'use strict; use warnings' 放置在每一个脚本或模块的最前面。'use foo' 这样的语句是编译提示,编译提示是给 Perl.exe 的信号,在程序运行前,对解释器初始化语法验证规则施加影响。在代码运行的时候,解释器将忽略这些东西。

en: The symbol # begins a comment. A comment lasts until the end of the line. Perl has no block comment syntax.
cn: '#' 符号是一个注释的开始。注释的结束是到行的末尾。Perl 没有针对块的注释语法。

en: =head2 Variables
cn: =head2 变量

en: Perl variables come in three types: scalars, arrays and hashes. Each type has its own sigil: $, @ and % respectively. Variables are declared using my, and remain in scope until the end of the enclosing block or file.
cn: Perl 的变量有三种类型:标量,数组和散列。每种类型都有自己的前置符号:$, @ 和 % 分别代表这三种类型。变量使用 my 来声明,有效范围在闭合块范围内或到文件的末尾。

en: =head3 Scalar variables
cn: 标量变量

en: A scalar variable can contain:
cn: 一个标量变量可以包含:

=over 4

en: =item * undef (corresponds to None in Python, null in PHP)
cn: =item * undef (相当于 Python 中的 None, PHP 中的 null)

en: =item * a number (Perl does not distinguish between an integer and a float)
cn: =item * 一个数字( Perl 并不区分一个整型数字和浮点数字)

en: =item * a string
cn: =item * 一个字符串

en: =item * a reference to any other variable.
cn: =item * 一个指向别的变量的引用

=back

  my $undef = undef;
  print $undef; # prints the empty string "" and raises a warning

  # implicit undef:
  my $undef2;
  print $undef2; # prints "" and raises exactly the same warning
  my $num = 4040.5;
  print $num; # "4040.5"
  my $string = "world";
  print $string; # "world"

en: (References are coming up shortly.)
cn: (稍后介绍引用)

en: String concatenation using the . operator (same as PHP):
cn: 字符串使用 '.' 进行连接(和PHP一样)

  print "Hello ".$string; # "Hello world"

en: =head3 Booleans
cn: =head3 布尔值

en: Perl has no boolean data type. A scalar in an if statement evaluates to boolean "false" if and only if it is one of the following:
cn: Perl 没有布尔数据类型。只有如下几种值才能在 if 判断语句中返回 'false' :

=over 4

en: =item * undef
cn: =item * undef

en: =item * number 0
cn: =item * 数字 0

en: =item * string ""
cn: =item * 字符串 ""

en: =item * string "0"
cn: =item * 字符串 "0"

=back

en: The Perl documentation repeatedly claims that functions return "true" or "false" values in certain situations. In practice, when a function is claimed to return "true" it usually returns 1, and when it is claimed to return false it usually returns the empty string, "".
cn: Perl 文档中经常提到函数在某些情况下会返回 'true' 或 'false'. 实际上,函数通常用 'return 1' 来返回真,用返回空字符串 '' 来表示返回假。

en: =head3 Weak typing
cn: =head3 弱类型

论坛徽章:
30
水瓶座
日期:2014-08-22 21:06:3415-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:4715-16赛季CBA联赛之天津
日期:2019-01-11 01:11:44
8 [报告]
发表于 2013-03-05 23:21 |只看该作者
Perlvim 发表于 2013-03-05 17:04
翻得准确多了。我继续往下啊。

论坛徽章:
30
水瓶座
日期:2014-08-22 21:06:3415-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:4715-16赛季CBA联赛之天津
日期:2019-01-11 01:11:44
9 [报告]
发表于 2013-03-05 23:37 |只看该作者
  1. 获取信息的数据结构
  2. 这里有四个例子,其中最后一个是最有用的:
  3. 1.
  4. my $ownersRef = $account{"owners"};
  5. my @owners = @{ $ownersRef };
  6. my $owner1Ref = $owners[0];
  7. my %owner1 = %{ $owner1Ref };
  8. my $owner2Ref = $owners[1];
  9. my %owner2 = %{ $owner2Ref };
  10. print "Account #", $account{"number"}, "\n";
  11. print "Opened on ", $account{"opened"}, "\n";
  12. print "Joint owners:\n";
  13. print "\t", $owner1{"name"}, " (born ", $owner1{"DOB"}, ")\n";
  14. print "\t", $owner2{"name"}, " (born ", $owner2{"DOB"}, ")\n";

  15. 2.
  16. my @owners = @{ $account{"owners"} };
  17. my %owner1 = %{ $owners[0] };
  18. my %owner2 = %{ $owners[1] };
  19. print "Account #", $account{"number"}, "\n";
  20. print "Opened on ", $account{"opened"}, "\n";
  21. print "Joint owners:\n";
  22. print "\t", $owner1{"name"}, " (born ", $owner1{"DOB"}, ")\n";
  23. print "\t", $owner2{"name"}, " (born ", $owner2{"DOB"}, ")\n";

  24. 3.
  25. my $ownersRef = $account{"owners"};
  26. my $owner1Ref = $ownersRef->[0];
  27. my $owner2Ref = $ownersRef->[1];
  28. print "Account #", $account{"number"}, "\n";
  29. print "Opened on ", $account{"opened"}, "\n";
  30. print "Joint owners:\n";
  31. print "\t", $owner1Ref->{"name"}, " (born ", $owner1Ref->{"DOB"}, ")\n";
  32. print "\t", $owner2Ref->{"name"}, " (born ", $owner2Ref->{"DOB"}, ")\n";

  33. 4.
  34. print "Account #", $account{"number"}, "\n";
  35. print "Opened on ", $account{"opened"}, "\n";
  36. print "Joint owners:\n";
  37. print "\t", $account{"owners"}->[0]->{"name"}, " (born ", $account{"owners"}->[0]->{"DOB"}, ")\n";
  38. print "\t", $account{"owners"}->[1]->{"name"}, " (born ", $account{"owners"}->[1]->{"DOB"}, ")\n";

  39. 数组与数组引用
  40. 这个数组有五个要素:
  41. my @array1 = (1, 2, 3, 4, 5);
  42. print @array1; # "12345"

  43. 这个数组,有一个元素(是一个匿名引):
  44. my @array2 = [1, 2, 3, 4, 5];
  45. print @array2; # e.g. "ARRAY(0x182c180)"

  46. 这个标量是一个匿名引用:
  47. my $array3Ref = [1, 2, 3, 4, 5];
  48. print $array3Ref; # e.g. "ARRAY(0x22710c0)"
  49. print @{ $array3Ref }; # "12345"
  50. print @$array3Ref; # "12345"
复制代码

论坛徽章:
30
水瓶座
日期:2014-08-22 21:06:3415-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:4715-16赛季CBA联赛之天津
日期:2019-01-11 01:11:44
10 [报告]
发表于 2013-03-06 00:09 |只看该作者
  1. 条件
  2. if ... elsif ... else ...
  3. 在这里没有惊喜,除了拼写elsif:
  4. my $word = "antidisestablishmentarianism";
  5. my $strlen = length $word;
  6. if($strlen >= 15) {
  7. print "'", $word, "' is a very long word";
  8. } elsif(10 <= $strlen && $strlen < 15) {
  9. print "'", $word, "' is a medium-length word";
  10. } else {
  11. print "'", $word, "' is a a short word";
  12. }

  13. Perl提供了一个较短的语法,这是高度推荐的:
  14. print "'", $word, "' is actually enormous" if $strlen >= 20;

  15. unless ... else ...
  16. my $temperature = 20;
  17. unless($temperature > 30) {
  18. print $temperature, " degrees Celsius is not very hot";
  19. } else {
  20. print $temperature, " degrees Celsius is actually pretty hot";
  21. }

  22. unless通常最好避免使用,因为它们使程序难读懂。“unless[…else]“块可以重构到一个“if[…else]”块。幸运的是,没有elsunless关键字。

  23. 这个,相比之下,强烈推荐,因为它是如此容易阅读:
  24. print "Oh no it's too cold" unless $temperature > 15;

  25. 三元操作符
  26. The ternary operator ? : allows simple if statements to be embedded in a statement. The canonical use for this is singular/plural forms:
  27. 三目操作符?:允许简单的if语句嵌入在一个声明中。
  28. my $gain = 48;
  29. print "You gained ", $gain, " ", ($gain == 1 ? "experience point" : "experience points"), "!";

  30. 三元操作符可以嵌套:
  31. my $eggs = 5;
  32. print "You have ", $eggs == 0 ? "no eggs" :
  33. $eggs == 1 ? "an egg" :
  34. "some eggs";


  35. 循环
  36. 不止有一种方法来做。
  37. Perl有传统的while循环:
  38. my $i = 0;
  39. while($i < scalar @array) {
  40. print $i, ": ", $array[$i];
  41. $i++;
  42. }
  43. Perl还提供了until关键字:
  44. my $i = 0;
  45. until($i >= scalar @array) {
  46. print $i, ": ", $array[$i];
  47. $i++;
  48. }

  49. These do loops are almost equivalent to the above (a warning would be raised if @array were empty):
  50. do循环几乎相当于上述的例子(如果@array是空的将会有一个警告):
  51. my $i = 0;
  52. do {
  53. print $i, ": ", $array[$i];
  54. $i++;
  55. } while ($i < scalar @array);


  56. my $i = 0;
  57. do {
  58. print $i, ": ", $array[$i];
  59. $i++;
  60. } until ($i >= scalar @array);
  61. 基本的c风格的for循环也是可以的。
  62. for(my $i = 0; $i < scalar @array; $i++) {
  63. print $i, ": ", $array[$i];
  64. }

  65. 这种循环被认为是老式的,应该尽量避免这样使用。迭代列表是好得多。
  66. foreach my $string ( @array ) {
  67. print $string;
  68. }

  69. 操作符. .创建一个匿名的整数列表:
  70. foreach my $i ( 0 .. $#array ) {
  71. print $i, ": ", $array[$i];
  72. }

  73. 你不能遍历一个散列。然而,你可以遍历它的键值。使用键内置函数来检索一个数组包含所有键的一个散列。然后使用foreach方法,我们用于数组:
  74. foreach my $key (keys %scientists) {
  75. print $key, ": ", $scientists{$key};
  76. }

  77. Since a hash has no underlying order, the keys may be returned in any order. Use the sort built-in function to sort the array of keys alphabetically beforehand:
  78. 因为一个散列并没有顺序,键值可能在按任何顺序返回。使用排序内置函数事先按字母顺序排序数组的键:
  79. foreach my $key (sort keys %scientists) {
  80. print $key, ": ", $scientists{$key};
  81. }

  82. 如果你不提供显式迭代器,Perl使用一个默认的迭代器,$ _。$ _内置变量:
  83. foreach ( @array ) {
  84. print $_;
  85. }

  86. 如果使用默认的迭代器,你只希望把单个语句内部循环,您可以使用超短循环语法:
  87. print $_ foreach @array;

  88. 循环控制
  89. next and last can be used to control the progress of a loop. In most programming languages these are known as continue and break respectively. We can also optionally provide a label for any loop. By convention, labels are written in ALLCAPITALS. Having labelled the loop, next and last may target that label. This example finds primes below 100:
  90. next和last可用于控制循环的进展。在大多数编程语言这些被称为continue和break。我们还可以为任何循环提供一个label。按照惯例,lable所有字母都大写:
  91. CANDIDATE: for my $candidate ( 2 .. 100 ) {
  92. for my $divisor ( 2 .. sqrt $candidate ) {
  93. next CANDIDATE if $candidate % $divisor == 0;
  94. }

  95. print $candidate. " is prime\n";
  96. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP