我有一个 Perl 项目,如果我只是通过循环包调用遇到问题。下面的代码演示了这个问题。
执行此操作时,每个包将调用另一个包,直到耗尽计算机的所有内存并锁定。我同意这是一个糟糕的设计,并且不应在设计中进行这样的循环调用,但我的项目足够大,我想在运行时检测到这一点。
我已经阅读了有关弱化函数和 Data::Structure::Util 的信息,但我还没有想出一种方法来检测是否存在循环包加载(我假设,因为每次迭代都会制作一个新副本并存储在 $this 哈希的每个副本中)。有任何想法吗?
use system::one;
my $one = new system::one();
package system::one;
use strict;
use system::two;
sub new {
my ($class) = @_;
my $this = {};
bless($this,$class);
# attributes
$this->{two} = new system::two();
return $this;
}
package system::two;
use strict;
use system::one;
sub new {
my ($class) = @_;
my $this = {};
bless($this,$class);
# attributes
$this->{one} = new system::one();
return $this;
}