关于perl的ref 函数
来源:互联网 发布:大淘客cms优化教程 编辑:程序博客网 时间:2024/05/17 02:39
原文链接:http://blog.csdn.net/lixueping861111/article/details/5874266
我们都知道perl有引用的概念:一组数据实际上是另一组数据的引用。这些引用称为指针,第一组数据中存放的是第二组数据的头地址。引用的方式被用得相当普遍,特别是在面向对象的模块、函数的参数传递等常见。但perl对每个引用都是以一个普通的变量来定义的,有时候,如果数据的架构比较复杂,我们可能会困惑于某个变量所指向的地址的实际内容是什么?perl的ref函数就可以帮助我们。
一、说明
从perl自带的帮助说明可以了解相关的用法:
引用
$ 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.
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.
二、举例
简单来说,就是如果一个变量是个引用,那ref就可以返回一个表示其实际引用对象的描述性字符串,否则就会返回空值。如果没有指定ref函数的参数,默认对$_变量操作。如果被引用的对象已经被打包,则会返回该包的名称,类似typeof操作符。
代码:
#!/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";
%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.
Jerry is Female
Tom is Male
href is a reference to a hash.
href is HASH.
- 关于perl的ref 函数
- 关于perl的ref 函数
- perl的ref 函数(转贴)
- perl中,的bless,ref函数
- perl中,的bless,ref函数
- 好用的Perl包 Class::Ref
- perl中关于字符串的一些函数
- 关于perl中Exit的函数使用
- 关于理解Perl的fork函数
- 关于perl中的sleep函数
- 关于使用Perl map函数和命令行解析的技巧
- 关于PERL的数据库
- 关于ref
- 关于ref
- ref函数
- Perl的replace函数
- Perl 的函数原型
- perl 的select 函数
- C语言的计算耗时问题
- 关于Asp.net mvc框架下的自带数据库sql Server的发布相关问题
- cocos2d场景切换效果
- 理解RESTful架构
- cocos2d旋转按钮
- 关于perl的ref 函数
- Linux与Windows线程机制简单比较
- openlayers select-feature show polygon dimension label
- IsWindow函数不适用于新桌面
- cocos2d循环播放动画
- 进程间通信——共享的内存
- 新算法之友情链接有讲究
- a
- cat小解