Perl 收藏笔记总结(2018年前)

1:单行注释和多行注释使用介绍、应用技巧

收藏链接: http://www.jb51.net/article/34931.htm

  • 单行注释:行头加#

  • 多行注释 :使用 POD(Plain Old Documentations)

    =pod
    codes to comment
    =cut
    
    # 注意:=pod =cut只能在行首以=开头,以=cut结尾。 注意:=后面要紧接一个字符。=cut后面可以不用。
    

2:目录文件操作–复制,移动,重命名

收藏链接: http://blog.csdn.net/xuexiaokkk/article/details/51726835

## perl目录操作,创建目录句柄指向要操作的目录。 打开目录句柄使用 opendir ;
opendir dirhandle,directory ;
例:取某目录下文件的列表:
opendir (e,'E:\娱乐\music\阿杜')|| die"can't open e: $!" ;
@files=readdir e; 
closedir(e) ;

for($index=0;$index<@files ;$index++){
   print "@files[$index]\n" ;
}
# 输出指定目录下的所有文件(该目录不包含子目录)。

## 创建目录:mkdir
mkdir('d:\test',0755)||die "can't create directory: $!" ;

## 删除目录:rmdir
rmdir('d:\perl\wzj')||die "can't remove diretory: $!";  #删除之前目录必须为空。

## 在某一目录内找指定文件:
# 例:在d盘下找311.txt
use strict ;
use File::Find ;
sub wanted{
  if ($_ eq "311.txt"){
      print $File::Find::name ;
      print "\n" ;
  }
}
find \&wanted ,"d:/";

## 列出指定目录下所有的文件,包含子目录的内容:
use strict ;
use File::Find ;
sub wanted{
   if (-f $File::Find::name){    #判断传入的是文件而不是目录。
      print "remove $File::Find::name ";
      print "\n";
    #  unlink $File::Find::name ;
   }
}
find \&wanted,'E:\nero\Content' ;

## 查找指定目录下某类型文件:
# 例:查找e盘下所有exe文件
use strict ;
use File::Find ;
sub wanted{
   if (-f $File::Find::name){
      if ($File::Find::name =~/\.exe$/i){
          print "remove $File::Find::name ";
          print "\n";
     }
     
    #  unlink $File::Find::name ;
   }
}
find \&wanted,'E://' ;

## 文件复制
use File::Copy ;
use strict ;
copy("d:/book/三国演义.txt","d:/book/aa")||warn "could not copy files :$!" ;
#将d:/book/三国演义.txt复制到d:/book/aa下

## 文件移动
use File::Copy ;
use strict ;
move("d:/book/ss.txt","d:/book/aa")||warn "could not copy files :$!" ;

## 文件改名
use File::Copy ;
use strict ;
rename("d:/perl/tt.txt","d:/perl/tttttt.txt");
#将d:/perl/tt.txt改名为d:/perl/tttttt.txt

3:使用Getopt::Long模块来接收用户命令行参数

收藏链接:http://www.jb51.net/article/34989.htm

下面的例子略微复杂呀,看不懂,看不懂。

#!/usr/bin/perl
use strict;
use Getopt::Long;
use Smart::Comments;

my @libs    = (); 
my %flags   = (); 
my ( $verbose, $all, $more, $diam, $debug, $test, $step);

GetOptions(
        'verbose+'  => \$verbose,
        'more!'     => \$more,
        'debug:i'   => \$debug,
        'lib=s'     => \@libs,
        'flag=s'    => \%flags,
        'test|t'    => \$test,
        'all|everything|universe' => $all,
);

### $verbose
### $more
### $debug
### $test
### @libs;
### %flags

# 'verbose+':接有 + 的选项不接收变量,后面不需要加内容,直接使用就行了,会在每次出现时增加一次变量,就是讲命行时在参数中 -verbose -verbose 出现二次时 verbose 的值就会变成2
# 'more!':接有!的选项不接收变量(也就是讲后面不需要加参数 -more 来使用就行了),只要命令行中出现了这个参数,就会默认是 1 ,是用来设置打开和关掉一个功能的>,可以在参数前加 no 变成负的例如-nomore
# `flag=s':接有 = 的字符串要求接字符串(s)、整数(i),或者浮点(f)等类型的变量
# `debug:i':接有 : 的选项会接受缺省为0或者为空字符串的可选变量
# `test|t':接有 | 的选项表示可以给 –test 简写为 -t
# `lib=s'     => @libs    如果相关联的变量是个数组, 如这个地方的 @libs, 那么选项可以多次出现, 值可以被推到数组里
# ‘flag=s'    => %flags  如果相关联的变量是个散列, 那么就要求一个键=值(key=value)对, 并被插入到散列里
# 备注:在匹配参数名的时候,GetOptions 在缺省设置下会忽略大小写,默认参数被简写为唯一的最短字符串(首字母)(例如,-m 代表 -more. 相同的首字母时,会加上第二个字母来区分)

