Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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().

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-16 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found