珀尔 |定义()函数
如果提供的变量 'VAR' 的值不是 undef 值,则 Perl 中的Defined()返回 true,或者如果未指定 VAR,它会检查 $_ 的值。这可以与许多函数一起使用来检测操作失败,因为如果出现问题,它们会返回 undef。
如果 VAR 是函数或函数函数函数,则返回 false。如果指定了散列元素,则如果已定义相应的值,则返回 true,但不检查散列中是否存在键
Syntax: defined(VAR)
Parameters:
VAR which is to be checked
Returns:
Returns 0 if VAR is undef and 1 if VAR contains a value
示例 1:
#!/usr/bin/perl
# Defining a variable
$X = "X is defined";
# Checking for existence of $X
# with defined() function
if(defined($X))
{
print "$X\n";
}
# Checking for existence of $Y
# with defined() function
if(defined($Y))
{
print "Y is also defined\n";
}
else
{
print "Y is not defined\n";
}
输出:
X is defined
Y is not defined
示例 2:
#!/usr/bin/perl
# Defining a function
sub X
{
# Defining a variable
$VAR = 20;
}
# Checking for existence of $X
# with defined() function
if(defined(X))
{
print "Function Exists\n";
}
# Checking for existence of $Y
# with defined() function
if(defined($Y))
{
print "Y is also defined\n";
}
else
{
print "Y is not defined\n";
}
输出:
Function Exists
Y is not defined