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

perl -le'sub foo(){2};*::foo=sub(){3};print &foo()'
Other ways of avoiding Perl to inline foo() are:
print *foo{CODE}(); print eval 'foo()';

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


In reply to Re: Little Perl Mysteries: what's your answer? by Juerd
in thread 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.