是的,您可以在Perl中执行此操作.但对于Mark Dominus在这 三篇 文章中解释的所有原因,这是一个可怕的想法.
将这些值存储在哈希中是一个好主意.
#!/usr/bin/perl use strict; use warnings; my $params = { x => 1, y => 2}; my @params = qw(x y z a b c); my %var; foreach my $p (@params) { # You need to take care exactly what you want in this # logical statement. The options are: # 1/ $p exists in the hash # exists $params->{$p} # 2/ $p exists in the hash and has a defined value # defined $params->{$p} # 3/ $p exists in the hash and has a true value # $params->{$p} # I think the first option is most likely. The last one has # good chance of introducing subtle bugs. if ( exists $params->{$p} ) { $var{$p} = $params->{$p}; } } print join ', ', map { "$_: " . ($var{$_} // 'undef') } @params; print "\n";