Chapter 7. Symbol Tables and Typeglobs
Although I donât normally deal with typeglobs or the symbol table, I need to understand them for use in later chapters. Iâll lay the foundation for advanced topics including dynamic subroutines and jury-rigging code in this chapter. Symbol tables organize and store Perlâs package (global) variables, and I can affect the symbol table through typeglobs. By messing with Perlâs variable bookkeeping I can do some powerful things. Youâre probably already getting the benefit of some of these tricks without even knowing it.
Package and Lexical Variables
Before I get too far, I want to review the differences between package and lexical variables. The symbol table tracks package variables but not lexical variables. When I fiddle with the symbol table or typeglobs, Iâm dealing with package variables. Package variables are also known as global variables since they are visible everywhere in the program.
In Learning Perl and Intermediate
Perl, we used lexical variables whenever possible. We declared
lexical variables with my
, and those
variables could only be seen inside their scope. Since lexical variables have limited reach, I didnât
need to know all of the program to avoid a variable name collision.
Lexical variables are a bit faster too, since Perl doesnât have to deal
with the extra bookkeeping of the symbol table.
Lexical variables have a limited scope, and they affect only the part of the program where I define them. This little snippet ...
Get Mastering Perl, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.