如何使用 mod_perl 导出只读变量?
我试图通过创建一个常量来更容易地遵循一些 Perl 最佳实践
模块,导出整本书中使用的几个标量。 特别是 $EMPTY_STRING
,我几乎可以在我编写的每个 Perl 脚本中使用它。 我想要的是自动导出这些标量,这样我就可以使用它们,而无需在每个脚本中显式定义它们。
#!perl
package Example::Constants;
use Exporter qw( import );
use Readonly;
Readonly my $EMPTY_STRING => q{};
our @EXPORT = qw( $EMPTY_STRING );
示例用法:
#!perl
use Example::Constants;
print $EMPTY_STRING . 'foo' . $EMPTY_STRING;
使用上面的代码会产生错误:
Global symbol "$EMPTY_STRING" requires explicit package name
如果我将 Readonly
声明更改为:
Readonly our $EMPTY_STRING => q{}; # 'our' instead of 'my'
错误将变为:
Attempt to reassign a readonly scalar
这对于 mod_perl 来说是不可能的吗?
I'm trying to make it easier to follow some Perl Best Practices by creating a Constants
module that exports several of the scalars used throughout the book. One in particular, $EMPTY_STRING
, I can use in just about every Perl script I write. What I'd like is to automatically export these scalars so I can use them without defining them explicitly in each script.
#!perl
package Example::Constants;
use Exporter qw( import );
use Readonly;
Readonly my $EMPTY_STRING => q{};
our @EXPORT = qw( $EMPTY_STRING );
An example usage:
#!perl
use Example::Constants;
print $EMPTY_STRING . 'foo' . $EMPTY_STRING;
Using the above code produces an error:
Global symbol "$EMPTY_STRING" requires explicit package name
If I change the Readonly
declaration to:
Readonly our $EMPTY_STRING => q{}; # 'our' instead of 'my'
The error becomes:
Attempt to reassign a readonly scalar
Is this just not possible with mod_perl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有 4 个问题:
strict
和warnings
编译指示base
编译指示包含导出器(因为它设置@ISA
为您)our
变量)这是更正后的模块。
嗯,我错过了关于尝试分配给只读的部分,听起来模块被加载了不止一次。 我相信 mod_perl 有一个 机制 用于加载与脚本本身。 此加载仅发生一次,因此您应该使用它。
You had 4 problems:
strict
andwarnings
pragmasbase
pragma (since it sets@ISA
for you)our
variables) can be exportedHere is the corrected module.
Hmm, I missed the bit about attempting to assign to a readonly, it sounds like the module is getting loaded more than once. I believe mod_perl has a mechanism for loading modules separate from the scripts themselves. This loading happens only once, so you should be using it.
我是 Readonly 模块的作者。 Readonly 的下一个版本将提供对 mod_perl 的支持,正是因为这个问题。
我知道这现在无法解决您的问题,但是......好吧,我正在努力:-)
-- Eric
I'm the author of the Readonly module. The next version of Readonly will provide support for mod_perl, specifically because of this problem.
I know this doesn't solve your problem now, but... well, I'm working on it :-)
-- Eric
我没有方便测试的 mod_perl 实例,所以我无法测试这些建议。 我希望他们能成功。
尝试使用 Scalar::Util::readonly 检查变量是否已标记为只读。
您还可以尝试
use vars
:您还可以使用 typeglob 常量:
使用 typeglob 常量似乎很完美,因为该技术的巨大限制(它需要全局包)在这里不是问题。
I don't have a mod_perl instance handy to test with, so I can't test these suggestions. I hope they pan out.
Try using
Scalar::Util::readonly
to check if the variable has already been marked read only.You could also try
use vars
:You could also use a typeglob constant:
Using a typeglob constant seems perfect, since the big limitation of the technique (it requires a package global) is not an issue here.