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().

my $carp; *SomePackage::carp = sub { $carp = join '', @_ };

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.

$ perl -e '*::foo = sub{3};print foo()' 3

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).

$ perl -e 'sub foo(){2};*::foo=sub(){3};print foo()'

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.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to Little Perl Mysteries: what's your answer? by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.