Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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



  • 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 rifling through the Monastery: (3)
As of 2024-04-24 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found