Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

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

robartes appears to be the first to have correctly answered the question, but I'll respond to this as you've answered more in depth and you have a question that needs answering.

The article that I read, by Sean Burke, is about Constants in Perl. It discusses some a traditional compiler optimization known as "constant folding". Essentially, this occurs when, for a given operations, if all of the operands are known at compile time and cannot change, then the compiler goes ahead and precomputes the resulting value to save a bit of time.

perl -MO=Deparse -e 'print 3*4' print 12; -e syntax OK

Thus, what appears to be an expression is turned into a literal constant. Note that the 3*4 is now optimized away. There is no longer any remnant of that code. This, naturally, would be even more useful when it occurs in an inner loop, but the mechanism is not perfect and Perl sometimes needs to be helped. See the article for details and the snippet below for an example.

$ perl -MO=Deparse -e '$i=6;$j=2;print 3*4*$i*$j*3*4' $i = 6; $j = 2; print 12 * $i * $j * 3 * 4; -e syntax OK

That result is disappointing, but adding the '-p' switch to force parenthese can tell us why Perl is getting a bit confused.

$ perl -MO=Deparse,-p -e '$i=6;$j=2;print 3*4*$i*$j*3*4' ($i = 6); ($j = 2); print(((((12 * $i) * $j) * 3) * 4)); -e syntax OK

Tying all of that together with my snippet, we have the following quote from the third Camel.

Inlining Constant Functions

Functions prototyped with (), meaning that they take no arguments at all, are parsed like the time built-in. More interestingly, the compiler treats such functions as potential candidates for inlining. If the result of that function, after Perl's optimization and constant-folding pass, is either a constant or a lexically scoped scalar with no other references, then that value will be used in place of calls to that function. Calls made using &NAME are never inlined, however, just as they are not subject to any other prototype effects.

Because Perl can optimize the sub call away, there is no longer any subroutine for the typeglob to override.

Juerd wrote:

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.

I don't believe this is a bug. I ran that on my Linux box at home with both 5.6.1 and 5.8.0 and the latter does not display the definition of the sub, but the former does. I suspect that this is actually an optimization where there is no need to have the sub's code in the bytecode if it's been optimized away. Unfortunately, I cannot find a reference to this in the 5.8.0 docs and thus cannot confirm it.

Cheers,
Ovid

Thanks to larsen for pointing out my typo: s/Contacts/Constants/;

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


In reply to Re: Re: Little Perl Mysteries: what's your answer? by Ovid
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 goofing around in the Monastery: (2)
As of 2024-04-25 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found