string - 如何将一行中的哈希引用转换为perl中的常量
问题描述
我正在使用Sphinx::Search
.
此代码示例是否有更简单的方法将字符串转换为常量?
use Sphinx::Search;
my $config = {
x => 'SPH_MATCH_EXTENDED2',
};
my $x = $config->{x};
print Sphinx::Search->$x(); # output: 6
我使用了 如何在 Perl 中访问名称包含在变量中的常量的建议? 这个例子有效,但如果我总是使用哈希中的字符串,那么我是否需要将它放入一个单独的变量中才能以这种方式使用它?
my $x = $config->{x};
print Sphinx::Search->$x(); # output: 6
有没有一种方法可以解决这个问题?
# does not work
print Sphinx::Search->$config->{x}();
解决方案
您可以创建对该值的引用并立即取消引用它:
Sphinx::Search->${ \$config->{x} };
(如果没有参数,则()
是可选的)。
推荐阅读
- android - gRPC Android DEADLINE_EXCEEDED 但未向服务器发出请求
- python - 让python控制台在Windows任务栏中闪烁
- python - 在 Plotly 中创建具有多个标题和字幕轴的图形
- sql - 查询优化器是否能够从 WHERE 子句中提取有效常量以避免重复评估?
- javascript - 使用 AJAX POST/GET 方法解析 Base64 字符串时的未定义索引
- jquery - 有条件的切换类
- c# - 如果我不能在 C# 中调用析构函数,为了安全起见销毁对象的解决方案是什么?
- java - 滚动窗格中的 TextField 未正确显示
- macos - 如何使用 IOHIDManager 或蓝牙读取 iOS 键盘上的 home 键?
- php - incIude.php,在“l”位置用大写“i”拼写