Chinaunix首页 | 论坛 | 博客
  • 博客访问: 340577
  • 博文数量: 85
  • 博客积分: 1420
  • 博客等级: 上尉
  • 技术积分: 787
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-10 09:02
文章分类

全部博文(85)

文章存档

2011年(5)

2010年(51)

2009年(29)

我的朋友

分类:

2010-04-20 16:34:52

exist

[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

| |
  • defined EXPR

  • defined

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

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

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

    Use of 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:

    1. if (@an_array) { "has array elements\n" }
    2. if (%a_hash) { "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 for the latter purpose.

    Examples:

    1. if $switch{'D'};
    2. "$val\n" while ($val = (@ary));
    3. "Can't readlink $sym: $!"
    4. unless ($value = $sym);
    5. sub foo { &$bar ? &$bar(@_) : "No bar"; }
    6. $debugging = 0 unless $debugging;

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

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

    The pattern match succeeds, and $1 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 only when you're questioning the integrity of what you're trying to do. At other times, a simple comparison to 0 or "" is what you want.

    See also , , .

exists

| |
  • 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.

    1. "Exists\n" if $hash{$key};
    2. "Defined\n" if $hash{$key};
    3. "True\n" if $hash{$key};
    4. "Exists\n" if $array[$index];
    5. "Defined\n" if $array[$index];
    6. "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 method that makes it spring into existence the first time that it is called -- see .

    1. "Exists\n" if &subroutine;
    2. "Defined\n" if &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:

    1. if ( $ref->{A}->{B}->{$key}) { }
    2. if ( $hash{A}{B}{$key}) { }
    3. if ( $ref->{A}->{B}->[$ix]) { }
    4. if ( $hash{A}{B}[$ix]) { }
    5. if ( &{$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"} and $ref->{"A"}->{"B"} will spring into existence due to the existence test for the $key element above. This happens anywhere the arrow operator is used, including even:

    1. $ref;
    2. if ( $ref->{"Some key"}) { }
    3. $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.

    1. ⊂ # OK
    2. &sub(); # Error
阅读(1361) | 评论(0) | 转发(0) |
0

上一篇:SQL游标

下一篇:kill 信号列表

给主人留下些什么吧!~~