当它们递归调用时,为什么包A中的函数不导入到包B中? [英] Why don't functions from package A get imported into package B when they recursively call each other?

查看:73
本文介绍了当它们递归调用时,为什么包A中的函数不导入到包B中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个程序包并相互调用函数,但是出现了这个错误:

I'm trying to use two packages and call functions from one to other, but I've got this error:

在module2.pm第20行调用的未定义子例程& module2 :: method_1_2.

Undefined subroutine &module2::method_1_2 called at module2.pm line 20.

有什么方法可以将一个包中的函数调用到另一个包中而又不会出现此错误?

Is there any way to call functions from one package to the other one without getting this error?

谢谢.

xabi

执行错误:

./test.pl
method_1_1
method_2_1
method_2_2
Undefined subroutine &module2::method_1_2 called at module2.pm line 20.

示例代码(test.pl):

Sample code (test.pl):

#!/usr/bin/perl

use strict;
use module1;
use module2;

method_1_1();
method_2_2();

module1.pm

module1.pm

package module1;

use strict;

use module2;

require Exporter;
use vars qw(@ISA @EXPORT);
@ISA     = qw(Exporter);
@EXPORT  = qw( method_1_1 method_1_2 );

sub method_1_1
{
   print "method_1_1\n";
   method_2_1();
}

sub method_1_2
{
   print "method_1_2\n";
}

1;

module2.pm:

module2.pm:

package module2;

use strict;

use module1;

require Exporter;
use vars qw(@ISA @EXPORT);
@ISA     = qw(Exporter);
@EXPORT  = qw( method_2_1 method_2_2 );

sub method_2_1
{
   print "method_2_1\n";
}

sub method_2_2
{
   print "method_2_2\n";
   method_1_2();
}

1;

推荐答案

问题是module1要做的第一件事是对use module2.这意味着在module1仍在编译时,将读取并执行所有module2.

The problem is that the very first thing module1 does is to use module2. That means all of module2 is read and executed while module1 is still compiling.

接下来要发生的是module2执行use module1.因为已找到module1并将其放入%INC中,所以Perl不会再次执行它,而module1->import只会执行以提取导出的符号.

The next thing to happen is that module2 does use module1. Because module1 has been found and put into %INC Perl doesn't execute it again, and just does module1->import to fetch the exported symbols.

但是,当然module1实际上几乎还没有开始编译,并且@module1::EXPORT甚至不存在,不必在意它的两个子例程.这使得Exporter根本不导入任何内容到module2中,因此,当进行调用method_1_2()时,它一无所知.

But of course module1 has in fact barely started compiling, and @module1::EXPORT doesn't even exist, never mind about its two subroutines. That makes Exporter import nothing at all into module2, so when it comes to make the call method_1_2() it knows nothing about it.

解决此问题的最干净方法是在编译后(包括所有use语句和BEGIN块)(但在之前运行时)导入 . Perl的 INIT是对此非常理想,我们可以通过将模块更改为以下形式来使代码正常工作.我在这里只显示了module2,因为调用方式意味着这是解决此特定问题所需的全部,但是一般情况下需要对所有协作模块进行相同的更改.

The cleanest way to fix this is to do the import after the compilation (including all the use statements and BEGIN blocks) but before runtime. Perl's INIT block is ideal for this, and we can get the code working by changing the modules to the form below. I have shown only module2 here, as the pattern of calls means this is all that's needed to fix this particular problem, but the general case needs the equivalent change to all cooperating modules.

package module2;

use strict;
use warnings;

use module1;
INIT { module1->import }

use base 'Exporter';
our @EXPORT  = qw( method_2_1 method_2_2 );

sub method_2_1 {
   print "method_2_1\n";
}

sub method_2_2 {
   print "method_2_2\n";
   method_1_2();
}

1;

这篇关于当它们递归调用时,为什么包A中的函数不导入到包B中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