![d3d6c6e59c22de6ab9f1e49a05ddf92d.png](https://img-blog.csdnimg.cn/img_convert/d3d6c6e59c22de6ab9f1e49a05ddf92d.png)
本文介绍Perl语言中 defined undef使用。
目录
欢迎微信随缘关注@pythonic生物人
1、undef
2、defined()函数
1、undef
指代完全未定义的, 是perl中变量未初始化时的默认值,这不等于字符串的空,不等于数值0,它是另一种类型;上下文需要数值的时候,undef为0;上下文需要字符串的时候,undef为空字符串。2、defined()函数
可判断一个变量是否被赋值。例如defined(
#!/usr/bin/perl
use strict;
use warnings;
my $word="";#被赋空值
if(defined($word)){#$word被赋空值,defined($word)返回1
print "0n$word";
}else{
print "1n$word";}
perl undef3.pl
0
例2,undef2.pl
#!/usr/bin/perl
use strict;
use warnings;
my $word;#没有赋值
if(defined($word)){#$word没被赋值,defined($word)返回0
print "0n$word";
}else{
print "1n$word";}
perl undef2.pl
1
欢迎微信随缘关注@pythonic生物人