cheng029 发表于 2015-12-27 15:19:44

Perl知识

  1. s/// 进行替换
  2. /g 全局替换
  3. s/// 替换中如果使用配对的字符,则需要使用两对
  4. /i,/x,/s同样可以使用在替换中
  5. =~绑定操作
  6. \U表示大写 \L表示小写 \E终止大小写影响 \u \l 表示只作用于下一个字符
  7. split操作 @fields = split /separtor/,$string; 默认对$_操作
  8. my @fields=split #split /\s+/,$_;
  9. my $result=join $glue,@pieces; 列表元素小于2,则不会有粘合的元素;
  my $x=join ":",4,6,8,10,12;#$x为"4:6:8:10:12"
  10. $_ ="Hello there,neighbor!";
  my($first,$second,$third)=/(\S+) (\S+),(\S+)/;
  print "$second is my $third\n";
  11.非贪婪的数量词 +? , *? , ?? , {5,10}? , {8,}?
  12. /m 多行匹配
  13. $^I更新文件.
  14.$ perl -p -i.bak -w -e 's/Randall/Randal/g' fred*.dat
  -p要求perl写一个程序; while(<>) { print; }-n自动省略print语句
  -i.bak 表示$^I的值设置为".bak" -w 将警告打开-e 执行下面的代码
  #!perl -w
  $^I=".bak";
  while(<>) {
  s/Randall/Randal/g;
  print:
  }
  15. ?号的四种用法:问号,表示0,1的数量词,非贪婪修饰符,开头符
  if(/(?:bronto)?saurus (steak|burger)/{ print "Fred wants a $1\n"; }
  16. unless控制结构 与if相反 当条件为假时才执行
  17. until ($j>$i) {$j*=2;} 直到表达式返回的值为真
  18. 表达式修饰符 print "$n is a negative number.\n"
  if $n<0;
  19. The Naked Block控制结构 {body; body; body;}
  20. elsif语句类似 else if
  21. 自增++ 自减--,注意区别前置与后置
  22. for控制结构for(init; test; increment) { body;}
  for和foreach是等价的 for(1..10){print;}
  23. 循环控制 last等同于break
  next等同于continue
  redo 与next的区别 next会进入下一次循环,redo会继续执行本次循环
  24. 标签块LINE: while(<>) {
  foreach(split){
  last LINE if/__END__/;
  ...}
  }
  25. 逻辑操作符
  AND(&&),OR(||),如果左侧满足结果,右侧不会进行求值
  短路操作: my $last_name=$last_name{$someone}||'(No last name)';
  26. 三元操作符 ?:
  27. 文件检测操作
  die "Oops! A file call'd '$filename' already exists.\n"
  if -e $filename;      #新建文件时,检测是否存在相同名字的文件.
  28. -M 过去多少天没有修改   -s 大小-A 最近90天

  29. stat和lstat函数
  my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
      $size, $atime, $mtime, $ctime, $blksize, $blocks)
      = stat($filename);
  $dev和$ino 设备号和索引节点号
  $mode 文件权限位以及一些其他的位
  $nlink 文件或目录的(硬)连接数
  $uid和$gid 文件所有权的user ID 和 group ID
  $size 返回其大小
  $atime $mtime $ctime 系统时间格式,32位,表示从某个时刻到现在经过的秒数
  30. localtime 或者 gmtime 当前的时间
  31. 位操作 &,|,^,<<,>>,~
页: [1]
查看完整版本: Perl知识