#!/usr/bin/perl use 5.010; $line[19] = "hello"; #say $#line; #标量变量的引用 $a += 2; $ra = \$a; $$ra += 2; #say $$ra; #say ${$ra}; #数组引用 @array = qw ( one two three four five ); $len = @array; #say $len;#数组大小 $rarray = \@array; #say $rarray; $len = @$rarray; #say $len;#引用数组大小 push(@array,'a',1,2); say "@array"; push(@$rarray,'a',1,2);#通过引用赋值 say "@array"; #say $array[2]; #say $$rarray[2]; #say ${$rarray}[2]; #say $rarray -> [2];#箭头记号 @s1 = @array[1,2,3,4]; #say "@s1"; @s2 = @$rarray[1,2,3,4];#通过引用获取值 #say "@s2"; #对散列表的引用 %hash = ( giggs => 39, beckham => 38, ); $rhash = \%hash;#值为指向散列表的引用 #say $hash{"giggs"}; #say $$rhash{"giggs"}; #say $rhash -> {"giggs"};#箭头记号 @rhash = @$rhash{"giggs","beckham"};#通过引用获取值 say "@rhash"; #创建匿名数组,使用方括号替代圆括号 $ra = [];#创建一个空的匿名数组,并返回对它的引用 $ra = ["hello",12]; #创建匿名散列表,使用大括号替代圆括号 $rh = {}; $rh = {"k1","v1","k2","v2"}; #常数是静态分配和匿名的 $r = \10; $rs = \"hello"; #say $rs; #多重引用的间接访问 p37 $a = 10; $ra = \$a; $rra = \$ra; $rrra = \$rra; #say $a; #say $$ra; #say $$$rra; #say $$$$rrra; $a =20; sub text{ return \$a; } $b = ${text()};#大括号中返回变量的引用 #say $b; #特洛伊木马 say "${foo()}"; sub foo{ # say "hello"; #system('/bin/rm *'); } #嵌套数据结构 %sue = ( name => 'giggs', age => 45, ); %john = ( name => 'john', age => 21, ); %lina = ( name => "lina", age => 19, ); @children = (\%john,\%lina); $sue{'children'} = \@children; $sue{'children'} = [\%john,\%lina]; #say $sue{'children'}->[1]->{name}; #say $sue{'children'}[1]{name};#省去两个下标间的箭头 #隐含的创建复杂的数据结构 $sue{'children'}->[1]->{age} = 10; #say $sue{'children'}[1]{age}; #引用的查询 $a = 10; $b = \$a; #say ref($a); #say ref($b); #符号引用 #use strict 'refs'; #不允许符号引用 $x = 10; $var = "x"; $$var = 20; #say $x; #say $$var; $var = "x"; @$var = (1,2,3,4);#@符号指示变量的存取类型$$var=$x,@$var=@x #say "@x"; #二维数组 @matrix = ( (1,2,3), (4,5,6), (7,8,9) ); $matrix[1][2]=100; #say "@matrix"; #颁奖demo open(F,'oscar.txt') || die 'Could not open database.$!'; %category_index=(); %year_index=(); while($line = <F>){ chomp $line; #say $line; ($year,$category,$name) = split(/:/,$line); create_entry($year,$category,$name) if $name; } sub create_entry{ my($year,$category,$name) = @_; $rlEntry = [$year,$category,$name];#匿名数组 #say $year,$year_index{$year}; push(@{$year_index{$year}},$rlEntry); push(@{$category_index{$category}},$rlEntry); } #&print_year('1995'); #打印出给定年份的所有条目 sub print_year{ my($year) = @_; foreach $rlEntry(@{$year_index{$year}}){ say "$rlEntry->[1]:$rlEntry->[2]"; } } #say "------------------"; #&print_sort_year(); #打印出所有年度排序的条目 sub print_sort_year{ foreach $year(sort keys %year_index){ print_year($year); } } #say "------------------"; #根据年度与类别打印特定的条目 sub print_year_category{ my($year,$category) = @_; foreach $rlEntry(@{$year_index{$year}}){ if ($rlEntry ->[1] eq $category) { say "$category ($year) $rlEntry->[2]"; return; } } say "No entry for $category($year)."; } #&print_year_category('1995','Picture'); #say "------------------"; #格式化打印工具 @sample = (11.233,{3=>4,"hello"=>[6,7]}); require 'dumpvar.pl'; dumpValue(\@sample); say ref($year_index{$year}); dumpValue($year_index{$year}); say "-----------------三维数组---------------"; $line = ['solod','black',['1','2','3',['4','5','6']]]; say "$$line[0]"; say "$line->[1]"; say "$line->[2][2]"; say "$line->[2][3][0]"; say "----------------标量引用demo-------------------------"; $var = 10; $rvar = \$var; say $$rvar; say "----------------数组引用demo-------------------------"; @array = qw(one two three four five); $rarray = \@array; say "$$rarray[0]";#输出第一个元素 say "${$rarray}[0]"; say "$rarray->[0]"; $len = scalar(@$rarray); say "数组大小:$len"; say "@$rarray"; #打印数组所有元素 say "@$rarray[1]"; say "@{$rarray}[1]"; #say "@$rarray->[1]";#不推荐 #遍历数组 foreach (@$rarray) { say "foreach遍历数组:$_"; } while(($i,$value)=each(@$rarray)){ say "while-each遍历数组:$i:$value"; } say "----------------散列表引用demo-------------------------"; %hash = ( '01' => "one", '02' => "two", '03' => "three", '04' => "four", ); $rhash = \%hash; say "$$rhash{'03'}";#输出'03'的值 say "${$rhash}{'03'}"; say "$rhash->{'03'}"; #say %$rhash->{'01'};#不推荐 say %$rhash;#打印散列表所有元素 #遍历散列表 foreach (sort keys %$rhash) { say "foreach遍历散列表:$_ : ${$rhash}{$_}"; } while(($k,$v)=each(%$rhash)){ say "while-each遍历散列表:$k:$v"; } say "----------------子程序引用demo-------------------------"; sub method{ my($v) = @_; say "i am $v..."; } $rm = \&method; #对有名子程序的引用 $rn = sub{ #对匿名子程序的引用 my($v) = @_; say "i am $v method..."; }; #两种调用子程序方式 &$rm("method"); &$rn("noname"); $rm->("method"); $rn->("noname"); say "----------------子程序传递多个数组(需要利用引用)-------------------------"; @array1 = qw(one two three four); @array2 = qw(100 200 300 400); &print_array(\@array1,\@array2); sub print_array{ my($a1,$a2) = @_; $i = 0; foreach (@$a1) { say "遍历:$i:$$a1[$i] \t $i:@$a2[$i]"; $i++; } } say "----------------文件句柄引用(将一条信息发送到多个文件)--------------------------------------------"; use utf8; #文件句柄引用的语法为\*FILEHANDLE &print_Msg(\*STDOUT); &print_Msg(\*LPHANDLE); &print_Msg(\*LOGHANDLE); $app = open(F,">>log.txt"); &print_Msg(\*F); sub print_Msg{ my $taregt_file = shift; #say $taregt_file; #say $$taregt_file; say $taregt_file "the msg have send $$taregt_file ...."; } say '----------------------闭包demo-----------------------------------'; sub errorMsg{ my $lvl = shift; say $lvl; return sub{ my $msg = shift;#实际上内部保存了$lvl变量:my($lvl) = $lvl; say "$lvl:$msg"; } } $severe = errorMsg("AAA"); $fatal = errorMsg("BBB"); $annoy = errorMsg("CCC"); &$severe("aaaaaa"); &$fatal("bbbbbb"); &$annoy("cccccc"); say "----------------闭包迭代子偶数流demo---------------------------------------------"; sub print_even_number{ my($input) = @_; if($input % 2){ $input++; } my $rs = sub{ say "$input"; $input += 2; }; return $rs; } $iterator = print_even_number(27);#迭代器 for($i=0;$i<5;$i++){ &$iterator(); } say "-------print_even_odd_number-------"; sub print_even_odd_number{ my($rs1,$rs2); my ($input) = shift; $rs1 = sub{ if($input%2){ $input+=2; }else{ $input++; } say $input; }; return ($rs1,$rs2); } ($even_number,$odd_number) = print_even_odd_number(10); &$even_number(); #&$odd_number();