In the present implementation of Perl (and probablybut you never knowin all future implementations ) there is a symbol table entry for each unique identifier in a package. That entry contains slots for one of each of the possible types of valuesscalar, array, hash, filehandle, and so on. To a certain extent, you can directly manipulate the contents of the symbol table. One way to do this is with a construct called a typeglob. A typeglob is an identifier preceded by an asterisk, for example, *a . It represents the symbol table entry that contains all of the different types of values stored under that identifier. A warning to the uninitiated: Typeglobs are generally considered to be an obscure feature. For that reason, you should avoid using them unnecessarily. Furthermore, you should avoid using typeglobs for tasks that could be handled with references (see Item 30), because references are far more efficient. With that admonition out of the way, let's move on. You can use typeglobs to alias names :
Typeglobs can be localized:
A similar effect, but with run-time symbol table lookup, is available using the symbol table hash directly. Here is an example that manipulates the main package's symbol table:
You can pass typeglobs as arguments to subroutines, or store them like scalar values:
You also can alias a single kind of variable, such as an array or subroutine only, by assigning a reference of the appropriate type to a typeglob:
You can use typeglobs to localize filehandles and directory handles (see Item 26):
You also can use typeglobs in places in which you would ordinarily use references (but avoid doing so unless you have to):
A recent addition to the language is the *FOO{BAR} or " typeglob subscript" syntax, which allows you to extract individual references from a typeglob:
Many of the things typeglobs were once used for now can be done more sensibly with references (see Item 30), packages, and/or object-oriented programming. However, you're likely to encounter them from time to time in older Perl code, so you should be aware of what they look like and what they do. |