简体   繁体   English

如何在Perl中的另一个模块子内部调用一个模块子

[英]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.pmConfig.pmOperation.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. 这是调用堆栈。

Operation.pm Operation.pm

    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;

Config.pm 配置文件

    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 ,将发生以下情况:

  • scan ABC::Operation for use statements 扫描ABC :: Operation for use语句
  • load Term::ANSIColor 加载期限:: ANSIColor
    • parsing switches to Term::ANSIColor 解析开关到Term :: ANSIColor
    • scan Term::ANSIColor for use statements ... scan Term :: ANSIColor for use语句...
  • parsing switches back to ABC::Operation 解析切换回ABC :: Operation
  • load File::Basename 加载File :: Basename
    • parsing switches to File::Basename 解析切换到File :: Basename
    • scan File::Basename 扫描File :: Basename
  • parsing switches back to ABC::Operation 解析切换回ABC :: Operation
  • ... a couple more of those ...其中一些
  • load ABC::Config 加载ABC :: Config
    • parsing switches to ABC::Config 解析切换到ABC :: Config
    • scan ABC::Config for use statements ... 扫描ABC :: Config以获取use声明...
  • parsing witches back to ABC::Operation 将女巫解析回ABC :: Operation
    • import stuff from Term::ANSIColor, File::Basename, Exporter and Cwd; 从Term :: ANSIColor,File :: Basename,Exporter和Cwd import内容; these do not get loaded again because Perl already loaded them before. 这些不会再次加载,因为Perl之前已经加载了它们。 It only imports the symbols into the current ABC::Config namespace 它仅将符号导入到当前的ABC :: Config命名空间中
    • import the function commonFunc from ABC::Config; 从ABC :: Config import功能commonFunc ; again, this has also already been loaded, so it doesn't load it again 再次,它也已经被加载,因此它不会再次加载
    • throw an error because at this point, ABC::Config has not finished parsing, and it does not yet export a commonFunc symbol 引发错误,因为此时ABC :: Config尚未完成解析,并且尚未导出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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM