![](/img/trans.png)
[英]How to call a subroutine from one perl module to another perl module?
[英]How to call one module sub inside another module sub in Perl
我已經在/my_project/ABC/
下創建了Perl模塊。 ABC
文件夾包含三個子例程模塊: Build.pm
, Config.pm
和Operation.pm
。 我需要從Config.pm
訪問Operation.pm
中的一個通用子例程,但是當我嘗試時會拋出
“ configureTheRepository”不是由ABC :: Config模塊導出的。
這是調用堆棧。
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;
請讓我知道我在哪里弄錯了。
存在循環依賴關系。 use
語句在編譯時完成。 這意味着在運行任何實際代碼之前。 因此,當您啟動程序時,首先要做的是進入ABC::Operation
,將發生以下情況:
use
語句 use
語句... use
聲明... import
內容; 這些不會再次加載,因為Perl之前已經加載了它們。 它僅將符號導入到當前的ABC :: Config命名空間中 import
功能commonFunc
; 再次,它也已經被加載,因此它不會再次加載 commonFunc
符號 這有點令人困惑,但這表明您的體系結構已損壞。 如果事情很普遍,那么它們肯定可以放在一個共同的包裝中。 但是該通用軟件包不能使用任何使用它的軟件包。 如果真是這樣,無論使用什么定義,它也變得很普遍。
解決的辦法是重新考慮哪些功能在哪里。 找到最小的零件,並將它們放在一個包裝中。 這就是其他所有軟件包共享的東西。 然后在需要的地方使用它。 使用此功能的下一個功能不應相互融合。只有最后一級可以將所有這些功能結合在一起。 依賴樹之所以稱為樹。 在那里圍成一圈是行不通的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.