Avoid a warnings about undef used as a hash key

If the data structure contains an undef, it would generate a warning when
recording the seen items.  Besides avoiding putting undef in the hash,
we'll generalize and skip the loop unless it's a reference - regular
scalar values don't need to be further processed

Change-Id: Id99759a14d406c08d7efc3274b8fe391be7a86c5