Perl--函数引用

函数的引用:       $ref=&func;   (func的定义在其他位置),不要(),当&func()时为执行函数,返回值再引用。

当&func()时为执行函数,返回值再引用。

[oracle@jhoa 3]$ cat 3.pl 
sub generate_greeting { my($greeting) = "hello world"; 
return sub {print $greeting;print "goodbye"}; }
##引用
 $rs = generate_greeting();
print "$rs is $rs
";
##使用函数
 &$rs; 

[oracle@jhoa 3]$ perl 3.pl 
$rs is CODE(0x61c52a0)
hello worldgoodbye[oracle@jhoa 3]$ 


引用的使用;

简单变量:$$ref  ${$ref}
数组:@$ref @{$ref};  元素:$$ref[0]    $ref->[0];

散列:%$ref  %{$ref};   元素:$$ref{‘a’} $ref->{‘a’};

函数:&$ref(a,b);   $ref->(a,b)

文件:$ref

函数的引用:       $ref=&func;   (func的定义在其他位置),不要(),当&func()时为执行函数,返回值再引用。


[oracle@jhoa 3]$ cat 3.pl 
sub generate_greeting { my($greeting) = "hello world"; 
return sub {print $greeting}; } $rs = generate_greeting();
 &$rs(); 
 &$rs; 

[oracle@jhoa 3]$ perl 3.pl 
hello worldhello world[oracle@jhoa 3]$ 

原文地址:https://www.cnblogs.com/hzcya1995/p/13351960.html