Table of Contents

Namespaces

We cannot use the variable names $height and $ratio because we modify them in the subroutine and that will affect the main program. So we choose new ones to do the operation on. Finally, a small change is made to the print output.

This approach works well enough for our small program here. For larger programs, having to think of new variable names all the time is difficult. It would be even more difficult if different programmers were working on different sections of the program. It would be impossible if a program were written, then an extension created by another person somewhere else, and that same extension had to be used by many people in many different programs. Obviously, the risk of using the same variable name is too great. There are only so many logical names out there.

There is a solution. Imagine you own a house with two gardens. You have two identical dogs, one in the front garden, one in the back garden. Bear with me, this is relevant. Both dogs are called Rover, because their owner lacks imagination.

When you go to the front garden and call 'Rover!!!' or open a can of dog food, the dog in the front garden comes running. Similarly, you go to the back garden, call your dog and the other dog bounces up to you.

You have two dogs, both called Rover, and you can change either one of them. Wash one, neuter the other -- it doesn't matter, but both are dogs and both have the same name. Changes to one won't affect the other. You don't get them confused because they are in different places, in two different namespaces.