免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: shijiang1130
打印 上一主题 下一主题

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
21 [报告]
发表于 2013-03-07 20:51 |只看该作者
  1. 调用没有文件句柄的< >,会从STDIN中读取数据,或从任何文件中。
  2. As you may have gathered, print prints to STDOUT by default if no filehandle is named.
  3. 如果没有文件句柄被命名, 默认情况下打印到STDOUT。

  4. 文件测试
  5. 功能- e是一个测试指定的文件是否存在的内置函数。
  6. print "what" unless -e "/usr/bin/perl";

  7. 功能- d是一个测试指定的文件是否为一个目录的内置函数。
  8. 功能- f是一个测试是否在指定的文件是纯文本文件的内置功能,。
  9. 这些只是三个大型类的函数的形式- X,X是一些小写或大写字母。这些函数被称为文件测试。注意前置”-”。在谷歌查询,”-”表示排除结果包含该搜索词。搜索“perl文件测试”可得到结果。

  10. 正则表达式

  11. 正则表达式出现在许多语言和工具里。Perl的核心正则表达式语法和在其他地方一样基本上是相同的,但Perl的正则表达式功能完整复杂和难以理解。我给你的最好建议是只要有可能避免这种复杂性。
  12. 执行匹配操作使用= ~ m / /。在标量上下文,= ~ m / /成功执行,将返回true;如果失败将返回false。
  13. my $string = "Hello world";
  14. if($string =~ m/(\w+)\s+(\w+)/) {
  15. print "success";
  16. }

  17. 括号执行子匹配。成功后执行匹配操作匹配得到的值在的变量$ 1,$ 2,$ 3,…:
  18. print $1; # "Hello"
  19. print $2; # "world"

  20. 在列表上下文,= ~ m / /返回$ 1,$ 2,…作为一个列表。
  21. my $string = "colourless green ideas sleep furiously";
  22. my @matches = $string =~ m/(\w+)\s+((\w+)\s+(\w+))\s+(\w+)\s+(\w+)/;
  23. print join ", ", map { "'".$_. "'" } @matches;
  24. # prints "'colourless', 'green ideas', 'green', 'ideas', 'sleep', 'furiously'"

  25. 替换操作执行使用= ~ s / / /。
  26. my $string = "Good morning world";
  27. $string =~ s/world/Vietnam/;
  28. print $string; # "Good morning Vietnam"

  29. 注意,$ string的内容已经改变了。你必须对标量变量操作。如果是一个文字字符串,你会得到一个错误。
  30. / g标志表示”组匹配”。
  31. 在标量上下文, ~ m / / g执行全文查找,成功,返回true;如果执行失败将返回false。你可以访问$1得到内容。例如:
  32. my $string = "a tonne of feathers or a tonne of bricks";
  33. while($string =~ m/(\w+)/g) {
  34. print "'".$1."'\n";
  35. }

  36. 在列表中上下文中,一个= ~ m / / g在一次调用返回所有的内容。
  37. my @matches = $string =~ m/(\w+)/g;
  38. print join ", ", map { "'".$_. "'" } @matches;

  39. 一个= ~ s / / / g调用执行全文搜索/替换并返回匹配的数量。在这里,我们替换所有元音字母“r”。
  40. #试一试没有/ g。
  41. $string =~ s/[aeiou]/r/;
  42. print $string; # "r tonne of feathers or a tonne of bricks"

  43. $string =~ s/[aeiou]/r/;
  44. print $string; # "r trnne of feathers or a tonne of bricks"

  45. #和使用/ g
  46. $string =~ s/[aeiou]/r/g;
  47. print $string, "\n"; # "r trnnr rf frrthrrs rr r trnnr rf brrcks"

  48. /I 使匹配和替换不区分大小写的。
  49. / x标志允许您的正则表达式包含空格(例如。、换行符)和评论。
  50. "Hello world" =~ m/
  51. (\w+) # one or more word characters
  52. [ ] # single literal space, stored inside a character class
  53. world # literal "world"
  54. /x;
  55. #返回true
复制代码

