Item 3: Don t assign undef when you want an empty list.


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:

 $toast = undef; 

It's toast.

 undef $history; 

It's history.

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.

 @still_going = undef; 

WRONG @still_going = (undef) .

 if (@still_going) { ... } 

Therefore this is TRUE.

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:

 @going_gone = ();  if (@going_gone) { ... } 

@going_gone = empty list, so scalar(@going_gone) = = FALSE.

 undef @going_gone;  if (defined(@going_gone)) { ... } 

Now it's really gone. FALSE

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.

 if (defined($a)) { ... }  if (defined(0)) { ... } 

TRUE if $a is not undef .

TRUE; error in Perl 4.

 if (defined(@a)) { ... }  if (defined(())) { ... } 

TRUE if @a is initialized .

TRUE; error in Perl 4.

You can assign undef to an element of an array:

 $puka[3] = undef;  @puka[1, 5, 7] = ();  @puka[0..99] = (); 

"Puka" is Hawaiian for "hole."

Create more holes.

100 copies of undef .

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 .

 @a = 1 .. 10;  $a[9] = undef;  print scalar(@a), "\n"; 

@a has 10 elements.

@a still has 10.

"10" (1..9, undef)

 $val = pop @a;  print scalar(@a), "\n"; 

@a now has 9 elements: (1..9) .

"9" (1..9)

 splice @a, -2;  print scalar(@a), "\n"; 

Splice off the last 2 elements.

"7" (1..7)

 $#a = 4;  print scalar(@a), "\n"; 

Shorten @a to 5 elements.

"5" (1..5)

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.

 %gone = ();  if (keys %gone) { ... } 

%gone now contains no keys.

FALSE

 %nuked = (U => '235', Pu => 238);  undef %nuked;  if (keys %nuked) { ... }  if (defined %nuked) { ... } 

%nuked has two key-value pairs.

"Nuked" itcompletely gone.

FALSE

Also FALSE, because %nuked is completely gone.

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.

 %spacers = (    husband => "george", wife => "jane",    daughter => "judy", son => "elroy"  ); 

Some sample data.

 delete $spacers{'husband'};  if (exists $spacers{'husband'}) { ... } 

husband/george is gone.

FALSE

 delete @spacers{'daughter', 'son'}; 

daughter/judy and son/elroy are gone.



Effective Perl Programming. Writing Better Programs with Perl
Effective Perl Programming: Writing Better Programs with Perl
ISBN: 0201419750
EAN: 2147483647
Year: 1996
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net