in reply to testing defined($x) && length($x)

Sure:

if (has_length($x)) { ... }

Implementation of has_length left to the reader.