in reply to Confused about typeglobs and references
Your questions been answered, but here's a bit more background information as to what's happening.
A typeglob is merely a value in a symbol table that points to package variables of the same name. For each typeglob, there are 6 slots (did I miss one? I keep thinking I missed one). For example, in package %SomePackage::, if we refer to *SomeVar, we're referring to a typeglob with the following slots:
Symbol Table Typeglob Values
%SomePackage:: *SomeVar $SomeVar (scalar)
@SomeVar (array)
%SomeVar (hash)
SomeVar (format)
SomeVar (file handle)
&SomeVar (code)
In the above little diagram, you can refer to the scalar as $SomePackage::SomeVar.
For Perl to access a package variable, it must look up the typeglob in the appropriate symbol table and then check to see if the appropriate slot for the appropriate variable type has a value.
The local keyword merely takes the value of any package variable (which, of course, is a slot in a typeglob), saves the value, let's you reassign a value to that package variable and then restores the saved value when the scope exits. What this means is that you cannot use local on a lexically scoped variable (unless you predeclare it with our, but that's a lexically scoped package variable and will just make your head hurt). In other words, if you declare a variable with my, you don't use local on it. However, even with package variables you rarely want to use local unless you are either:
Note: like using strict, there are times to break these rules, but don't do that unless you can clearly explain why you shouldn't break them :)
Which brings us back to your original question of whether or not you can use that typeglob trick with strict. If you read further in the "Shiny Ball" book, you'll see that passing typeglobs in the manner described was used in older versions of Perl where references didn't exist. Now we have references and unless you're a Damian Conway or a related construct, I would not use this syntax. Use explicit references instead.
Cheers,
Ovid
Join the Perlmonks Setiathome Group.
New address of my CGI Course.
|
|---|