in reply to Trying to understand aliasing (*var)

I know of one particular example of where typeglobs are useful, that was shown to me by ChemBoy. One of his colleagues had a variable called %SEPERATOR and ChemBoy kept "mis"-typing it because it was spelled wrong. Rather than do a global search-and-replace on the source code, which is always dangerous, he wrote:
use vars '%SEPARATOR'; *SEPARATOR = \%SEPERATOR;
and went on his merry way using %SEPARATOR in the rest of his code.

Replies are listed 'Best First'.
Re^2: Trying to understand aliasing (*var)
by chromatic (Archbishop) on Feb 11, 2005 at 18:41 UTC
    Rather than do a global search-and-replace on the source code, which is always dangerous

    Run the tests before, make the change, and run the tests after. Fix what breaks. It's pretty easy.

      Depends on the size of your code and how clean it is. IIRC, in this case the code was fairly old and poorly organized, with references to package globals spread across an unpleasantly large number of source files. I've never seen the code, but this is my strong impression. Though I am a strong supporter of good spelling among developers and people in general, I think in this case the risk-reward analysis didn't favor changing the existing code.

      In the above example, I think it's more reasonable to get the official correct spelling, and have everyone learn a bit of spelling, than to use aliasing.

      In the general case, you're assuming that a) there are tests to run, and, more importantly, b) that the tests have 100% coverage. While there may not be a lot of pity for the fool (apologies to Mr. T) in the first situation, the latter is much more realistic. Getting to 100% coverage is difficult, if not actually impossible once you've released a module and others may be using it beyond your ability to test/modify. Sometimes the alias is fixing what breaks. Then again, in this situation, search-and-replace doesn't really work, either.