in reply to Little Perl Mysteries: what's your answer?
Note: spoiler, black text on a black background. Select the text to read it. On some systems, you might have to paste it before it's actually visible. (And I put in some glow-in-the-dark hyperlinks *grin* (code may also be visible, if you defined a background color for it, in your PM CSS)). Update: Grumble. There's an HTML filter that filtered out the bgcolor of my <tr> tag. Putting it in <td> seems to be allowed.
robartes is right. (Note: I'll be using the words 'compile-time' and 'run-time', even though there is no clear distinction in Perl which is when.) The first definition is happening at compile-time, the second is at run-time. One might expect that in print foo(), a subroutine is called, and that that happens at run-time, thus using the new definition of foo(). But there is no subroutine call. Perl tries to be smart, and optimizes the code. The foo() has become a constant, the actual print that does happen is just print 2, as dada points out. podmaster was able to redefine the function effectively, because BEGIN blocks are executed as soon as possible (i.e. immediately), and redefinition takes place before run-time and before the foo() "call" is encountered. Other compile-time tricks would allow redefinition too, like having the code in a .pm file and using use. You get the expected 3 if you force Perl to actually call the subroutine . You can do that by using the & sigil. This disables prototype checking and inlining (probably because inlining depends on the prototype). Other ways of avoiding Perl to inline foo() are:
(I think I found a bug in my B::Deparse. perl -e'sub foo(){2}' doesn't even display the definition of foo, while dada's Deparse does display inlined subroutines. It's not as if the sub is completely gone: perl -le'sub foo(){2} print *foo{CODE}()' does work as expected. Perl 5.8.0 on linux x86. Please confirm.) |
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Little Perl Mysteries: what's your answer?
by Ovid (Cardinal) on Oct 31, 2002 at 17:06 UTC | |
by Juerd (Abbot) on Oct 31, 2002 at 19:13 UTC | |
Re^2: Little Perl Mysteries: what's your answer?
by Aristotle (Chancellor) on Oct 31, 2002 at 11:04 UTC |