Module: ./FOO/BAR/Foobar.pm
use strict;
use warnings;
package Foobar;
our($VERSION , @ISA , @EXPORT , @EXPORT_OK , %EXPORT_TAGS , $FOO);
BEGIN {
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&foo);
}
sub foo {
print "Loaded\n";
$FOO = q{some val};
}
1;
Program: ./Caller.pl
#!/usr/bin/perl
use strict;
use warnings;
use FOO::BAR::Foobar qw/foo/;
Foobar::foo(); # works
foo(); # errors out - can't find &main::foo
I'd list all the things I've tried, but there are a lot - as you can see I have more global Foobar globals than that listed. I've removed the BEGIN and done some other things as suggested in [older] posts on PerlMonks.
I think I read somewhere once that if the package name is the same name as the module name, Exporter works a certain way by default. I don't know if putting the module in a sub-directory could have changed that behavior(?). However, I'm eager to see how I've screwed up.
In addition to choroba's answer I found this post by Sherm Pendley, which I think is the same post I read years ago:
So, it seems if you have multiple packages per module, only the one that share the same name as the file can export its variables/functions. I suspect you could get it to work if you were to create your own import function for the other packages (?).
Anyhow, in my example, if I didn't want to call the package the whole hierarchy tree, I could set the lib:
This would mean I could still have
Foobar.pm
and inside itpackage Foobar
instead ofpackage FOO::BAR::Foobar
If the module is located in FOO/BAR/Foobar.pm, then the package name should be
FOO::BAR::Foobar
.I would also remove the
&
sign from the @EXPORT_OK array.