Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Importing a variable into a package (updated)

by haukex (Abbot)
on May 17, 2018 at 08:35 UTC ( #1214717=note: print w/replies, xml ) Need Help??


in reply to Re^2: Importing a variable into a package
in thread Importing a variable into a package

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214717]
help
Chatterbox?
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-07-21 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (449 votes). Check out past polls.

    Notices?