4:Perl6与生物信息

收藏链接:https://mp.weixin.qq.com/s/3lowLB_GTWF-TLXaHpSkzg

Perl6我是不会去学习了,但是这里的一个博客可以收藏Young For Perl 6

5:GD::Graph模块

收藏链接:http://www.kuqin.com/article/25perl/1062258.html

用Perl模块绘制报表和统计图的帖子,很难会用到了。

6:map和grep两大函数简单解析

收藏链接:https://mp.weixin.qq.com/s/OD52lY4Xkyqk9GKnSEfAfA

perl中的一个利器就是正则表达式,处理文本时非常简洁和高效。但还有两个函数:map和grep,在处理数组相关问题的时候特别强大!
map函数的语法:
map EXPR, LIST
map BLOCK LIST
简单说明:
map函数对LIST里的每个元素按BLOCK或EXPR进行计算,遍历LIST时,临时将LIST里的每个元素赋值给$_变量,map对每次的计算返回一个结果列表赋予左值。其中,左值的含义可以这样理解:如果返回值存储在list中,则表示处理后的list,若返回值存储在scalar中,则表示处理后的list中元素个数。

## 示例1:打印数组元素
my @array = qw/aa ab aa ac ad/;
map {print "$_\n"} @array;

# 输出结果:
aa
ab
aa
ac
ad

## 示例2:元素大小写转换
my @array = qw/aa ab aa ac ad/;
@array = map {uc $_} @array;
print "@array\n";

# 输出结果:
AA AB AA AC AD
# 备注:perl中内置了两个函数uc(大写)和lc(小写)

## 示例3:数组和哈希转换
my @array = qw/aa ab ac ad/;
my %hash = map {$_,1} @array;
foreach my $key (sort keys %hash){
    print "$key ==> $hash{$key}\n";
}

# 输出结果:
aa ==> 1
ab ==> 1
ac ==> 1
ad ==> 1

## 示例4:生成随机密码
my @array = (1..10,"a".."z");
my @password =  join "",@array[map {rand @array } 0..6];
print "@password\n";

# 输出结果:
6qatw1s
# 备注:这个是随机的,每次结果都会不同。

## 示例5:元素替换
my @array = 1..10;
map {$_ = 's' unless $_ > 5 } @array;
print "@array\n";

# 输出结果:
s s s s s 6 7 8 9 10
# 备注:对$_ 修改,相当于修改@array的元素

grep函数的语法:
grep BLOCK LIST
grep EXPR, LIST
简单说明:
grep的语法格式与map类似,不过grep是通过判断列表中每个元素是否满足表达式或者块来返回true和false,再根据true和false来决定最终的返回列表。所以grep多时用来过滤元素用的。

## 示例1:元素去重
my @array = qw/ac ac aa aa/;
my %count;
@array = grep {++$count{$_} < 2} @array;
print "@array\n";

# 输出结果:
ac aa

## 示例2:特定条件筛选
my @array = 1..20;
@array = grep $_ > 10, @array;
print "@array\n";

# 输出结果:
11 12 13 14 15 16 17 18 19 20

