用户工具

站点工具


语法:perl语法速查

差别

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

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
语法:perl语法速查 [2026/10/13 22:10] – 外部编辑 127.0.0.1语法:perl语法速查 [2026/10/14 06:19] (当前版本) – 外部编辑 127.0.0.1
行 2: 行 2:
 # - perl语法速查 # - perl语法速查
 ## - 技巧文章 ## - 技巧文章
-![:indexmenu>:linux:perl](:indexmenu>:linux:perl)+{{indexmenu>:linux:perl}}
  
  
行 9: 行 9:
  
 ## - perl单行命令 ## - perl单行命令
-<code bash>+ 
 +```bash
 # linux里面可以用 '' # linux里面可以用 ''
 perl -e "print 0;" perl -e "print 0;"
行 15: 行 16:
  
 perl -e "$i = int(rand(100000)); print $i"    # 单行命令产生随机数 perl -e "$i = int(rand(100000)); print $i"    # 单行命令产生随机数
-</code>+``` 
  
 ## - 正则表达式 ## - 正则表达式
行 36: 行 38:
  
 ## - number ## - number
-<code perl>+ 
 +```perl
 浮点数 浮点数
 1.25 1.25
行 57: 行 60:
  
 0xff       # FF hex, also 255 decimal    十六进制数 0xff       # FF hex, also 255 decimal    十六进制数
 +```
  
-</code> 
  
 ## - list ## - list
-<code perl>+ 
 +```perl
  
 @ar = (); @ar = ();
行 69: 行 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);
行 82: 行 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) {
行 95: 行 102:
 3 3
 4 4
-</code>+``` 
 ## - hash ## - hash
-<code perl>+ 
 +```perl
  
 %hash = (); %hash = ();
行 134: 行 143:
 #按ref值的降序排列 #按ref值的降序排列
 } }
 +```
  
-</code> 
  
  
行 141: 行 150:
 ## - reference ## - reference
 ### - ref for array ### - ref for array
-<code perl>+ 
 +```perl
  
 @a = (1, 2, 3); @a = (1, 2, 3);
行 152: 行 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);
行 170: 行 182:
 4 5 6 4 5 6
 7 8 9 7 8 9
-</code>+``` 
 ### - ref for hash ### - ref for hash
-<file perl t.pl>+ 
 +~~~perl t.pl
 sub new_block { sub new_block {
     return {     return {
行 207: 行 221:
 } }
 print "\n"; print "\n";
-</file>+~~~ 
  
 运行结果: 运行结果:
-<code>+ 
 +```
 D:\>perl t.pl D:\>perl t.pl
 ------------- -------------
行 220: 行 236:
 a   a0 a   a0
 b   b0 b   b0
-</code>+``` 
  
 ### - hash包array ### - hash包array
-<code perl>+ 
 +```perl
 @a = (1, 2, 3); @a = (1, 2, 3);
 @b = (11, 22, 33); @b = (11, 22, 33);
行 246: 行 264:
 33 33
 c = 33 c = 33
-</code>+``` 
  
  
 ### - array包hash ### - array包hash
-<code perl>+ 
 +```perl
 $hash_a{a} = 1; $hash_a{a} = 1;
 $hash_b{a} = 2; $hash_b{a} = 2;
行 266: 行 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;
行 281: 行 303:
 a a
 2 2
-</code>+``` 
 + 
  
-<code perl>+```perl
 @ar = (); @ar = ();
 sub new_hash { sub new_hash {
行 309: 行 333:
 2 2
 value_2 value_2
-</code>+``` 
 ## - 循环 ## - 循环
 ### - for ### - for
-<code perl>+ 
 +```perl
 @ar = (1, 2); @ar = (1, 2);
  
行 335: 行 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'
行 347: 行 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")
行 364: 行 394:
 close($handle) close($handle)
   || warn "close failed: $!";   || warn "close failed: $!";
-</code>+```
  
  
  
-<code perl>+ 
 + 
 +```perl
 #写文件 #写文件
 print $fh "hello\n"; print $fh "hello\n";
行 377: 行 409:
   print $line;   print $line;
 } }
-</code>+``` 
  
 ### - readdir ### - readdir
-<code perl>+ 
 +```perl
 # 读目录 # 读目录
 my $dir_to_process = '/etc'; my $dir_to_process = '/etc';
行 388: 行 422:
 } }
 closedir $dh; closedir $dh;
-</code>+``` 
  
  
 ### - 删除文件 unlink ### - 删除文件 unlink
-<code perl>+ 
 +```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 ### - rename
-<code perl>+ 
 +```perl
 rename 'old', 'new'; rename 'old', 'new';
-</code>+``` 
  
  
 ### - mkdir rmdir ### - mkdir rmdir
-<code perl>+ 
 +```perl
 mkdir 'fred', 0755 mkdir 'fred', 0755
 rmdir $temp_dir; rmdir $temp_dir;
-</code>+``` 
  
  
 ### - chmod ### - chmod
-<code perl>+ 
 +```perl
 chmod 0755, 'fred', 'barney'; chmod 0755, 'fred', 'barney';
-</code>+``` 
  
 ### - chown ### - chown
-<code perl>+ 
 +```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 ## - perl处理excel
 ### - 读excel ### - 读excel
-<code perl>+ 
 +```perl
 use Spreadsheet::ParseExcel; use Spreadsheet::ParseExcel;
 use Spreadsheet::ParseXLSX; use Spreadsheet::ParseXLSX;
行 468: 行 518:
     print "Cell A1: $cell_value\n";     print "Cell A1: $cell_value\n";
 } }
-</code>+``` 
  
  
 ## - perl处理json ## - perl处理json
-<code perl [enable_line_numbers="true"]>+ 
 +```perl [enable_line_numbers="true"]
 use JSON; use JSON;
 use Data::Dumper; use Data::Dumper;
行 531: 行 583:
     }     }
 } }
 +```
  
-</code> 
  
  
 ### UTF8与GBK的转换 ### UTF8与GBK的转换
-<code perl>+ 
 +```perl
 use Encode; use Encode;
 gbk转uft-8: gbk转uft-8:
行 546: 行 599:
 uft-8转gb2312: uft-8转gb2312:
 $line = encode("gb2312", decode("utf8", $line)); $line = encode("gb2312", decode("utf8", $line));
 +```
  
-</code> 
  
 # - 常用脚本 # - 常用脚本
 ## - 删除当前目录下除pdf之外的其它普通文件 ## - 删除当前目录下除pdf之外的其它普通文件
-<code perl>+ 
 +```perl
  
 #!/usr/bin/perl #!/usr/bin/perl
行 585: 行 639:
     $target_dir     $target_dir
 ); );
 +```
  
- 
-</code> 
语法/perl语法速查.1791900622.txt.gz · 最后更改: 由 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki