关于perl的ref 函数
发布时间:2020-12-16 00:12:15 所属栏目:大数据 来源:网络整理
导读:我们都知道perl有引用的概念:一组数据实际上是另一组数据的引用。这些引用称为指针,第一组数据中存放的是第二组数据的头地址。引用的方式被用得相当普遍,特别是在面向对象的模块、函数的参数传递等常见。但perl对每个引用都是以一个普通的变量来定义的,
我们都知道perl有引用的概念:一组数据实际上是另一组数据的引用。这些引用称为指针,第一组数据中存放的是第二组数据的头地址。引用的方式被用得相当普遍,特别是在面向对象的模块、函数的参数传递等常见。但perl对每个引用都是以一个普通的变量来定义的,有时候,如果数据的架构比较复杂,我们可能会困惑于某个变量所指向的地址的实际内容是什么?perl的ref函数就可以帮助我们。
引用
$ perldoc -tf ref
ref EXPR ref Returns a non-empty string if EXPR is a reference,the empty string otherwise. If EXPR is not specified,$_ will be used. The value returned depends on the type of thing the reference is a reference to. Builtin types include: SCALAR ARRAY HASH CODE REF GLOB LVALUE If the referenced object has been blessed into a package,then that package name is returned instead. You can think of "ref" as a "typeof" operator. if (ref($r) eq "HASH") { print "r is a reference to a hash./n"; } unless (ref($r)) { print "r is not a reference at all./n"; } See also perlref.
#!/usr/bin/perl -w
%hash=('Tom'=>'Male','Jerry'=>'Female'); $href=/%hash; for $key (keys %$href) { print $key." is ".$href->{$key}; print "/n"; } if ( ref($href) eq "HASH" ) { print "href is a reference to a hash./n"; } unless ( ref($href) ) { print "href is not a reference at all./n"; } print "href is ",ref($href),"./n";
引用
$ perl testref.pl Jerry is Female Tom is Male href is a reference to a hash. href is HASH.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |