Perl:脚本和模块调用第二个模块

时间:2016-08-23 16:19:44

标签: perl module

我有以下内容:

模块1

package module1;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(<list of subs within>);

use Module2;

sub M1S1 ()
{
  $x = M2S1();
}

第2单元

package module2;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(<list of modules within>);
sub M2S1()
{
...
}

sub M2S2()
{
...
}

脚本

use Module2;
use Module1;

$y = M1S1();
$z = M2S2();

当脚本调用Module 1中的某个子进程调用Module 2中的子进程时,即使脚本可以直接调用这些子进程,也找不到该子进程。

我无论如何都不是Perl的初学者,但我从来没有完全掌握模块。我们的环境非常依赖module2,因此我不想进行任何需要更改所有使用它的脚本的更改。 Module1使用有限,因此我可以根据需要对其进行更改。

1 个答案:

答案 0 :(得分:2)

文件名,package指令中的名称以及use语句中的名称必须匹配,包括大小写。

 Module1.pm
 package Module1;
 use Module1;

或者如果你有一个非平坦的命名空间,

 Foo/Bar.pm
 package Foo::Bar;
 use Foo::Bar;

请注意,如果您有两个相互使用的导出模块(直接或间接),则可以使用类似的problems,但这似乎并非如此。

$ cat Module1.pm
package Module1;
use strict;
use warnings;
use Exporter qw( import );
our @EXPORT = qw( M1S1 );
use Module2;
sub M1S1 { M2S1() }
1;

$ cat Module2.pm
package Module2;
use strict;
use warnings;
use Exporter qw( import );
our @EXPORT = qw( M2S1 M2S2 );
sub M2S1 { "M2S1" }
sub M2S2 { "M2S2" }
1;

$ cat script.pl
#!/usr/bin/perl
use strict;
use warnings;
use Module2;
use Module1;
print(M1S1(), "\n");
print(M2S2(), "\n");

$ ./script.pl
M2S1
M2S2