功能 defined() Perl

在Perl编程语言都有一个内置的功能 defined().

大多数情况下这种特征是用于区别 值 undef 从任何其他价值。 但是,即使有帮助的 defined() ,你可以了解你是否有定义的一个函数。

检查 undef

主要应用功能 defined() 个检查价值 undef.

如果传输功能 defined()undef,该功能将返回错误的。 该功能将返回'true'如果值其它转 是什么,但 undef. 这里是一个例子码:

▶ Run
#!/usr/bin/perl

use Data::Dumper;

print Dumper defined(8);
print Dumper defined(0);
print Dumper defined('');
print Dumper defined(undef);

结果这种代码:

$VAR1 = 1;
$VAR1 = 1;
$VAR1 = 1;
$VAR1 = '';

检查功能定义

大多数情况下,功能的 defined() 是用以区分 undef 从另一个价值。 但是,除了使用 defined() ,你可以了解你是否有定义的一个函数。 这里是一个例子码:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

print Dumper defined(&check);
print Dumper defined(&Data::Dumper::Dumper);

功能 check() 是没有定义,因此 defined() 返回是假的,但该功能 Dumper 一束 Data::Dumperdefined() 返回'true'。 这是什么这个程序将显示在屏幕上:

$VAR1 = '';
$VAR1 = 1;

参数

如果功能 defined() 不会给没有参数、功能适用 的可变 $_:

如果传输功能 defined() 多个参数,它将是一个错误和程序的执行 将停止:

Too many arguments for defined operator at script.pl line 3, near "3)"
Execution of script.pl aborted due to compilation errors.

回值

该功能的结果 defined() 始终是一个布尔的价值。 真正的或虚假的。

使用了一系列和Hesham

非常长的功能 defined() 的工作中一个特殊的方式如果你告诉她 一系列或哈希作为参数。 但在Perl5.8这种使用 发出警告,因为5.22这种使用开始失败。

这里是一样的代码,在 defined() 是通过一系列:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my @arr;

print Dumper defined(@arr);

结果这种代码在perl版5.8:

defined(@array) is deprecated at script.pl line 10.
    (Maybe you should just omit the defined()?)
    $VAR1 = '';

这样的结果时,下午5时30分perl:

Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at script.pl line 10.

和同样的代码,但是关于哈希:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my %h;

print Dumper defined(%h);

这项工作的结果在perl5.8和下午5时30分:

defined(%hash) is deprecated at script.pl line 10.
    (Maybe you should just omit the defined()?)
    $VAR1 = '';
Can't use 'defined(%hash)' (Maybe you should just omit the defined()?) at script.pl line 10.

官方文件

这里是输出的命令 perldoc -f defined:

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

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

               You may also use "defined(&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 an
               "AUTOLOAD" method that makes it spring into existence the first
               time that it is called; see perlsub.

               Use of "defined" 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 overuse "defined" and are then
               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, 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 use
               "defined" only when 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 "undef", "exists", "ref".

相关的主题

其他文章