As mobrule indicates, you could use the following instead for a small savings:
if (defined $name && $name ne '') {
# do something with $name
}
You could ditch the defined check and get something even shorter, e.g.:
if ($name ne '') {
# do something with $name
}
But in the case where $name
is not defined, although the logic flow will work just as intended, if you are using warnings
(and you should be), then you'll get the following admonishment:
Use of uninitialized value in string ne
So, if there's a chance that $name
might not be defined, you really do need to check for definedness first and foremost in order to avoid that warning. As Sinan Ünür points out, you can use Scalar::MoreUtils to get code that does exactly that (checks for definedness, then checks for zero length) out of the box, via the empty()
method:
use Scalar::MoreUtils qw(empty);
if(not empty($name)) {
# do something with $name
}