模块相互循环使用。 Perl 中的编译错误

Modules use each other in cycle. Compilation error in Perl

一共有3个模块,所以它们按模式相互使用 a -> b -> c -> a。我无法编译这种情况。

例如,

我遇到编译错误

"Throw" is not exported by the LIB::Common::Utils module
Can't continue after import errors at /root/bin/ppm/LIB/Common/EnvConfigMgr.pm line 13
BEGIN failed--compilation aborted at /root/bin/ppm/LIB/Common/EnvConfigMgr.pm line 13.

Utils.pm

use Exporter qw(import);
our @EXPORT_OK = qw(
        GetDirCheckSum
        AreDirsEqual
        onError
        Throw);
use LIB::Common::Logger::Log;

Log.pm

use Log::Log4perl;

use LIB::Common::EnvConfigMgr qw/Expand/;

EnvConfigMgr.pm

use Exporter qw(import);

our @EXPORT = qw(TransformShellVars ExpandString InitSearchLocations);
our @EXPORT_OK = qw(Expand);

use LIB::Common::Utils qw/Throw/;

为什么它没有被编译以及如何让它工作?

您需要在依赖循环中的某处使用 require 而不是 use 以延迟绑定。使用不导出任何内容的模块是最方便的,否则您需要编写一个显式的 import 调用

在你的情况下 LIB::Common::Logger::Log 不使用 Export,所以把

require LIB::Common::Logger::Log

进入LIB/Common/Utils.pm解决了问题

您可以访问无法正常工作的代码,您可以通过简单地显示故障代码来为我们节省大量时间。您忽略了两条要求提供更多信息的评论,所以我设置了这些文件

注意这段代码什么都不做:它只是编译

LIB/Common/Utils.pm

package LIB::Common::Utils;

use Exporter 'import';

our @EXPORT_OK = qw/
    GetDirCheckSum
    AreDirsEqual
    onError
    Throw
/;

require LIB::Common::Logger::Log;

sub GetDirCheckSum { }

sub AreDirsEqual { }

sub onError { }

sub Throw { }

1;

LIB/Common/Logger/EnvConfigMgr.pm

package LIB::Common::EnvConfigMgr;

use Exporter 'import';

our @EXPORT = qw/ TransformShellVars ExpandString InitSearchLocations /;
our @EXPORT_OK = 'Expand';

use LIB::Common::Utils 'Throw';

sub TransformShellVars { }

sub ExpandString { }

sub InitSearchLocations { }

sub Expand { }

1;

LIB/Common/Logger/Log.pm

package LIB::Common::Logger::Log;

use Log::Log4perl;

use LIB::Common::EnvConfigMgr 'Expand';

1;

main.pl

use strict;
use warnings 'all';

use FindBin;
use lib $FindBin::Bin;

use LIB::Common::Utils;