For the most part, Perl's garbage collection will handle discarding variables and cleaning up for you automatically. As you suggest, if you're mindful of the imminenet death of a variable scope, don't worry about calling
undef.
With that in mind, there are a couple of places where you might want to massage things by hand.
- When you have a circular data structure, like a doubly linked list or some other monstrosity. As the gc works by reference counting, if you have two objects that only point to each other, they'll still stick around. Break the links or undefine one or both objects.
- When you won't be leaving the scope for a while, but you have an item using lots of memory. One example would be performing some sort of transformation on a large data file. If you must do it all in memory (for speed reasons) but you copy it from one scalar to another, undef the first scalar as soon as possible.
The first is the only one I'd really worry about -- with some thoughtful programming, the second may never come up. I really don't worry about it much, as wise coding tends to minimize memory usage anyway.