Little Perl Mysteries: what's your answer?by Ovid (Cardinal)
|on Oct 30, 2002 at 21:15 UTC||Need Help??|
I was reading an article about how Perl behaves internally when I realized I can create some very simple code that does not produce the output I expect. Consider a typeglob (I know, you don't want to, but bear with me). A typeglob is merely an identifier, preceded by an asterisk that refers to a slot in a symbol table. This 'slot' refers to all possible types of the identifier (hence the term "typeglob"). So the typeglob *main::foo refers to, amongst other things, $main::foo, %main::foo, &main::foo, etc. Lexically scoped variables are not in typeglobs (though a reference to one might be), only package variables. Also, note that if you're referring to the %main:: namespace, the "main" is optional; $::foo is the same as $main::foo.
I use typeglobs quite a bit when I'm writing test suites so that I can override the behavior of a subroutine in my code. For example, I might override the behavior of carp().
That now allows me to test when carp() has been called and test if the error message is appropriate. Reducing this to a one-liner, we can see how defining a subroutine via a typeglob works.
Now that's a pretty nifty trick that many people like, many dislike, and I'll just stay out of that debate. Now, here's the question. What does the following print and why does it print it? Fair warning, I have not given you enough information to answer this. (and I can easily see this stunt being abused in obfuscations).
I'll post a link later to the article that I mentioned. Also, if you know the answer, wait a while before responding, or post it with a spoiler warning with black on black.
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.