用户工具

站点工具


语法:perl语法速查

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
语法:perl语法速查 [2026/07/14 09:32] – [2.1 删除当前目录下除pdf之外的其它普通文件] user01语法:perl语法速查 [2026/10/14 06:19] (当前版本) – 外部编辑 127.0.0.1
行 1: 行 1:
  
-====== - perl语法速查 ====== +# - perl语法速查 
- +## - 技巧文章
- +
-===== - 技巧文章 =====+
 {{indexmenu>:linux:perl}} {{indexmenu>:linux:perl}}
  
行 10: 行 8:
  
  
-===== - perl单行命令 =====+## - perl单行命令
  
-<code bash>+```bash
 # linux里面可以用 '' # linux里面可以用 ''
 perl -e "print 0;" perl -e "print 0;"
行 18: 行 16:
  
 perl -e "$i = int(rand(100000)); print $i"    # 单行命令产生随机数 perl -e "$i = int(rand(100000)); print $i"    # 单行命令产生随机数
-</code> +```
- +
-===== - 正则表达式 =====+
  
-==== - 匹配多次 ==== 
  
 +## - 正则表达式
 +### - 匹配多次
 /a{5,15}/将匹配5 个到15 个a 中的任意一个(包括 5,和15) /a{5,15}/将匹配5 个到15 个a 中的任意一个(包括 5,和15)
  
行 37: 行 34:
  
  
-==== - 匹配非 ====+### - 匹配非
 [^def]将匹配上这三个字符中之外的任意单个字符 [^def]将匹配上这三个字符中之外的任意单个字符
  
-===== - number =====+## - number
  
-<code perl>+```perl
 浮点数 浮点数
 1.25 1.25
行 63: 行 60:
  
 0xff       # FF hex, also 255 decimal    十六进制数 0xff       # FF hex, also 255 decimal    十六进制数
 +```
  
-</code> 
  
-===== - list =====+## - list
  
-<code perl>+```perl
  
 @ar = (); @ar = ();
行 76: 行 73:
 $bb = pop $ar;   # 取出数组最后一个元素给变量,数组长度减1 $bb = pop $ar;   # 取出数组最后一个元素给变量,数组长度减1
 unshift @ar, @add; #将add数据里面的数据移入ar前面 unshift @ar, @add; #将add数据里面的数据移入ar前面
 +```
  
-</code> 
  
  
-<code>+ 
 +```
 @a = (1, 2, 3); @a = (1, 2, 3);
 $a = (21, 22, 33); $a = (21, 22, 33);
行 89: 行 87:
 a = 33 a = 33
 a = 1 2 3 a = 1 2 3
-</code>+```
  
-<code perl>+ 
 + 
 +```perl
 @a = ((1, 2), (3, 4)); @a = ((1, 2), (3, 4));
 for $i (@a) { for $i (@a) {
行 102: 行 102:
 3 3
 4 4
-</code> +``` 
-===== - hash =====+ 
 +## - hash
  
-<code perl>+```perl
  
 %hash = (); %hash = ();
行 142: 行 143:
 #按ref值的降序排列 #按ref值的降序排列
 } }
 +```
  
-</code> 
  
  
  
-===== - reference ===== +## - reference 
-==== - ref for array ====+### - ref for array
  
