且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何组合两个字符串并将结果视为变量的名称?

更新时间:2023-01-19 21:41:56

首先 - 正如评论所说,请阅读以下内容:http://perl.plover.com/varvarname.html

First off - as the comments say read this: http://perl.plover.com/varvarname.html

这样做的方法是使用哈希.

The way to do this is with a hash.

my %stuff;

$stuff{'container0'} = "voila";
$stuff{'container1'} = "ssss"; 
$stuff{'container2'} = "swat";

my $value = int rand 3;

my $ans = $stuff{'container'.$value};
print $ans,"\n";

(需要 3 - int rand 2 只会给你 1 或 0).

(Needs to be 3 - int rand 2 will only ever give you 1 or 0).

但是,这可能比您需要的要复杂一些.

However, that's perhaps a bit more complicated than you need.

相反:

my @container = qw ( voila ssss swat );
print $container[rand @container],"\n"; 

为了完整起见 - 您可以这样做:

For the sake of completeness - here is how you can do it:

你永远不应该这样做,这是一个可怕的想法!它会以各种晦涩的方式破坏您的代码,并且在您拥有哈希时完全冗余

$container0 = "voila";
$container1 = "ssss"; 
$container2 = "swat"; 

$container3 = int rand 3; 

$ans = 'container'."$container3"; 

print $$ans,"\n"; 

这会在 strictwarnings 下给你错误.

This will give you errors under strict and warnings.

Can't use string ("container1") as a SCALAR ref while "strict refs" in use

这样做是有充分理由的.我说真的,当你有非常好的散列可用时,再重复一遍这个想法是多么糟糕.有各种各样令人难以置信的时髦方式破坏代码中的随机变量可能会导致灾难性的错误.认真 - 阅读上面的链接.它有一些可爱的例子.***是如果你不小心篡改了一些 special 变量,比如 $*$/ 并搞砸了每个正则表达式或文件句柄在程序的其余部分 - 你会在错误的地方寻找错误的来源.

There is a good reason that it does this. I seriously, cannot re-iterate enough how terrible an idea it is to do this when you've perfectly good hashes available. There are all sorts of incredibly funky ways that clobbering random variables in your code can go disastrously wrong. Seriously - read the link above. It has some lovely examples. The best being if you accidentally tamper with some of the special variables, such as $* or $/ and screw up every regular expression or filehandle in the rest of your program - you'll be looking in the wrong place for the source of the error.

要继续评论 - 如果你这样做,你不能使用 my - 你需要使用 our.理解原因的最简单方法是想象 my 将变量重命名为函数唯一名称,并将其隐藏"在包的其余部分中.因为符号 ref 在 run 时进行评估 - perl 在编译和验证阶段无法预先准备,因此在运行时无法看到"词法范围的变量.

To follow on from a comment - you cannot use my if you do this - you need to use our. The easiest way to understand why, is just imagine that my renames a variable to a function unique name, and 'hides' it from the rest of the package. Because the symbolic ref evaluates at run time - perl can't pre-prepare when it's doing it's compilation and validation phases, so cannot 'see' the lexically scoped variable when it's running.

使用 our 将在更广泛的范围内(例如有效地全局)声明变量.就这样:(不过还是很恶心)

Using our would declare the variable in a broader scope (e.g. effectively global). So like this: (But it's still nasty)

use strict;
use warnings;

no strict "refs"; 

our $container0 = "voila";
our $container1 = "ssss"; 
our $container2 = "swat"; 

my $container3 = int rand 3; 

my $ans = 'container'."$container3"; 

print $$ans,"\n"; 

另请参阅常见问题解答以了解更多原因,这不是一个好主意:

Also refer to the FAQ for some more reasons it's not a good idea:

http://perldoc.perl.org/perlfaq7.html#How-can-I-use-a-variable-as-a-variable-name%3f