论坛徽章:
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
22 [报告]
发表于 2013-03-07 22:12 |只看该作者
  1. 模块和包
  2. 在Perl中模块和包是不同的东西。

  3. 模块
  4. 一个模块是一个.pm文件,您可以包括在另一个Perl文件(脚本或模块)。语法和.pl Perl脚本完全相同。一个示例模块: C:\foo\bar\baz\Demo\StringUtils. pm 或 /foo/bar/baz/Demo/StringUtils. Pm :
  5. use strict;
  6. use warnings;
  7. sub zombify {
  8. my $word = shift @_;
  9. $word =~ s/[aeiou]/r/g;
  10. return $word;
  11. }

  12. return 1;

  13. 因为一个模块从上到下执行加载时,您需要返回真值最后表明它已经成功加载。
  14. 为了Perl解释器可以找到他们, Perl模块应该列入你的环境变量$ PERL5LIB:
  15. set PERL5LIB=C:\foo\bar\baz;%PERL5LIB%

  16. export PERL5LIB=/foo/bar/baz:$PERL5LIB

  17. Once the Perl module is created and perl knows where to look for it, you can use the require built-in function to search for and execute it during a Perl script. For example, calling require Demo::StringUtils causes the Perl interpreter to search each directory listed in PERL5LIB in turn, looking for a file called Demo/StringUtils.pm. After the module has been executed, the subroutines that were defined there suddenly become available to the main script. Our example script might be called main. pl and read as follows:
  18. 一旦Perl模块创建而且Perl知道在哪里寻找它,你可以使用 require内置函数搜索并执行它在。例如,调用 require Demo::StringUtils使Perl解释器来搜索列在PERL5LIB环境变量里的每个目录,寻找一个文件叫做Demo/ StringUtils.pm。如下:
  19. use strict;
  20. use warnings;
  21. require Demo::StringUtils;
  22. print zombify("i want brains"); # "r wrnt brrrns"

  23. 注意,使用双冒号::作为目录分隔符。

  24. 现在的问题是,:如果main.pl包含许多require调用,每个模块又包含更多的require调用,然后它难以追踪原始的zombify()子程序。这个问题的解决方案是使用包。


  25. A package is a namespace in which subroutines can be declared. Any subroutine you declare is implicitly declared within the current package. At the beginning of execution, you are in the main package, but you can switch package using the package built-in function:
  26. 一个包是一个子程序声明的名称空间。任何子程序隐式声明在当前的包。执行开始时,你在main包,但你可以用内置package函数切换包:
  27. use strict;
  28. use warnings;
  29. sub subroutine {
  30. print "universe";
  31. }

  32. package Food::Potatoes;
  33. # no collision:
  34. sub subroutine {
  35. print "kingedward";
  36. }

  37. 注意,使用双冒号::作为一个名称空间分隔符。
  38. 任何时间你调用子程序,默认在当前的包。或者,您可以显式地指定一个包名。看看会发生什么,如果我们继续上面的脚本:
  39. subroutine(); # "kingedward"
  40. main::subroutine(); # "universe"
  41. Food::Potatoes::subroutine(); # "kingedward"

  42. 所以逻辑解决上述问题是修改 C:\foo\bar\baz\Demo\StringUtils. pm or /foo/bar/baz/Demo/StringUtils. pm

  43. use strict;
  44. use warnings;
  45. package Demo::StringUtils;
  46. sub zombify {
  47. my $word = shift @_;
  48. $word =~ s/[aeiou]/r/g;
  49. return $word;
  50. }

  51. return 1;

  52. 和修改main.pl:
  53. use strict;
  54. use warnings;
  55. require Demo::StringUtils;
  56. print Demo::StringUtils::zombify("i want brains"); # "r wrnt brrrns"

  57. 现在仔细读一下。
  58. 包和模块是两个完全独立的和独特的特征的Perl编程语言。事实上,他们都使用相同的双冒号分隔符是让人觉得模棱两可。
  59. 分离这两个概念是最愚蠢的一个特性的Perl,并把它们当作单独的概念会不可避免地导致混乱,令人发狂的代码。对我们来说幸运的是,大多数的Perl程序员遵守以下两个定律:
  60. 1。一个Perl脚本(.pl文件)必须始终包含完全零包声明。
  61. 2。一个Perl模块(.pm文件)必须始终包含一个包声明,对应其名称和位置准确。如模块 Demo/ StringUtils。必须用 package Demo::StringUtils 作为第一行。
  62. 正因为如此,在实践中你会发现,大多数“包”和“模块”可以被认为和指互换。然而,重要的是,你不要把这是理所当然的,因为有一天你会遇到一个疯子的代码。
复制代码

论坛徽章:
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
23 [报告]
发表于 2013-03-07 22:39 |只看该作者


  1. 杂项笔记
  2. •核心模块Data::Dumper可以用来输出任意标量到屏幕上。这是一个基本的调试工具。
  3. •有另一个语法,qw { },为声明数组。这是经常出现在使用语句:
  4. use Account qw{create open close suspend delete};

  5. •In =~ m// and =~ s/// operations, you can use braces instead of slashes as the regex delimiters. This is quite useful if your regex contains a lot of slashes, which would otherwise need escaping with backslashes. For example, =~ m{///} matches three literal forward slashes, and =~ s{^https? ://}{} removes the protocol part of a URL.
  6. •在= ~ m / /和= ~ s / / /操作,您可以使用括号代替斜杠分隔符的正则表达式。这是非常有用的,如果你的正则表达式包含大量的斜杠,否则需要与反斜杠转义。例如,= ~ m { / / / }匹配三个字面斜杠,{ ^ = ~年代https吗?:/ / } { }解除协议的一部分,一个URL。
  7. •Perl does have CONSTANTS. These are discouraged now, but weren't always. Constants are actually just subroutine calls with omitted brackets.
  8. •Perl确实有常量。这些是气馁了,但不总是。常数实际上只是子程序调用与省略括号。
  9. •Sometimes people omit quotes around hash keys, writing $hash{key} instead of $hash{"key"}. They can get away with it because in this situation the bareword key occurs as the string "key", as opposed to a subroutine call key().
  10. •有时人们忽略引号散列键、写$hash{key}而不是$hash{“key”}。他们可以这样做,因为在这种情况下,bareword关键是字符串“key”,与子程序调用键()。
  11. •如果你看到一块非格式化代码包装在一个分隔符,像< < EOF、你就要用关键词“here-doc”来搜索答案。

  12. •警告!很多内置函数可以调用不带参数,使用变量$ _。希望这将有助于你理解:
  13. print foreach @array;
  14. and
  15. foreach ( @array ) {
  16. next unless defined;
  17. }

  18. 我不喜欢这种阵型,因为当重构它可以导致问题。

  19. ----      <全剧终>   -----
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP