如何扩展文本字符串中的变量? [英] How can I expand variables in text strings?

查看:55
本文介绍了如何扩展文本字符串中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Perl 脚本,我在其中从包含变量名称的配置文件中读取一行.当我将这一行读入一个变量时,需要用给定值替换的变量名没有输入.我使用的是 Config::Tiny 访问我的配置文件.示例:

I have a Perl script where I read in a line from a configuration file that contains a variable name. When I read the line into a variable the variable name that needs to be replaced with the given value is not put in. I am using Config::Tiny to access my configuration file. Example:

thing=I want to $this

脚本

$this = "run";
my $thing = $Config->{thing};
print $thing;

<小时>

print $thing 显示为 I want to $this.我希望它以我要运行的形式出现.


print $thing comes out as I want to $this. I want it to come out as I want to run.

推荐答案

好的,所以您希望 Perl 评估您的字符串而不是打印它.

OK, so you're wanting Perl to evaluate your string as opposed to print it.

这实际上包含在这个 Perl 常见问题解答中:如何扩展文本字符串中的变量?

This is actually covered in this Perl FAQ: How can I expand variables in text strings?

简而言之,您可以使用以下正则表达式:

In short, you can use the following regular expression:

$string =~ s/(\$\w+)/$1/eeg;

小心:从脚本外部评估任意字符串是一个主要的安全风险.一个优秀的黑客可以利用它在您的服务器上执行任意代码.

BE CAREFUL: Evaluating arbitrary strings from outside of your scripts is a major security risk. A good hacker can exploit this to execute arbitrary code on your server.

Brian 的常见问题解答涵盖了其中的一些内容.

Brian's FAQ answer covers some of this.

这篇关于如何扩展文本字符串中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