Say I have 3 perl files.
run.pl
#!/usr/bin/perl
use strict;
use warnings;
use Common;
validate(); # no need of Common::validate()
Common.pm
package Common;
use strict;
use warnings;
use Exporter qw(import);
use Validator;
our @EXPORT = qw(validate inArray);
sub validate
{
Validator::doSomething();
}
sub inArray
{
print("HERE\n");
}
return 1;
Validator.pm
package Validator;
use strict;
use warnings;
use Common;
sub doSomething
{
inArray(); # only Common::inArray() works here, why?
}
return 1;
When run the output is: Undefined subroutine &Validator::inArray called at Validator.pm line 10.
If I change
sub doSomething
{
inArray();
}
to
sub doSomething
{
Common::inArray();
}
then the result is expected HERE
.
My question is why are the subroutines exported by the Common module not available in the Validator module?
I'm using perl 5.22.0.
Because Validator.pm
is loaded and processed before @Common::EXPORT
is defined.
Some workarounds are either to
define @Common::EXPORT
during the "compile phase" of Common.pm
and before Validator.pm
is loaded
# Common.pm
...
BEGIN { our @EXPORT = qw(validate inArray) }
use Validator;
...
load Validator.pm
during the "run phase" of Common.pm
and after @Common::EXPORT
is defined
# Common.pm
...
our @EXPORT = qw(validate inArray);
require Validator;
Validator->import;
...