Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Your understanding is <update> almost correct, just your reason for needing the BEGIN in the root node is a little off, the problem happens at the compile time of the print $y. It's probably best if you look at compile time vs. run time more in general. </update>

Perl of course has to parse and compile a piece of code like *T::x=\"x"; when it encounters it. However, that piece of code isn't run until runtime, that is, the assignment to the glob doesn't happen until after the entire surrounding code is compiled and while it is being executed.

So if the assignment is immediately followed by print $x;, when Perl tries to compile that statement, it does not yet know about $x!

That's why in your example code you need to move the execution of the assignment into the compile time of the surrounding code via BEGIN.

And in the case of the module, which does indeed appear to use eval: eval STRING needs to compile and run the code it is given, but it does not do so until the eval itself is executed, that is, the runtime of the eval statement. So in this case, if the assignment to the glob happens at run time instead of compile time, as long as it happens before the eval, this will still work, because the assignment is still happening before the compile time of the code that was given to eval.

Note that this way, with BEGIN and eval in Perl it's possible to basically have as many compile and run times as one wants.

Also a few updates for clarity.

In reply to Re^3: Importing a variable into a package (updated) by haukex
in thread Importing a variable into a package by frazap

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (8)
    As of 2021-01-21 22:03 GMT
    Find Nodes?
      Voting Booth?