如何使用 %EXPORT_TAGS

5 perl

我有一个类似这样的模块,在“lib”中,名为 Fool.pm,它基于CGI.pm 的源代码(因为这是我在考虑导出标签时想到的第一个模块):

package Fool;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw/raspberry/;
%EXPORT_TAGS = (
    ':all' => \@EXPORT_OK,
);
1;
Run Code Online (Sandbox Code Playgroud)

和这样的测试脚本:

use lib 'lib';
use Fool qw/:all/;
Run Code Online (Sandbox Code Playgroud)

我尝试运行脚本并获得以下信息:

perl fool.pl
"all" is not defined in %Fool::EXPORT_TAGS at fool.pl line 2
  main::BEGIN() called at lib/Fool.pm line 2
  eval {...} called at lib/Fool.pm line 2
Can't continue after import errors at fool.pl line 2
BEGIN failed--compilation aborted at fool.pl line 2.
Run Code Online (Sandbox Code Playgroud)

我看不出这里有什么错误,有人可以帮忙吗?

amp*_*ine 3

您的密钥中不应该有冒号。另外,我认为必须声明变量our才能使其可用Exporter

our @ISA = qw(Exporter);
our @EXPORT_OK = qw/raspberry/;

our %EXPORT_TAGS = (
    'all' => \@EXPORT_OK,
);
Run Code Online (Sandbox Code Playgroud)

  • 不,“our”只是(词汇上)声明全局变量的使用;如果没有它,变量仍然是全局的(但会在“use strict”下给出错误) (2认同)