Perl 变量值代表另一个变量 [英] Perl Variable value is representing another variable
问题描述
my $var = "Hello";
my $Hello = "Hi";
我的问题是:如何在 $var 中替换 $Hello 的值?这里 $var 包含字符串Hello"(有一个同名的变量 $Hello).我正在寻找一种用变量 $Hello 的值替换它的方法.请让我知道我该怎么做?
my question is: how can I substitute the value of $Hello in $var? Here $var contains string "Hello" (there is a variable with same name $Hello). I am looking for a way to substitute it with the value of variable $Hello. Please let me know how can I do that?
推荐答案
这在 Perl FAQ (7) 中有讨论:如何使用变量作为变量名?.
This is discussed in Perl FAQ (7): How can I use a variable as a variable name?.
简而言之,答案是:不要这样做.改用哈希.
In short, the answer is: Don't do it. Use hashes instead.
更长的答案:
更好、更简单的解决方案是将值存储在哈希中:
my %sayings = ("Hello" => "Hi");
my $key = "Hello";
my $var2 = $sayings{$key};
<小时>
你也可以使用 eval:
You can also use eval:
my $var = "Hello";
my $Hello = "Hi";
print "1.$var\n";
my $var2 = eval "\$$var";
print "2.$var2\n";
<小时>
作为最后的手段,您可以使用符号引用.但是(如上面链接的常见问题解答中所述),它们仅适用于全局变量,并且仅在 `use strict 'refs` 无效时才起作用 - 对于正常的 Perl 开发来说应该始终如此.
As a last resort, you COULD use symbolic references. HOWEVER (as discussed in the FAQ linked above), they ONLY work on global variables and ONLY when `use strict 'refs`` is not in effect - which it should always be for normal Perl development.
##############################################################
# This works. But only without "use strict" and on a package (global) variable.
##############################################################
no strict; # BOO! Bad coder!
my $var = "Hello";
$Hello = "Hi"; #Look, ma, GLOBAL VARIABLE! Bad Coder!
print "1.$var\n";
my $var2 = ${$var}; # You could use $$var shorthand
print "2.$var2\n";
# OUTPUT:
# 1. Hello
# 2. Hi
##############################################################
# I meant it about package (global variable)
##############################################################
no strict;
my $var = "Hello";
$Hello = "GLOBAL value";
my $Hello = "Hi";
print "1.$var\n";
my $var2 = ${$var}; # You could use $$var shorthand
print "2.$var2\n";
# OUTPUT - see how we don't get "Hi" on a second line
# If we did not assign "GLOBAL Value" to $Hellp on line 3
# , second line would contain no value at all (e.g. "2.")
# 1. Hello
# 2. GLOBAL value
##############################################################
# And, if you do what you SHOULDA done and used strict:
##############################################################
use strict; # Much better!
my $var = "Hello";
my $Hello = "Hi";
print "1.$var\n";
my $var2 = ${$var}; # You could use $$var shorthand
print "2.$var2\n";
# OUTPUT:
# Global symbol "$Hello" requires explicit package name at line 4.
# Execution aborted due to compilation errors.
<小时>
附言如果你只是想使用 $Hello
硬编码变量的值,你可以做 $var2 = $Hello;
,但我感觉你的意思是你想使用任何一个变量名包含在 $var 中.
P.S. If you simply want to use the value of $Hello
hardcoded variable, you can do $var2 = $Hello;
, but I have a feeling you meant you want to use whichever variable's name is contained in $var.
这篇关于Perl 变量值代表另一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!