@my_array = undef;
if (@my_array ) {
print 'TRUE';
} else {
print 'FALSE';
}
This will print TRUE
Why does the array have an element ?
The array has an element because you assigned one. Consider the following:
@array = undef; # Assigns the value 'undef' to @array
@array = (); # Assigns the empty list to @array
undef @array; # Undefines @array
They look similar, but the first line is different from the other two (which are equivalent). The first line results in array with a single element (the value undef
). The other two result in an empty array. In Perl, undef
is both a value and an operator. The first line uses it as a value, the last line uses it as an operator.
It isn't usually necessary to clear an array. They are empty when declared:
my @array; # There's nothing in here, yet
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With