Perl--函数引用
发布时间:2020-12-15 23:49:58 所属栏目:大数据 来源:网络整理
导读:函数的引用: $ref=func; (func的定义在其他位置),不要(),当func()时为执行函数,返回值再引用。当func()时为执行函数,返回值再引用。[oracle@jhoa 3]$ cat 3.pl sub generate_greeting { my($greeting) = "hello world"; return sub {print $greeting
函数的引用: $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 $rsn"; ##使用函数 &$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]$ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |