Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

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

The trick is that *T::y = \"b"; needs to happen at compile time. Try adding a BEGIN to your first block and your code works. Remember that use Module LIST; is equivalent to BEGIN { require Module; Module->import( LIST ); }.

Update: I'm not completely sure how you're connecting this question to Re: Text::Template and delayed use vars - could you make that clearer? Without digging into the Text::Template source code, I suspect that something slightly different is going on there. First, note that fully qualified variable names ($T::x) are exempt from strict vars.

Second, the above statement can be generalized to: Perl needs to know about the variable name before compiling a piece of code. That's why in your example, you need to move the "predeclaration" of $T::y to the compile time before the runtime of the main script by placing it in a BEGIN block (note you could also use the vars pragma, which just hides the same thing behind a nicer syntax).

But if during the runtime of your main script, you compile and run another piece of code via require, do, or eval, then that code's compile time is later, during the runtime of your script (confused yet? ;-) see BEGIN, UNITCHECK, CHECK, INIT and END). That means the following:

use warnings; use strict; eval ' package T; print "A: $x\n"; 1 ' or warn "A: $@"; # => Global symbol "$x" requires explicit package name { no warnings 'once'; *T::x = \"foo"; } # runtime of main! eval ' package T; print "B: $x\n"; 1 ' or warn "B: $@"; # => now works

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

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 taking refuge in the Monastery: (8)
As of 2024-04-18 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found