【问题标题】:Perl not able to access sub from module filePerl 无法从模块文件访问子
【发布时间】:2014-05-03 08:01:03
【问题描述】:

我在同一个文件夹中创建了两个文件来学习模块。

模块.pm

package Modules;

use strict;
use warnings;

require Exporter;

BEGIN { our @EXPORT = qw(Print); }

sub Print { print shift(@_); }

END { }

1;

Main.pl

use strict;
use warnings;

use Modules;

Print("Hello World!");

在运行命令 perl Main.pl 时出现以下错误。我做错了什么?

Undefined subroutine &main::Print called at Main.pl line 7

【问题讨论】:

    标签: perl module perl-module


    【解决方案1】:

    你忘了告诉你的包它继承自Exporter

    package Modules;
    
    use strict;
    use warnings;
    
    require Exporter;
    our @ISA = qw(Exporter);
    our @EXPORT = qw(Print);
    
    sub Print { print shift(@_); }
    
    
    1;
    

    或不易出错,

    package Modules;
    
    use strict;
    use warnings;
    
    use parent qw( Exporter );
    our @EXPORT = qw(Print);
    
    sub Print { print shift(@_); }
    
    
    1;
    

    更多内容请关注perldoc

    【讨论】:

    • 在实践中,应避免手动修改@ISAuse parent 'Exporter' 将负责加载和从 Exporter 继承。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多