Perl:同一文件中的两个软件包不能导入同一软件包? [英] Perl : Two packages in same file cannot import same package?

查看:112
本文介绍了Perl:同一文件中的两个软件包不能导入同一软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有趣的Perl行为. (至少对我来说:))

This is an interesting Perl behaviour. (atleast to me :) )

我有两个软件包PACKAGE1PACKAGE2,它们以相同的名称Method1()导出函数.

I have two packages PACKAGE1 and PACKAGE2 which exports function with same name Method1().

由于将有很多软件包将导出相同的功能,因此use-将Perl文件中的所有内容繁琐.因此,我创建了一个包含这些use的常规包含文件INCLUDES.pm.

As there will be so many packages which will export this same function, use-ing everything in a Perl file will be tedious. So, I have created a general include file INCLUDES.pm which will have these uses.

INCLUDES.pm:

INCLUDES.pm:

use PACKAGE1;
use PACKAGE2;

1;

PACKAGE1.pm:

PACKAGE1.pm:

package PACKAGE1;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw (
          Method1
);

sub Method1{
print "PACKAGE1_Method1 \n";
}

1;

PACKAGE2.pm:

PACKAGE2.pm:

package PACKAGE2;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw (
    Method1
);

sub Method1{
    print "PACKAGE2_Method1 \n";
}

1;

Tests.pl:

##################first package################
package Test1;
use INCLUDES;
my @array = values(%INC);
print  "@array  \n";

Method1();

##################second package################
package Test2;
use INCLUDES;  #do "INCLUDES.pm";
my @array = values(%INC);
print  "@array  \n";

Method1();

动机是,在任何Perl文件中都只能使用最新软件包的Method1().

The motive is, only the latest package's Method1() should be used in any Perl file.

输出使我感到惊讶. 我希望Tests.pl中的两个Method1()调用都应该成功. 但是只有第一个Method1()执行,第二个Method1()调用说未定义".

The output surprises me. I would expect that both Method1() calls in Tests.pl should be success. But only the first Method1() executes, the second Method1() call says "undefined".

输出:

C:/Perl/site/lib/sitecustomize.pl PACKAGE1.pm C:/Perl/lib/Exporter.pm PACKAGE2
.pmINCLUDES.pm

PACKAGE2_Method1

C:/Perl/site/lib/sitecustomize.pl PACKAGE1.pm C:/Perl/lib/Exporter.pm PACKAGE2
.pm INCLUDES.pm

Undefined subroutine &Test2::Method1 called at C:\Temp\PackageSample\Tests.pl line 15.

有人对此有任何答案/看法吗?

Do somebody have any answers/views on this?

实际情况:

多个Perl模块中的方法将具有相同的名称.但是,只能使用高优先级" perl模块中的方法.

the methods in multiple Perl modules will be having same name. But the methods from the High preference perl module should only be used.

例如,如果PACKAGE1包含Method1(), Method2()& PACKAGE2仅包含Method1(),则应从PACKAGE2& ;;中使用Method1().应从PACKAGE1

For example, if PACKAGE1 contains Method1(), Method2() & PACKAGE2 contains only Method1(), then Method1() should be used from PACKAGE2 & Method2() should be used from PACKAGE1

基本上,我想在基于首选项的模块之间实现层次结构.有什么办法吗?

Basically I want to achieve a Hierarchy among modules based on Preference. Is there any way for this?

推荐答案

在Perl中, use Module 等同于

In Perl, use Module is equivalent to

BEGIN { require Module; Module->import; }

但是 require 会缓存所需的模块列表.每个Perl进程仅加载一次模块.因此,只有第一个use IMPORTS会执行任何操作.由于您的IMPORTS模块没有import方法,因此当您再次use时,什么也不会发生.

But require caches the list of modules that have been required. It only loads the module once per Perl process. So only the first use IMPORTS does anything. Since your IMPORTS module doesn't have an import method, nothing happens when you use it again.

我不太确定您要完成什么.也许您的IMPORTS模块应该是一个实际的程序包,并带有import方法,该方法可以导出所需的任何功能.这样,每个use IMPORTS都会将函数导出到调用它的程序包中.

I'm not quite sure what you're attempting to accomplish. Perhaps your IMPORTS module should be an actual package, with an import method that exports whatever functions you want. That way, each use IMPORTS would export functions into the package that called it.

这篇关于Perl:同一文件中的两个软件包不能导入同一软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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