在第 4 行 ex1.pl 处调用了未定义的子例程 &main::add

Undefined subroutine &main::add called at ex1.pl line 4

我有一个 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);

我收到以下错误:

Undefined subroutine &main::add called at ex1.pl line 4.

可能是什么原因?

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

如果你包括

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.