in reply to Global symbol x requires explicit package name at main.pl

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.

Replies are listed 'Best First'.
Re^2: Global symbol x requires explicit package name at main.pl
by beanryu (Novice) on Aug 19, 2010 at 00:44 UTC
    actually, in the beginning, I used our, it just reports the same error.
      That would have been nice to know up front. What else did you try that you haven't told us?

      What OS are you on? Windows?

      perl -v

      I misdiagnosed the problem. See my update. However, my advice still holds.

        actually, I am not sure, I am testing it on a server... and uname reports linux... and I've also tried other package names... still the same problem... thanks alot anyways.