In Perl v5.12, length(undef) returns undef.

Starting with v5.12, the length of an undefined value is undefined itself. Instead of converting the undef to the empty string then taking the length of that string, v5.12 returns undef:


$ perl5.10.1 -le 'print defined length(undef) ? "Defined" : "undef"'
Defined
$ perl5.12.1 -le 'print defined length(undef) ? "Defined" : "undef"'
undef

When you use warnings, you see one problem. Prior to v5.12, the conversion of the undef issues a warning:

$ perl5.10.1 -wle 'print defined length(undef) ? "Defined" : "undef"'
Use of uninitialized value in length at -e line 1.
Defined
$ perl5.12.1 -wle 'print defined length(undef) ? "Defined" : "undef"'
undef

To avoid the warning, you have to use a guard condition for $string before you call length:

my $string = undef;

if( defined $string and length $string ) { ... }

With v5.12, you don’t need the guard condition:

use v5.12;

my $string = undef;

if( length $string ) { ... }  # no problem

This is likely what you want since an undefined value and an empty string both skip the code you want to run for a non-zero length string. If you want to differentiate those conditions, you still have to do some more work. With v5.12, you can put the most common case (I’m guessing) first. You test for a non-zero string in the first branch:

use v5.12;

my $string = undef;

if( length $string )     { ... }
elsif( defined $string ) { ... } # the 0-length case
else                     { ... } # the undef case

Prior to v5.12, you would put the uncommon case first, meaning that it runs and is skipped for most cases:

my $string = undef;

if( ! defined $string ) { ... } # the undef case
elsif( length $string ) { ... } # common case
else                    { ... } # the 0-length case