Code:
# No "my" here
$a1 = "A" ;
$a2 = "B" ;
$a3 = "C" ;
foreach my $k ( 1,2,3 )
{
my $msg = ${"a${k}"};
print "$msg\n" ;
}
This is symbolic reference, and is not typically recommend. It works for variables in the symbol table only, so that means you cannot use this trick for variables that are lexically scoped (i.e. "my").
Are you really sure you want this? I can't think of any reason to advocate such constructs in typical programs except specialized modules that need to mess with the symbol table directly.