我想在另一个perl变量中插入一个标量变量.例如:
my $var = "test";
my $test_1 = "DONE\n";
Run Code Online (Sandbox Code Playgroud)
我想$test_1通过利用来打印价值$var.我想先插入$var并得到它的值$test_1.我们能做到吗?
...
$hello = "ciao";
$$hello = "salut"; # $ciao = "salut";
print $ciao; # prints "salut"
...
Run Code Online (Sandbox Code Playgroud)
不推荐这种做法.
为了适合您的数据,您可以这样做:
$var = "test";
${$var."_1"} = "done\n";
print $test_1; # prints `done\n`
Run Code Online (Sandbox Code Playgroud)
使用哈希会更好.
...
my %hash = ();
my $key = "ciao"; # in the example seen before this was `$hello`
$hash{$key} = "salut"; # $hash{ciao} = "salut";
print $hash{ciao}; # will print "salut".
...
Run Code Online (Sandbox Code Playgroud)