Item 3: Don't assign undef when you want an empty list.
Uninitialized scalar variables in Perl have the value undef . You can reset scalar variables to their "pristine" state by assigning undef to them, or by using the undef operator:
Uninitialized array variables, however, have the value () , the empty list. If you assign undef to an array variable, what you actually get is a list of one element containing undef :
Don't assign undef to an array variable.
The simplest way to avoid this is to assign the empty list () to array variables when you want to clear them. You can also use the undef verb:
The defined operator is the only way to distinguish undef from and the empty string '' . The defined operator will work on any valuein earlier versions of Perl it would work only on lvalues, but that is no longer the case.
You can assign undef to an element of an array:
Note that undef is a perfectly reasonable element value. You cannot shorten an array by assigning undef values to elements at the end of the array. To actually shorten an array without assigning a whole new value to it, you must assign to $#array_name or use one of the array operators like splice or pop .
Hashes and undef
The remarks above also apply to hashes. As with arrays, you cannot undef a hash by assigning undef to it. In fact, assigning any list with an odd number of elements to a hash results in a warning message (at least in newer versions of Perl). You can assign the empty list () to create an empty hash, or you can use the undef operator to reset the hash to a pristine state.
As with arrays, you cannot shorten or remove elements from a hash by assigning undef values to them. In order to remove elements from a hash you must use the delete operator. The delete operator can be used on hash slices as well as single elements:
Use delete to remove key-value pairs from hashes.