[英]How to call one module sub inside another module sub in Perl
I have created Perl modules under /my_project/ABC/
. 我已经在
/my_project/ABC/
下创建了Perl模块。 The ABC
folder contains three subroutine modules: Build.pm
, Config.pm
, and Operation.pm
. ABC
文件夹包含三个子例程模块: Build.pm
, Config.pm
和Operation.pm
。 There is a common subroutine in Operation.pm
that I need to access from Config.pm
, but when I tried i throws 我需要从
Config.pm
访问Operation.pm
中的一个通用子例程,但是当我尝试时会抛出
"configureTheRepository" is not exported by the ABC::Config module`.
“ configureTheRepository”不是由ABC :: Config模块导出的。
Here is the call stack. 这是调用堆栈。
package ABC::Operation;
use strict;
use warnings;
use Term::ANSIColor qw(:constants);
use File::Basename qw(dirname);
use Exporter qw(import);
use Term::ANSIColor qw( colored );
use Data::Dumper qw(Dumper);
use JSON::PP;
use File::Basename qw(dirname);
use Cwd qw(abs_path);
use ABC::Config qw(configureTheRepository);
our @EXPORT = qw(commonFunc)
sub commonFunc{
#handle logic
}
1;
package ABC::Config;
use strict;
use warnings;
use Term::ANSIColor qw(:constants);
use File::Basename qw(dirname);
use Exporter qw(import);
use Cwd qw(abs_path);
use Cwd qw(abs_path);
use ABC::Operation qw(commonFunc); #Compilation failed when i insert this line.if i removed this my script will execute and but in runtime throws undefined commonFunc.
our @EXPORT = qw(configureTheRepository);
sub configureTheRepository{
#handle logic
}
1;
Please let me know where I made the mistake. 请让我知道我在哪里弄错了。
There is a circular dependency. 存在循环依赖关系。
use
statements get done at compile time . use
语句在编译时完成。 That means before any actual code is run. 这意味着在运行任何实际代码之前。 So when you start your program, and the first thing it does is go into
ABC::Operation
, the following will happen: 因此,当您启动程序时,首先要做的是进入
ABC::Operation
,将发生以下情况:
use
statements use
语句 use
statements ... use
语句... use
statements ... use
声明... import
stuff from Term::ANSIColor, File::Basename, Exporter and Cwd; import
内容; these do not get loaded again because Perl already loaded them before. import
the function commonFunc
from ABC::Config; import
功能commonFunc
; again, this has also already been loaded, so it doesn't load it again commonFunc
symbol commonFunc
符号 This is a bit confusing, but it's a sign that your architecture is broken. 这有点令人困惑,但这表明您的体系结构已损坏。 If things are common, they can sure be in a common package.
如果事情很普遍,那么它们肯定可以放在一个共同的包装中。 But that common package cannot use anything that uses it.
但是该通用软件包不能使用任何使用它的软件包。 If that's the case, whatever is using it also becomes common by definition.
如果真是这样,无论使用什么定义,它也变得很普遍。
The solution is to rethink which functions go where. 解决的办法是重新考虑哪些功能在哪里。 Find the smallest possible parts, and put them in one package.
找到最小的零件,并将它们放在一个包装中。 That's the things every other package shares.
这就是其他所有软件包共享的东西。 Then use it where it's needed.
然后在需要的地方使用它。 The next things that use this shouldn't bring each other in. Only the last level should combine all of them.
使用此功能的下一个功能不应相互融合。只有最后一级可以将所有这些功能结合在一起。 The dependency tree is called tree for a reason.
依赖树之所以称为树。 Having a circle in there can't work.
在那里围成一圈是行不通的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.