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

Hello,

I'm trying to use a package structure, but I have some problems.

I have two scripts:

- myFile.pl;

- Config.pm;

I'm trying to include config.pm in myFile.pm,
I'm doing it using Exporter module.


But I have an error and I don't understand what it is wrong with it:
This is the content of Config.pm:
package Config; use strict; use warnings; our (@ISA,@EXPORT,$VERSION); use Exporter; @ISA=qw(Exporter); @EXPORT=qw($test DEFAULT_LOCAL_PATH ); $VERSION=0.01; use constant DEFAULT_LOCAL_PATH=>'something here'; my $test='hello'; 1;

--------------------

An this is myFile.pl content:

use strict; use warnings; use FindBin; use lib "$FindBin::Bin"; use Config; print $test;

-------------------
I have this error:
Global symbol "$test" requires explicit package name at miFile.pl line + 10. Execution of backup.pl aborted due to compilation errors.

it seems exporter doesn't export anything.
Any help is really welcome.

Replies are listed 'Best First'.
Re: Exporter problem
by almut (Canon) on Jun 03, 2010 at 16:49 UTC

    First, lexical variables cannot be exported. Use our $test="hello" instead.

    Secondly, the name Config is somewhat unfortunate, because there's a core module of the same name, which you will likely load instead of your own...

    Update: to elaborate on the latter point: the use lib "$FindBin::Bin"; doesn't achieve the desired effect here, because use lib itself already loads Config.pm, so your own subsequent use Config just does nothing, because the (core) module is already loaded.  In theory, you could work around that problem (in this particular case) by saying

    BEGIN { unshift @INC, $FindBin::Bin }

    but it's probably a better idea to choose a different name, in order to avoid such pitfalls right from the start... (What if some other module you load later should need the core module's functionality after you've successfully manoeuvred your own module into the Config namespace?)

      thank you for your answer, my package now is "toConfig.pm".
        Hello Monks,
        now I would like to put my toConfig.pm
        in a directory called LIB;
        so I have update my scripts:
        use FindBin; use lib qw($FindBin::Bin LIB);
        In Linux it works fine, but in Windows I have:
        Can't locate toConfig.pm in @INC (@INC contains: $FindBin::Bin LIB C:/ +Perl/site/ lib C:/Perl/lib .) at C:\Documents and Settings\myFile. pl line 9.
        Why?
        Thank you...
Re: Exporter problem
by toolic (Bishop) on Jun 03, 2010 at 16:48 UTC
    Declare your variable with our inside your package:
    package Config; use strict; use warnings; our (@ISA,@EXPORT,$VERSION,$test); use Exporter; @ISA=qw(Exporter); @EXPORT=qw($test DEFAULT_LOCAL_PATH ); $VERSION=0.01; use constant DEFAULT_LOCAL_PATH=>'something here'; $test='hello'; 1;
    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.
      ok, thank you,
      but I have to declare
      another:
      our $test='hello';
      otherwise I have that error:
      Global symbol "$test" requires explicit package name at myFile.pl line + 16. Execution of myFile.pl aborted due to compilation errors.
      Thank you for your advise about exporting variable. I will not export them:
      $test was just a test.
      I will export just some constants, useful to define my environement in whole package