在Perl中,如何使用字符串作为变量名? [英] In Perl, how can I use a string as a variable name?
本文介绍了在Perl中,如何使用字符串作为变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何将变量用作在Perl中使用变量名?
Possible Duplicate:
How can I use a variable as a variable name in Perl?
这可行吗?我需要将字符串更改为变量.
Is this doable? I need to change a string into variable.
示例:
如果我有这样的变量:
$this_is_test = "what ever";
$default = "this";
$default = $default . "_is_test";
我希望$default
取$this_is_test
的值.
推荐答案
Along the lines of my other answer, whenever you find yourself adding string suffixes to a variable name, use a hash instead:
my %this = (
is_test => "whatever",
is_real => "whereever",
);
my $default = $this{is_test};
print "$default\n";
请勿不要使用符号引用,因为它们是不必要的,并且在您的问题中可能非常有害.有关更多信息,请参见为什么将变量用作变量名"是愚蠢的?,第2部分和第3部分,由 mjd .
Do NOT use symbolic references for this purpose because they are unnecessary and likely very harmful in the context of your question. For more information, see Why it's stupid to 'use a variable as a variable name'?, part 2 and part 3 by mjd.
这篇关于在Perl中,如何使用字符串作为变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文