Perl Programming Introduction to mod_perl (part 6): Even More Perl Basics |
You will hear a lot about namespaces, symbol tables and lexicalscoping in Perl discussions, but little of it will make any sensewithout a few key facts: Symbols, Symbol Tables and Packages; Typeglobs There are two important types of symbol: package global and lexical.We will talk about lexical symbols later, for now we will talk onlyabout package global symbols, which we will refer to simply asglobal symbols. The names of pieces of your code (subroutine names) and the names ofyour global variables are symbols. Global symbols reside in onesymbol table or another. The code itself and the data do not; thesymbols are the names of pointers which point (indirectly) to thememory areas which contain the code and data. (Note for C/C++programmers: we use the term `pointer' in a general sense of one pieceof data referring to another piece of data not in a specific sense asused in C or C++.) There is one symbol table for each package, (which is why globalsymbols are really package global symbols). You are always working in one package or another. Like in C, where the first function you write must be called main(),the first statement of your first Perl script is in package package mypackagename; From the following line you are in package When you create a symbol, Perl creates a symbol table entry for thatsymbol in the current package's symbol table (by default Most of the time, only one part of a typeglob is used (yes, it's a bitwasteful). You will by now know that you distinguish between them byusing what the authors of the Camel book call a funny character. Soif we have a scalar called ` Every global symbol is in some package's symbol table. To refer to aglobal symbol we could write the fully qualified name,e.g. Most of the time you do not need to use the fully qualified symbolname because most of the time you will refer to package variables fromwithin the package. This is very like C++ class variables. You canwork entirely within package The exception is when you import the variable from another package.This creates an alias for the variable in the current package, sothat you can access it without using the fully qualified name. Whilst global variables are useful for sharing data and are necessaryin some contexts it is usually wisest to minimise their use and uselexical variables, discussed next, instead. Note that when you create a variable, the low-level business ofallocating memory to store the information is handled automatically byPerl. The intepreter keeps track of the chunks of memory to which thepointers are pointing and takes care of undefining variables. When allreferences to a variable have ceased to exist then the perl garbagecollector is free to take back the memory used ready forrecycling. However perl almost never returns back memory it hasalready used to the operating system during the lifetime of theprocess. Lexical Variables and Symbols The symbols for lexical variables (i.e. those declared using thekeyword If you need access to the data from outside the package then you canreturn it from a subroutine, or you can create a global variable(i.e. one which has a package prefix) which points or refers to it andreturn that. The pointer or reference must be global so that you canrefer to it by a fully qualified name. But just like in C try to avoidhaving global variables. Using OO methods generally solves thisproblem, by providing methods to get and set the desired value withinthe object that can be lexically scoped inside the package and passedby reference. The phrase ``lexical variable'' is a bit of a misnomer, we are reallytalking about ``lexical symbols''. The data can be referenced by aglobal symbol too, and in such cases when the lexical symbol goes outof scope the data will still be accessible through the global symbol.This is perfectly legitimate and cannot be compared to the terriblemistake of taking a pointer to an automatic C variable and returningit from a function--when the pointer is dereferenced there will be asegmentation fault. (Note for C/C++ programmers: having a functionreturn a pointer to an auto variable is a disaster in C or C++; theperl equivalent, returning a reference to a lexical variable createdin a function is normal and useful.)
blog comments powered by Disqus |
|
|
|
|
|
|
|