如何在 Perl 中访问名称包含在变量中的常量?
我在 Perl 中声明了一组常量:
use constant C1 => 111;
use constant C2 => 222;
..
use constant C9 => 999;
my $which_constant = "C2";
如何构造一个 Perl 表达式,该表达式基于 $which_constant 派生出用该变量的值命名的常量值 - 例如“222”。
请注意,我无法更改上述任何条件 - 它们是真实场景的简化:我有一个模块(我无法控制)从中导入这些常量。常量之一的名称由用户从命令行提供。我需要访问适当的常量值。
我一直在用头撞墙(主要是围绕各种奇怪的全局结构),但它们都不起作用。
PS 如果解决方案访问其本机模块内的常量 - 比如 My::Constants::C2
(无需导入它们),那就更好,但不是必需的 - 我可以将正确的常量导入到 < code>main:: 轻松使用 My::Constants->import($which_constant)
。是的,最重要的是,默认情况下不会导出常量,因此需要显式 import() 调用。
我尝试过的一些事情:
main::$which_constant
- 语法错误main::${which_constant}
- 语法错误${*$which_constant}
- 返回空值*$which_constant
- 返回“*main::C2”${*${*which_constant}}
- 空
I have a set of constants declared in Perl:
use constant C1 => 111;
use constant C2 => 222;
..
use constant C9 => 999;
my $which_constant = "C2";
How do I construct a Perl expression which, based on $which_constant
, derives the value of a constant named with the value of this variable - e.g. "222".
Please note that I can not change any of the conditions above - they are a simplification of a real scenario: I have a module (which I have no control over) from which these constants are imported. The `name of one of the constants is supplied by the user from command line. I need to access the appropriate constants' value.
I've been beating my head against the wall (mostly around all sorts of weird glob constructs) but none of them work.
P.S. If the solution accesses the constants inside their native module - say My::Constants::C2
(without needing to import them), even better, but not necessary - I can import the correct constants into main::
easily using My::Constants->import($which_constant)
. and yes, to top it off, te constants are NOT exported by default thus needing the explicit import() call.
Some of the things I tried:
main::$which_constant
- syntax errormain::${which_constant}
- syntax error${*$which_constant}
- returns empty value*$which_constant
- returns "*main::C2"${*${*which_constant}}
- empty
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
constant.pm
定义的常量只是子例程。如果您在字符串中具有常量的名称,则可以使用方法调用语法:这样,如果您尝试使用未定义的常量,您将收到错误。
Constants defined by
constant.pm
are just subroutines. You can use method invocation syntax if you have the name of the constant in a string:This way, if you try to use a constant which is not defined, you will get an error.
Perl“常量”实际上是返回常量值的子例程。 Perl 编译器能够在编译时将它们替换为适当的值。但是,由于您希望根据运行时名称查找来获取值,因此您应该这样做:(
当然,您需要在某处
没有严格的“refs”
。)Perl "constants" are actually subroutines that return a constant value. The perl compiler is able to replace them with the appropriate value at compile time. However, since you want to get the value based on a runtime name lookup, you should do:
(And of course you need
no strict 'refs'
somewhere.)Sinan 建议使用方法调用语义来绕过严格的“refs”限制,这是最干净、最容易阅读的解决方案。
我对此唯一担心的是使用这种方法的速度损失可能是一个问题。我们都听说过方法调用性能损失和内联函数的速度优势。
所以我决定运行一个基准测试(代码和结果如下)。
结果表明,正常的内联常量的运行速度大约是使用字面子例程名称的方法调用的两倍,几乎是使用变量子例程名称的方法调用的三倍。最慢的方法是标准解引用和调用
无严格“refs”;
。但是,即使是最慢的方法,在我的系统上也非常快,每秒超过 140 万次。
这些基准消除了我对使用方法调用方法来解决此问题的保留意见。
结果:
在 Windows XP 上使用 ActivePerl 826 生成的结果,YMMV。
Sinan's suggestion to use method invocation semantics to get around
strict 'refs'
limits is the cleanest, easiest to read solution.My only concern about this was that the speed penalty for using this approach might be a problem. We've all heard about method call performance penalties and the speed benefits of inlineable functions.
So I decided to run a benchmark (code and results follow).
The results show that normal, inlined constants run about twice as fast as method calls with a literal subroutine name, and almost three times as fast as method calls with variable subroutine names. The slowest approach is a standard deref and invocation of
no strict "refs";
.But, even the slowest approach is pretty darn fast at over 1.4 million times a second on my system.
These benchmarks obliterate my one reservation about using the method call approach to solve this problem.
And the results:
Results generated with ActivePerl 826 on Windows XP, YMMV.