Good morning,

I was trying to find out how Exporter manages to prevent the calling module from "strict vars" errors, so that the calling module may use the imported variables without declaring them.

So I wrote a file called "imp.pl":

package imp; *main::foo = \42;

Then I tried

perl -le 'BEGIN { require "imp.pl"}; use strict; print $foo'
and it printed 42. Now that's cool: I may use $foo without declaring it, merely because it exists in my namespace.

However, removing the first line "package imp;" from imp.pl, I get those errors:

perl -le 'BEGIN { require "imp.pl"}; use strict; print $foo' Variable "$foo" is not imported at -e line 1. Global symbol "$foo" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

So, the exporting doesn't work within the "main" package, which appears very strange to me. Even more curious, perl does seem to recognize that I tried to import the variable, because it prints "...is not imported...". This warning isn't printed for other variables:

perl -le 'BEGIN { require "imp.pl"}; use strict; print $bar' Global symbol "$bar" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

Extract from perldiag:

Variable "%s" is not imported%s
(F) While "use strict" in effect, you referred to a global variable that you apparently thought was imported from another module, because something else of the same name (usually a subroutine) is exported by that module. It usually means you put the wrong funny character on the front of your variable.

Obviously, I did not put the wrong funny character.

Could anyone please shed light into this darkness of perl's? Thank you very much.

Update: Using perl -MO=Deparse on imp.pl, I see that *main::foo is optimized to *foo. However, without a package line, this shouldn't make any difference.


In reply to How to trick strict like Exporter does by betterworld

Title:
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, details, 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, summary, 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.