Update: Interestingly, I can not duplicate your exact problem on linux; I can only duplicate it on windows. Here is my hypothesis. There is a core module named Test. On windows, file names are case-insensitive. When I do use test; (lower-case), I think perl actually loads Test which is in the @INC path before ./test.pm. Add this to your main.pl script:

use Data::Dumper; print Dumper(\%INC);

Since Test does not have a variable named $x, I get the same error you get. In any case test.pm is not a very unique name, and it should be changed --- even for contrived examples.

Also, you should be using warnings.


Here is my original reply (which is still good advice):

Declare your variable with our inside your package:

package test; use strict; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw($x); our $x = "hi"; __END__

That solves your immediate problem. However, you may want to heed the advice in What not to Export:

Exporting variables is not a good idea. They can change under the hood, provoking horrible effects at-a-distance, that are too hard to track and to fix. Trust me: they are not worth it.

In reply to Re: Global symbol x requires explicit package name at main.pl by toolic
in thread Global symbol x requires explicit package name at main.pl by beanryu

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.