Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1512026
  • 博文数量: 289
  • 博客积分: 11086
  • 博客等级: 上将
  • 技术积分: 3291
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-22 17:06
个人简介

徐小玉的博客。

文章分类

全部博文(289)

文章存档

2023年(6)

2022年(1)

2021年(2)

2020年(9)

2019年(9)

2018年(6)

2017年(10)

2016年(10)

2014年(3)

2013年(4)

2011年(12)

2010年(16)

2009年(14)

2008年(119)

2007年(48)

2006年(20)

我的朋友

分类:

2008-03-21 16:11:47

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:

    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 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 , 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

    "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 , , .

uc

Returns an uppercased version of EXPR. This is the internal function implementing the escape in double-quoted strings. Respects current LC_CTYPE locale if use locale in force. See and for more details about locale and Unicode support. It does not attempt to do titlecase mapping on initial letters. See for that.

If EXPR is omitted, uses .

阅读(1267) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~