-<code perl>+```perl
  
 @a = (1, 2, 3); @a = (1, 2, 3);
行 161: 行 162:
 print "$ref\n";     # result: ARRAY(0x25d7338) print "$ref\n";     # result: ARRAY(0x25d7338)
 print "@$ref\n";    # result:  1 2 3 print "@$ref\n";    # result:  1 2 3
-</code>+```
  
  
-<code perl>+ 
 + 
 +```perl
 @a = (1, 2, 3); @a = (1, 2, 3);
 @b = (4, 5, 6); @b = (4, 5, 6);
行 179: 行 182:
 4 5 6 4 5 6
 7 8 9 7 8 9
-</code> +``` 
-==== - ref for hash ==== + 
-<file perl t.pl>+### - ref for hash 
 + 
 +~~~perl t.pl
 sub new_block { sub new_block {
     return {     return {
行 216: 行 221:
 } }
 print "\n"; print "\n";
-</file>+~~~ 
  
 运行结果: 运行结果:
-<code>+ 
 +```
 D:\>perl t.pl D:\>perl t.pl
 ------------- -------------
行 229: 行 236:
 a   a0 a   a0
 b   b0 b   b0
-</code>+```
  
-==== - hash包array ==== + 
-<code perl>+### - hash包array 
 + 
 +```perl
 @a = (1, 2, 3); @a = (1, 2, 3);
 @b = (11, 22, 33); @b = (11, 22, 33);
行 255: 行 264:
 33 33
 c = 33 c = 33
-</code>+```
  
  
-==== - array包hash ==== + 
-<code perl>+### - array包hash 
 + 
 +```perl
 $hash_a{a} = 1; $hash_a{a} = 1;
 $hash_b{a} = 2; $hash_b{a} = 2;
行 275: 行 286:
 1 1
 2 2
-</code>+```
  
-<code perl>+ 
 + 
 +```perl
 $hash_a{a} = 1; $hash_a{a} = 1;
 $hash_b{a} = 2; $hash_b{a} = 2;
行 290: 行 303:
 a a
 2 2
-</code>+``` 
 + 
  
-<code perl>+```perl
 @ar = (); @ar = ();
 sub new_hash { sub new_hash {
行 318: 行 333:
 2 2
 value_2 value_2
-</code> +``` 
-===== - 循环 ===== + 
-==== - for ==== +## - 循环 
-<code perl>+### - for 
 + 
 +```perl
 @ar = (1, 2); @ar = (1, 2);
  
行 344: 行 361:
 1 1
 2 2
-</code>+```
  
  
-===== - 字符串操作 ===== 
  
-<code perl>+## - 字符串操作 
 + 
 +```perl
 "hello" . "world"       # same as "helloworld" "hello" . "world"       # same as "helloworld"
 "hello" . ' ' . "world" # same as 'hello world' "hello" . ' ' . "world" # same as 'hello world'
行 357: 行 375:
 "barney" x (4+1) # is "barney" x 5, or "barneybarneybarneybarneybarney" "barney" x (4+1) # is "barney" x 5, or "barneybarneybarneybarneybarney"
 5 x 4.8          # is really "5" x 4, which is "5555" 5 x 4.8          # is really "5" x 4, which is "5555"
-</code> +```
- +
-===== - 文件和目录操作 ===== +
  
-==== - 读写文件 ==== 
  
 +## - 文件和目录操作
 +### - 读写文件
  
-<code perl>+```perl
 #open读文件 #open读文件
 open(my $fh, "<", "input.txt") open(my $fh, "<", "input.txt")
行 378: 行 394:
 close($handle) close($handle)
   || warn "close failed: $!";   || warn "close failed: $!";
-</code>+```
  
  
  
-<code perl>+ 
 + 
 +```perl
 #写文件 #写文件
 print $fh "hello\n"; print $fh "hello\n";
行 391: 行 409:
   print $line;   print $line;
 } }
-</code>+```
  
-==== - readdir ==== 
  
-<code perl>+### - readdir 
 + 
 +```perl
 # 读目录 # 读目录
 my $dir_to_process = '/etc'; my $dir_to_process = '/etc';
行 403: 行 422:
 } }
 closedir $dh; closedir $dh;
-</code>+```
  
  
-==== - 删除文件 unlink ==== + 
-<code perl>+### - 删除文件 unlink 
 + 
 +```perl
 unlink 'slate', 'bedrock', 'lava'; unlink 'slate', 'bedrock', 'lava';
 unlink qw(slate bedrock lava); unlink qw(slate bedrock lava);
 unlink glob '*.o'; unlink glob '*.o';
-</code>+```
  
-==== - rename ==== + 
-<code perl>+### - rename 
 + 
 +```perl
 rename 'old', 'new'; rename 'old', 'new';
-</code>+```
  
  
-==== - mkdir rmdir ==== + 
-<code perl>+### - mkdir rmdir 
 + 
 +```perl
 mkdir 'fred', 0755 mkdir 'fred', 0755
 rmdir $temp_dir; rmdir $temp_dir;
-</code>+```
  
  
-==== - chmod ==== + 
-<code perl>+### - chmod 
 + 
 +```perl
 chmod 0755, 'fred', 'barney'; chmod 0755, 'fred', 'barney';
-</code>+``` 
 + 
 + 
 +### - chown
  
-==== - chown ==== +```perl
-<code perl>+
 chown $user, $group, glob '*.o'; chown $user, $group, glob '*.o';
-</code>+```
  
  
-==== - 获取当前目录PATH ====+ 
 +### - 获取当前目录PATH
 有两种方法: 有两种方法:
-<code perl>+ 
 +```perl
 use Cwd; use Cwd;
 my $dir = getcwd; my $dir = getcwd;
 #$dir中即为当前目录的完整路径信息。 #$dir中即为当前目录的完整路径信息。
-</code>+``` 
 + 
  
-<code perl>+```perl
 my $dir = $ENV{'PWD'}; my $dir = $ENV{'PWD'};
 #ENV是一个散列,用于存放环境变量。PWD是Linux的环境变量,表示当前所在目录。 #ENV是一个散列,用于存放环境变量。PWD是Linux的环境变量,表示当前所在目录。
-</code> +```
-===== - perl处理excel =====+
  
-==== - 读excel ====+## - perl处理excel 
 +### - 读excel
  
-<code perl>+```perl
 use Spreadsheet::ParseExcel; use Spreadsheet::ParseExcel;
 use Spreadsheet::ParseXLSX; use Spreadsheet::ParseXLSX;
行 485: 行 518:
     print "Cell A1: $cell_value\n";     print "Cell A1: $cell_value\n";
 } }
-</code>+```
  
  
-===== - perl处理json ===== 
  
-<code perl [enable_line_numbers="true"]>+## - perl处理json 
 + 
 +```perl [enable_line_numbers="true"]
 use JSON; use JSON;
 use Data::Dumper; use Data::Dumper;
行 549: 行 583:
     }     }
 } }
 +```
  
-</code> 
  
  
-==== UTF8与GBK的转换 ====+### UTF8与GBK的转换
  
-<code perl>+```perl
 use Encode; use Encode;
 gbk转uft-8: gbk转uft-8:
行 565: 行 599:
 uft-8转gb2312: uft-8转gb2312:
 $line = encode("gb2312", decode("utf8", $line)); $line = encode("gb2312", decode("utf8", $line));
 +```
  
-</code> 
  
-====== - 常用脚本 ======+# - 常用脚本 
 +## - 删除当前目录下除pdf之外的其它普通文件
  
-===== - 删除当前目录下除pdf之外的其它普通文件 ===== +```perl
- +
-<code perl>+
  
 #!/usr/bin/perl #!/usr/bin/perl
行 599: 行 632:
             } else {             } else {
                 print "$name, is not pdf\n";                 print "$name, is not pdf\n";
 +                unlink $name;
             }             }
         },         },
行 605: 行 639:
     $target_dir     $target_dir
 ); );
 +```
  
- 
-</code> 
语法/perl语法速查.1783992724.txt.gz · 最后更改: (外部编辑)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki