Movatterモバイル変換


[0]ホーム

URL:


functions /defined
(source,CPAN)
You are viewing the version of this documentation from Perl 5.14.1.View the latest version
#defined EXPR
#defined

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

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

You may also usedefined(&func) to check whether subroutine&func has ever been defined. The return value is unaffected by any forward declarations of&func. A subroutine that is not defined may still be callable: its package may have anAUTOLOAD method that makes it spring into existence the first time that it is called; seeperlsub.

Use ofdefined on aggregates (hashes and arrays) is deprecated. It used to report whether memory for that aggregate had 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 overusedefined and are then surprised to discover that the number0 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, although 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 usedefined only when questioning the integrity of what you're trying to do. At other times, a simple comparison to0 or"" is what you want.

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

Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.

The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.


[8]ページ先頭

©2009-2026 Movatter.jp