Please note that this has NOTHING to do with the fact that you are using variables to contain the name of other variable.
The reason this doesn't work is because ${"var_a"}
construct in reality refers to a package level variable $main::var_a
.
Since $var_a
is declared as a lexical variable, it's a DIFFERENT identifyer, and therefore ${"var_a"}
is undef.
You can see that if you change my $var_a
to our $var_a
our $var_a="a";
my $var_b="b";
$var_c="c";
print ${"var_a"},"\n";
print ${"var_b"},"\n";
print ${"var_c"},"\n";
######## RESULTS:
a
c
As others noted, while there is a good explanation for why what you are trying to do doesn't work, WHAT you are doing is likely the wrong approach. You should almost NEVER use this method unless there's no better way; without your problem it's not clear what the better way is but most likely would be a hash like TLP's answer says.