📜  珀尔 | undef 和定义的函数

📅  最后修改于: 2022-05-13 01:54:35.974000             🧑  作者: Mango

珀尔 | undef 和定义的函数

undef用于那些没有任何赋值的变量。可以将其与 NULL(在Java、 PHP等中)和 Nil(在 Ruby 中)进行比较。所以基本上当程序员声明一个标量变量并且不给它赋值时,那么变量应该包含undef值。在 Perl 中,如果变量的初始值是 undef,那么它将不打印任何内容。
例子:

Perl
# Perl program for undef variable
# variable which declared without
# initial value
 
# variable x without initial value
my $x;
 
# printing its value
print "The value of x is = ${x}";


Perl
# Perl program to illustrate
# the undef() function
 
# a variable with initial value 10
$k = 10;
 
# displaying its initial value
print "The value of variable k is ${k}\n";
 
 
# undef the variable
undef $k;
 
# value after the undef function
print "The value of variable k is ${x}\n";
 
# a variable with initial value 20
$m = 20;
 
# displaying its initial value
print "The value of variable m is ${m}\n";
 
# undef the variable
# you can also use
# $m = undef;
$m = undef();
 
# value after the undef function
print "The value of variable m is ${m}\n";


Perl
# Perl program to illustrate
# the defined() function.
 
# a variable assigned value 15
$k = 15;
 
# To check if variable is defined or not
if (defined $k)
{
     print "k is defined\n";
}
else
{
    print "k is not defined\n";
}
 
 
# undef the variable
$k = undef;
 
# To check if the variable is defined or not
if (defined $k)
{
    print "k is defined\n";
}
else
{
    print "k is not defined\n";
}


Perl
# Perl program to demonstrate behaviour
# of undef variables, during operation
# like arithmetic, concatenation etc.
 
# first variable assigned with value
$x = 125;
 
# second variable with no value
my $y;
 
# arithmetic operation
$z = $x + $y;
 
# displaying sum
print "The sum of x and y is ${z}\n";
 
# declaring strx and assigned a value to it
$strx = "GFG";
 
# declaring stry without assigned value
my $stry;
 
# string concatenation
$strz = $strx.$stry;
 
# after concatenation
print "After concatenation of strx and stry  ${strz}\n";


输出:

The value of x is = 

undef()函数: undef用于重置任何变量的值。它可以带或不带括号使用。这意味着在使用此函数时括号是可选的。

  • 例子:

Perl

# Perl program to illustrate
# the undef() function
 
# a variable with initial value 10
$k = 10;
 
# displaying its initial value
print "The value of variable k is ${k}\n";
 
 
# undef the variable
undef $k;
 
# value after the undef function
print "The value of variable k is ${x}\n";
 
# a variable with initial value 20
$m = 20;
 
# displaying its initial value
print "The value of variable m is ${m}\n";
 
# undef the variable
# you can also use
# $m = undef;
$m = undef();
 
# value after the undef function
print "The value of variable m is ${m}\n";
  • 输出:
The value of variable k is 10
The value of variable k is 
The value of variable m is 20
The value of variable m is 

defined()函数:该函数用于检查是否为变量赋值。如果将值分配给变量,它将返回 True,否则将返回 false。

  • 句法:
defined $variable_name
  • 例子:

Perl

# Perl program to illustrate
# the defined() function.
 
# a variable assigned value 15
$k = 15;
 
# To check if variable is defined or not
if (defined $k)
{
     print "k is defined\n";
}
else
{
    print "k is not defined\n";
}
 
 
# undef the variable
$k = undef;
 
# To check if the variable is defined or not
if (defined $k)
{
    print "k is defined\n";
}
else
{
    print "k is not defined\n";
}
  • 输出:
k is defined
k is not defined

注意:虽然未定义的变量没有定义的值,但它们在运行过程中可以取空值。例如,如果用户添加两个变量 x 和 y,其中 x 为 5,y 未定义,则结果将保持为 5,因为 y 将占用值 0。类似地,如果用户将两个字符串strx 和 stry 与 strx 连接为值“GGF”并且stry是undef,结果将是“GFG”,因为stry取空字符串“”的值。

  • 例子:

Perl

# Perl program to demonstrate behaviour
# of undef variables, during operation
# like arithmetic, concatenation etc.
 
# first variable assigned with value
$x = 125;
 
# second variable with no value
my $y;
 
# arithmetic operation
$z = $x + $y;
 
# displaying sum
print "The sum of x and y is ${z}\n";
 
# declaring strx and assigned a value to it
$strx = "GFG";
 
# declaring stry without assigned value
my $stry;
 
# string concatenation
$strz = $strx.$stry;
 
# after concatenation
print "After concatenation of strx and stry  ${strz}\n";
  • 输出:
The sum of x and y is 125
After concatenation of strx and stry  GFG