Perl Check Variable Exists: Using Defined Function

In Perl, you can check if a variable exists by using the defined function. This function checks if a variable has been assigned a value, returning true if it has, otherwise false.

如何有一个成功的职业生涯?-What are the keys to a successful career?

my $var;

if (defined $var) {
    print "变量已经存在\n";
} else {
    print "变量不存在\n";
}

In the example above, because the $var variable is not assigned a value, the defined function will return false and output “variable does not exist”.

bannerAds