perl中的defined和exists

perl中的defined和exists,第1张

概述From: http://hi.baidu.com/wangj998/blog/item/3a8d63e9b5b7c035b90e2d3b.html   exists     [root@localhost~]# cat 1.pl #!/usr/bin/perl -w use strict; my $a; my $b=""; my $c=1; print "a ok/n" if ( defined

From: http://hi.baIDu.com/wangj998/blog/item/3a8d63e9b5b7c035b90e2d3b.HTML

 

exists

 

 

[root@localhost~]# cat 1.pl
#!/usr/bin/perl -w
use strict;
my $a;
my $b="";
my $c=1;
print "a ok/n" if ( defined $a );
print "b ok/n" if ( defined $b );
print "c ok/n" if ( defined $c );

my %hash=(
        'aa' => 'bejing',
        );

if ( exists $hash{'aa'} )
{
        print "aa exists/n";
}
else
{
        print "aa not exists/n";
}

结果:

[root@localhostr ~]# perl -w 1.pl
b ok
c ok
aa exists
# a 没有赋值过值,所以是undef ,b赋值为空,空也是赋值,c赋值,哈希存在aa值

 

 

defined是用来测试一个变量是否是undef的,也就是说这个变量一定有,只是不知道对这个变量赋过值没有

exists一般是用来测试hash表中是否存在一个变量的

exists判断散列或者数组中某值是否存在,defined判断一个值是不是undef。
存在的也有可能是undef。

defined 一般用来判断变量是非赋值或文件是否都结尾,exists 判断数组哈希是否存在某个变量

 

 

其他网络参考

defined Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage defined EXPR

defined

Returns a Boolean value telling whether EXPR has a value other than the undefined value undef @H_301_102@ . If EXPR is not present,$_ @H_301_102@ will be checked.

Many operations return undef @H_301_102@ to indicate failure,end of file,system error,uninitialized variable,and other exceptional conditions. This function allows you to distinguish undef @H_301_102@ from other values. (A simple Boolean test will not distinguish among undef @H_301_102@,zero,the empty string,and "0" @H_301_102@,which are all equally false.) Note that since undef @H_301_102@ is a valID scalar,its presence doesn't necessarily indicate an exceptional condition: pop @H_301_102@ returns undef @H_301_102@ when its argument is an empty array,or when the element to return happens to be undef @H_301_102@ .

You may also use defined(&func) @H_301_102@ to check whether subroutine &func @H_301_102@ has ever been defined. The return value is unaffected by any forward declarations of &func @H_301_102@ . Note that a subroutine which is not defined may still be callable: its package may have an autoLOAD @H_301_102@ method that makes it spring into existence the first time that it is called -- see perlsub .

Use of defined @H_301_102@ on aggregates (hashes and arrays) is deprecated. It used to report whether memory for that aggregate has ever been allocated. This behavior may disappear in future versions of Perl. You should instead use a simple test for size:

if (@an_array) { print "has array elements/n" } if (%a_hash) { print "has hash members/n" }

When used on a hash element,it tells you whether the value is defined,not whether the key exists in the hash. Use "exists" for the latter purpose.

Examples:

print if defined $switch{'D'}; print "$val/n" while defined ($val = pop (@ary)); die "Can't readlink $sym: $!" unless defined ($value = readlink $sym); sub foo { defined &$bar ? &$bar(@_) : die "No bar"; } $deBUGging = 0 unless defined $deBUGging;

Note: Many folks tend to overuse defined @H_301_102@,and then are surprised to discover that the number 0 @H_301_102@ and "" @H_301_102@ (the zero-length string) are,in fact,defined values. For example,if you say

"ab" =~ /a(.*)b/;

The pattern match succeeds,and @H_301_102@ is defined,despite the fact that it matched "nothing". It dIDn't really fail to match anything. Rather,it matched something that happened to be zero characters long. This is all very above-board and honest. When a function returns an undefined value,it's an admission that it Couldn't give you an honest answer. So you should use defined @H_301_102@ only when you're questioning the integrity of what you're trying to do. At other times,a simple comparison to 0 @H_301_102@ or "" @H_301_102@ is what you want.

See also "undef" , "exists" , "ref" .

exists Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage exists EXPR

Given an Expression_r_r_r that specifIEs a hash element or array element,returns true if the specifIEd element in the hash or array has ever been initialized,even if the corresponding value is undefined.

print "Exists/n" if exists $hash{$key}; print "defined/n" if defined $hash{$key}; print "True/n" if $hash{$key}; print "Exists/n" if exists $array[$index]; print "defined/n" if defined $array[$index]; print "True/n" if $array[$index];

A hash or array element can be true only if it's defined,and defined if it exists,but the reverse doesn't necessarily hold true.

Given an Expression_r_r_r that specifIEs the name of a subroutine,returns true if the specifIEd subroutine has ever been declared,even if it is undefined. Mentioning a subroutine name for exists or defined does not count as declaring it. Note that a subroutine which does not exist may still be callable: its package may have an autoLOAD @H_301_102@ method that makes it spring into existence the first time that it is called -- see perlsub .

print "Exists/n" if exists &subroutine; print "defined/n" if defined &subroutine;

Note that the EXPR can be arbitrarily complicated as long as the final operation is a hash or array key lookup or subroutine name:

if ( exists $ref->{A}->{B}->{$key}) { } if ( exists $hash{A}{B}{$key}) { } if ( exists $ref->{A}->{B}->[$ix]) { } if ( exists $hash{A}{B}[$ix]) { } if ( exists &{$ref->{A}{B}{$key}}) { }

Although the deepest nested array or hash will not spring into existence just because its existence was tested,any intervening ones will. Thus $ref->{"A"} @H_301_102@ and $ref->{"A"}->{"B"} @H_301_102@ will spring into existence due to the existence test for the $key element above. This happens anywhere the arrow operator is used,including even:

undef $ref; if ( exists $ref->{"Some key"}) { } print $ref; # prints HASH(0x80d3d5c)

This surprising autovivification in what does not at first--or even second--glance appear to be an lvalue context may be fixed in a future release.

Use of a subroutine call,rather than a subroutine name,as an argument to exists() is an error.

exists ⊂ # OK exists &sub(); # Error 总结

以上是内存溢出为你收集整理的perl中的defined和exists全部内容,希望文章能够帮你解决perl中的defined和exists所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1289181.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)