【问题标题】:Specal Handling of Base.pm?Base.pm 的特殊处理?
【发布时间】:2014-12-13 10:10:45
【问题描述】:

我在与要使用它的 Perl 程序相同的目录中创建了一个名为 Base.pm 的模块,但该模块似乎没有被加载。将模块的名称更改为 Base 以外的任何名称,一切正常。 Base.pm 有什么特别之处,如何覆盖这种行为?感谢您提供任何见解。

package Base;
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION     = 1.00;
@ISA         = qw(Exporter);
@EXPORT      = qw(func);
sub func { return "foo"; }
1;

use Base;
print func();

产量

Undefined subroutine &main::func called at test0.pl line 2.

package Case;
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION     = 1.00;
@ISA         = qw(Exporter);
@EXPORT      = qw(func);
sub func { return "foo"; }
1;

use Case;
print func();

产量

foo.

【问题讨论】:

  • 虽然不是重复的,但this question 与您的情况相关。

标签: perl


【解决方案1】:

base.pm 是一个核心系统“pragma”模块(现在,parent.pm 已弃用它)

use base 'MyBaseClass'; # Same as: our @ISA = ( 'MyBaseClass' );

这可能是 Windows 案例。 Perl 寻找“Base.pm”,但 windows 返回“base.pm”。当您将它命名为“除 Base 之外的任何东西”时,您可能没有将它重命名为另一个 Perl 模块。

一种检查方法是运行以下命令:

  • useData::Dumper
  • 打印%INC 的转储(所有已加载模块的地图及其加载位置)

    use Data::Dumper;
    ...
    use Base;
    ...
    print Dumper( \%INC );
    

查找 'base.pm''Base.pm' 以查看 Perl 加载的内容。

【讨论】:

    【解决方案2】:

    检查 %INC 以查看实际加载的内容:

    use Base;
    
    print $INC{'Base.pm'};
    

    输出:

    /Users/miller/perl5/perlbrew/perls/perl-5.20.0/lib/5.20.1/Base.pm
    

    我在一个不区分大小写的 OSX 分区上,所以这个文件对应于base pragma。

    为避免此类问题,请为您的项目提供私有命名空间。这样与当前或未来的 Perl Core 或 CPAN 模块发生冲突的风险就会降低:

    package MyProject::Base;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2014-09-13
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多