dsollen
dsollen

Reputation: 6459

Using exported variables from required module with use strict

I had previously had use Foo; to use a module I had written, and was using methods and variables from it which were exported using Exporter.pm.

I replaced the use with require to allow some flexibility on when the module was imported. I now get dozens of errors all saying that the symbols "require explicit package name."

I know I could add the explicit method name for each, but there are quite a few.

Is there a way to alias these once so that afterwards I can use the symbol without explicit packages each time?

Upvotes: 2

Views: 126

Answers (2)

Borodin
Borodin

Reputation: 126762

" I'm loading a module from a location specified in a config file, so it has to be done at run time"

Okay, but it doesn't have to be done at run time so you're asking the wrong question

Do this

use strict;
use warnings 'all';

use constant CONFIG_FILE => '/path/to/config_file';

my $libs;

BEGIN {
    open my $fh, '<', CONFIG_FILE or die $!;
    chomp($libs = <$fh>);
}

use lib $libs;

use MyLib;    #  MyLib.pm is in the directory specified in config_file

Upvotes: 4

ikegami
ikegami

Reputation: 386561

You need to import the symbols before they are encountered in the code by the compiler.

You can load the the symbols sooner.

use Foo;

sub moo { ... $SymbolImportedFromFoo ... }

You can compile the mention later.

eval(<<'__EOI__') or die $@;

    use Foo;

    sub moo { ... $SymbolImportedFromFoo ... }

    1;

__EOI__

Upvotes: 2

Related Questions