【问题标题】:Undefined subroutine &main::add called at ex1.pl line 4在 ex1.pl 第 4 行调用的未定义子例程 &main::add
【发布时间】:2015-02-27 01:04:46
【问题描述】:

我有一个 perl 模块

MyMathLib.pm

package MyMathLib;
require Exporter;

@ISA = qw/EXPORTER/;

@EXPORT = qw/add/;

sub add
{
  $_[0] + $_[1];
}

1;

Ex1.pl

#!usr/bin/perl
#
use MyMathLib;
print add(1,2);

我收到以下错误:

未定义的子程序 &main::add 在 ex1.pl 第 4 行调用。

可能是什么原因?

【问题讨论】:

    标签: perl module


    【解决方案1】:

    它是出口商而不是出口商。

    如果你包括

    use strict;
    use warnings;
    

    在您的脚本中,您将激活 more checks,它会向您显示问题的线索:

    Can't locate package EXPORTER for @MyMathLib::ISA at Ex1.pl line 5.
    Undefined subroutine &main::add called at Ex1.pl line 6.
    

    【讨论】:

    • 长期以来一直建议只使用Exporter的导入方式,不要子类化。将 require 和 @ISA= 替换为 use Exporter 'import';
    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 2021-04-16
    • 1970-01-01
    相关资源
    最近更新 更多