I have a perl problem: importing symbols, depending on the path elements in @INC and the use statement.
If I put the full path into @INC, the import works. If a part of the path is in the use statement the module to import is executed, but the import has to be done explicitly:
########################################
# @INC has: "D:/plu/lib/"
#------------------------------------------------
# Exporting file is here: "D:/plu/lib/impex/ex.pm"
#
use strict;
use warnings;
package ex;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw( fnQuark );
sub fnQuark { print "functional quark\n"; }
print "Executing module 'ex'\n";
1;
#------------------------------------------------
# Importing file, example 1, is here: "D:/plu/lib/impex/imp.pl"
#
use strict;
use warnings;
package imp;
use impex::ex;
ex->import( @ex::EXPORT ); # removing this line makes fnQuark unavailable!
# Why is this necessary, 'ex.pm' being an Exporter?
fnQuark();
#------------------------------------------------
# Importing file, example 2, is here: "D:/plu/lib/impex/imp2.pl"
#
use strict;
use warnings;
package imp2;
use lib 'D:/plu/lib/impex';
use ex;
fnQuark(); # works without explicit import
#-------------------------------------------------
What is my mistake?
When you say
this is equivalent to:
You’ve defined the package in your
ex.pmto be namedex, so when youuse impex::ex, Perl does an implicitimpex::ex->import. But there is no package namedimpex::ex, so you have to manually do the import fromexto get your symbols.The correct way to do this is to put your modules under an existing directory in
@INCand name the package after the full path-name relative to the@INCdirectory. So yourimpex/ex.pmshould begin withpackage impex::ex;and that’s how you shoulduseit.If you’re worried about package names being long and unwieldy, have a look at aliased.