如何在Perl中将常量导入多个模块?

如何在Perl中将常量导入多个模块?,perl,constants,activeperl,Perl,Constants,Activeperl,我正在用Perl编写一个包含几个模块的应用程序。我想写一些从任何地方都可以看到的全局常量,如下所示: #Constants.pm $h0 = 0; $scale = 20; 然后在几个模块中使用它们,而不必使用main::或Constants::。然而,如果我写使用常量在多个模块中,它们只导入到一个命名空间中。这有什么办法吗 我正在使用最新的ActivePerl。您可以将其放在常量的顶部。pm: package main; 在这种情况下,您定义的所有变量都将位于main命名空间中: $mai

我正在用Perl编写一个包含几个模块的应用程序。我想写一些从任何地方都可以看到的全局常量,如下所示:

#Constants.pm
$h0 = 0;
$scale = 20;
然后在几个模块中使用它们,而不必使用
main::
Constants::
。然而,如果我写
使用常量
在多个模块中,它们只导入到一个命名空间中。这有什么办法吗


我正在使用最新的ActivePerl。

您可以将其放在
常量的顶部。pm

package main;
在这种情况下,您定义的所有变量都将位于
main
命名空间中:

$main::x
$::x
或者如果你感到勇敢:

package;
在这种情况下,您定义的所有变量都将位于空命名空间中:

$main::x
$::x
请注意,不鼓励使用不带名称空间的
,而且在某些版本的Perl中显然会被弃用。见下面的引文


引用自
man perlfunc

package NAMESPACE package Declares the compilation unit as being in the given namespace. The scope of the package declaration is from the declaration itself through the end of the enclosing block, file, or eval (the same as the "my" operator). All further unqualified dynamic identifiers will be in this namespace. A package statement affects only dynamic variables--including those you've used "local" on--but not lexical variables, which are cre? ated with "my". Typically it would be the first decla? ration in a file to be included by the "require" or "use" operator. You can switch into a package in more than one place; it merely influences which symbol table is used by the compiler for the rest of that block. You can refer to variables and filehandles in other packages by prefixing the identifier with the package name and a double colon: $Package::Variable. If the package name is null, the "main" package as assumed. That is, $::sail is equivalent to $main::sail (as well as to $main'sail, still seen in older code). If NAMESPACE is omitted, then there is no current pack? age, and all identifiers must be fully qualified or lexicals. However, you are strongly advised not to make use of this feature. Its use can cause unexpected behaviour, even crashing some versions of Perl. It is deprecated, and will be removed from a future release. 包名称空间 包声明编译单元位于给定的 命名空间。包声明的范围是 从声明本身到本报告结束 封闭块、文件或eval(与“我的”相同) 操作员)。所有进一步的非限定动态标识符 将位于此命名空间中。包语句影响 只有动态变量——包括您使用过的变量 “local”on——但不是词法变量,它们是cre? 用“我的”。通常这是第一个十二月? “要求”文件中包含的定额;或 “使用”运算符。您可以在更多的时间内切换到包中 不止一个地方;它只影响哪个符号表 编译器在该块的其余部分使用。 您可以在其他文档中引用变量和文件句柄 通过使用包作为标识符的前缀来创建包 名称和双冒号:$Package::Variable。如果 包名为null,假定为“main”包。 也就是说,$::sail相当于$main::sail(以及 至于$main'sail,仍然可以在旧代码中看到)。 如果省略了名称空间,那么是否没有当前包? 年龄,且所有标识符必须完全合格或 词汇。但是,强烈建议您不要这样做 利用这个特性。它的使用可能会导致意外的后果 行为,甚至使某些Perl版本崩溃。它是 已弃用,将从未来版本中删除。

编辑:这个问题可能也会有帮助:

请查看
perlmod
手册页。

不要告诉任何人我告诉过你这一点,但Perl的特殊变量是 到处都有。你可能已经注意到了,这并不重要 工作:

这是因为
$global
实际上被称为
$Foo::global
。你已经 也可能注意到这个“规则”不适用于
@INC
%ENV
$\uuu
等。这是因为这些变量总是 假定位于
main

但事实上,不仅仅是这些变量。整个地球 “强制”进入
main
。这意味着你可以写类似的东西 这:

它会起作用的

(这同样适用于
$ENV
&INC
等。)

然而,如果你在真实的代码中这样做的话,你可能会被人谋杀 )不过,很高兴知道,以防万一你看到别人 这样做。

您可以这样使用:

#Constants.pm
$h0 = 0;
$scale = 20;
下午六时正:

#Constants.pm
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw($h0 $scale);
@EXPORT_OK = qw(myfunc);

$h0 = 0;
$scale = 20;
sub myfunc {...}
注:
*
@EXPORT
数组中的
&myfunc
中的
是可选的,建议您不要使用它。
*默认情况下,这将导出
$h0
$scale
,并且仅当明确请求时才导出&myfunc(请参见下文如何指定客户端模块导入的符号)

然后在导入Constants.pm并希望使用
$h0
$scale
&myfunc
的模块中,添加以下内容以导入Constants.pm中
@EXPORT
中的所有符号

#MyModule.pm
use Constants qw(;
如果只想导入部分符号,请使用:

#MyModule.pm
use Constants qw($h0);
#MyModule.pm
use Constants ();
最后,如果您不想导入任何常数.pm的符号,请使用:

#MyModule.pm
use Constants qw($h0);
#MyModule.pm
use Constants ();

这段代码应该完全符合您的要求。把所有的荣誉都送给我

包常数;
使用基本qw/导出器/;
使用常数BOB=>666;
使用常数ALICE=>555;
次级进口{
没有严格的“参考文献”;
${[caller]->[0]。::'}{$\u}=${{{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
foreach grep{not/^(ISA | ISA | BEGIN | import | Dumper)$/}
密钥%{{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
}

谢谢。我真正需要的是关于省略“main”的那篇文章。我很好奇它是否真的有效。如果是这样,我将删除答案顶部的免责声明。这会将常量放在main::中,但现在您必须在所有常量前面加上main::Nathan:它可以工作。布莱恩:不,你只需要在它们前面加上$:。这是可以容忍的。FWIW,Perl5.10拒绝解析“package;”。有趣的是,这是处理常量的唯一答案(甚至这个问题实际上也没有涉及到它们)。