Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #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

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 romping around the Monastery: (6)
    As of 2021-01-26 12:51 GMT
    Find Nodes?
      Voting Booth?