Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Global symbol x requires explicit package name at main.pl

by beanryu (Novice)
on Aug 18, 2010 at 23:53 UTC ( [id://855933]=perlquestion: print w/replies, xml ) Need Help??

beanryu has asked for the wisdom of the Perl Monks concerning the following question:

Hello there, I have a variable x defined in a file called test.pm and I tried to use it in another file call main.pl In test.pm, I have:
package test; use strict; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw($x); my $x = "hi"; __END__
and in main.pl I have:
use strict; use test; print $x
and when I run main.pl, its giving me this error Global symbol $x requires explicit package name at main.pl... Would someone please enlighten me on this?

Replies are listed 'Best First'.
Re: Global symbol x requires explicit package name at main.pl
by toolic (Bishop) on Aug 19, 2010 at 00:09 UTC

    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.
      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.

Re: Global symbol x requires explicit package name at main.pl
by oko1 (Deacon) on Aug 19, 2010 at 00:33 UTC

    On very rare occasion, there's a valid reason to make a package variable global (or declare it with 'our', as toolic recommended.) If you do so, then you can access it without exporting or any other special mechanisms, simply by specifying the package name:

    package test; $x = "Hi"; # Or $::x = "Hi"; package main; print $test::x;

    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
      thanx for the advice, I used the double colon method and assign the package variable to a local variable. However, it still didn't work. Then I change "use test" to "require("test.pm")" and it worked... thanx But this is still very magical, kinda confusing...
Re: Global symbol x requires explicit package name at main.pl
by rowdog (Curate) on Aug 19, 2010 at 10:37 UTC

    use lib to put the current directory at the front of the load list:

    use strict; use lib '.'; use test; print $x

    But you should just follow the good advice you've received from toolic and change the name of your module to something saner like MyTest.pm.

Re: Global symbol x requires explicit package name at main.pl
by Anonymous Monk on Aug 19, 2010 at 19:52 UTC

    beanryu, I think you may have 2 separate issues:

    1. Don't name your module "test". I think there's already a std module with that name.

    2. You don't need to use Exporter. Just do this:

    In MyTest.pm:

    package MyTest; use Modern::Perl; # or strict, warnings our $x = 'hi';

    and in main.pl:

    use Modern::Perl; use MyTest; say $MyTest::x;

    Finally, if main.pl fails to find MyTest.pm, you'll need a use lib 'path/to/your/modules' line before the use MyTest.

Re: Global symbol x requires explicit package name at main.pl
by Cybris (Sexton) on Aug 19, 2010 at 19:54 UTC
    Never name a module "Test" (or "test" on Windows). This has bitten me in the behind more often than I care to admit ;-)

    The name "Test" will be overriden by the pre-installed Test.pm core module, unless you fuzz about with "use lib...". It's not worth it, call it "MyTest" or "Scrap" or something if it's temporary.

    The reason "test" doesn't work on Windows is that Windows has a case-insensitive file system - you can't distinguish between files call Test.pm, test.pm or TEST.pm.

    Tl;dr: don't ever call a package any permutation of "Test".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://855933]
Approved by toolic
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found