If you declare a variable using my but don't give it a value
it still has a special value called undef.
By using the defined keyword one can tell if a
scalar value has any real value or it is undef.
As we will see later this undef value is also used to indicate some
kind of a failure.
The usage would look like this:
my $x;
if (defined $x) {
say "It has a value";
}
Sometimes the opposite case is what needs some action so we will use
the not operator to negate the trueness of the statement:
if (not defined $x) {
say "Do something as x is not yet defined";
}
If you are interested in on-site trainings by the author, please
contact me directly.
|