Friday, January 14, 2011

Perl's undef function


he undefine function (undef) undefines a scalar variable, an array element or an entire array. If you inhale a large table into an array, undef can be used to clear the array and free up system memory. For example:
open(COUNT,"/path/to/count/file.txt") || die "Can't Open Count Data File: $!\n"; 
@count = <COUNT>;
close(COUNT);
you can later do
undef (@count);
to free up memory resources. However
undef
is best for undefining a single element of a hash array. For example, if you load $hasharray{'element10'} you can do
undef $hasharray{'element10'}

to completely remove the element from the array. Just don't do this if you are iterating over the hash.
undef
does have two other uses that are fairly arcane.

No comments: