How about
if (length ($name || '')) { # do something with $name}
This isn't quite equivalent to your original version, as it will also return false if $name
is the numeric value 0 or the string '0'
, but will behave the same in all other cases.
In perl 5.10 (or later), the appropriate approach would be to use the defined-or operator instead:
use feature ':5.10';if (length ($name // '')) { # do something with $name}
This will decide what to get the length of based on whether $name
is defined, rather than whether it's true, so 0/'0'
will handle those cases correctly, but it requires a more recent version of perl than many people have available.