## 示例3:获取数组下标为偶数的元素
my @array = 1..10;
@array = @array[grep {$_ & 2} 0..$#array];
print "@array\n";

# 输出结果:
3 4 7 8

## 示例4:正则匹配
my @array = qw/abba baba/;
@array = grep /bb/ ,@array;
print @array,"\n";

# 输出结果:
abba

## 示例5:计数当前目录的文本文件
my $num = grep {-f && -T} glob "* .*";
print $num,"\n";

# 输出结果:
14

7:List::Util模块来处理数组

收藏链接:http://bnuzhutao.cn/archives/788

以下的函数都是通过C语言优化过的,运行速度非常快。

  • (1)求数组的和:不需要一个一个地累加,直接调用 sum 函数
use List::Util qw/sum/;
my @array = (10, 20, 30, 40);
my $sum = sum @array;       # 得到 100
  • (2)求数组的最大、最小值:不需要逐个比较,直接调用 max 和 min 函数
use List::Util qw/max min/;
my @array = (10, -1, 6, 25, 8);
my $max = max @array;           # 得到 25
my $min = min @array;           # 得到 -1
  • (3)如果是按照字符串排列的最大、最小值呢?调用 maxstr 和 minstr 函数
use List::Util qw/maxstr minstr/;
my @array = ("Beijing", "Shanghai", "Guangzhou", "Chengdu", "Nanjing");
my $maxstr = maxstr @array;     # 得到 Shanghai
my $minstr = minstr @array;     # 得到 Beijing

8:perl数组高级操作技巧

收藏链接:http://www.jb51.net/article/57754.htm

  • 去除一个数组中的重复元素

    • 使用grep函数代码片段
    my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 );
    my %count;
    my @uniq_times = grep { ++$count{ $_ } < 2; } @array;
    
    • 使用转换hash代码片段
    my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 );
    my %saw;
    @saw{ @array } = ( );
    my @uniq_array = sort keys %saw;
    
  • 合并两个array

push @array1, @array2;
  • 列表归并
use List::Util qw(reduce);
my $sum = reduce { $a + $b } 1 .. 1000;

# 也可以写成下面这样:
my $product = reduce { $a * $b } 1 .. 1000;
  • 判断是否有元素匹配
# 纯粹用Perl实现,找到列表中第一个符合某条件的元素,比找出所有符合条件的要麻烦一些。下面的例子,判断是否有大于1000的元素:
my $found_a_match = grep { $_ > 1000 } @list;

# 注意:如果@list有一亿个元素,而要找的就是1001?grep仍然还会循环一亿次,当然你可以向下面自己控制下:
my $found_a_match = 0;
foreach my $elem (@list) {
$found_a_match = $elem if $elem > 1000;
last if $found_a_match;
}

# List::Util:
use List::Util qw(first);
my $found_a_match = fist { $_ > 1000 } @list;

# List::MoreUtils模块
my $found_a_match = any { $_ > 1000 } @list;
my $all_greater = all { $_ > 1000 } @list;
my $none_greater = none { $_ > 1000 } @list;
my $all_greater = notall { $_ % 2 } @list;
  • 一次遍历多个列表
## 方法1:
my @a = ( ... );
my @b = ( ... );
my @c;
foreach my $i ( 0 .. $#list ) {
my ( $a, $b ) = ( $a[$i], $b[$i] );
push @c, $a + $b;
}

## 方法2:
use List::MoreUtils qw(pairwise);
my @c = pairwise { $a + $b } @a, @b;

## 方法3:
# pairwise只适合两个列表的同步计算,三个后用each_array:
use List::MoreUtils qw(each_array);
my $ea = each_array( @a, @b, @c );
my @d;
while ( my ( $a, $b, $c ) = $ea->() ) {
push @d, $a+$b+$c;
}

  • 数组合并
# MoreUtils的mesh
use List::MoreUtils qw(mesh);
my @odds = qw/ 1 3 5 7 9/;
my @evens= qw/ 2 4 6 8 0/;
my @nums = mesh @odds, @evens; # print: 1 2 3 4 ...

9:Merging two files based on first column and returns multiple values for each key

收藏链接:http://stackoverflow.com/questions/12377892/merging-two-files-based-on-first-column-and-returns-multiple-values-for-each-key

## 题目:
# Example:
## File Input 1:
12345|AA|BB|CC
23456|DD|EE|FF
## File Input2:
12345|A|B|C
12345|D|E|F
12345|G|H|I
23456|J|K|L
23456|M|N|O
32342|P|Q|R

# WANTED OUTPUT:
12345|AA|BB|CC|A|B|C
12345|AA|BB|CC|D|E|F
12345|AA|BB|CC|G|H|I
23456|DD|EE|FF|J|K|L
23456|DD|EE|FF|M|N|O

《Perl 收藏笔记总结(2018年前)》 我的微信公众号

如果实在有需要请给我发邮件:mengyuanshen@126.com
也可以关注我的公众号:沈梦圆(PandaBiotrainee)

    原文作者:沈梦圆1993
    原文地址: https://www.jianshu.com/p/bf92a0fd3eae
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