Trying to interpolate that with an eval is an interesting solution, and it looks like it would work. I never considered trying to assemble the variable names using an eval. But the reason is an array just works better, and is probably the technically correct solution.
Instead of trying to concatenate "$var" and "$i" to form "$var1" inside an eval, just use an array and access $var[$i]. The code would look like this.
#!/usr/bin/perl -w
my @arrayOfReferences;
push @arrayOfReferences, [qw(this is array 1)];
push @arrayOfReferences, [qw(this is array 2)];
push @arrayOfReferences, [qw(this is array 3)];
push @arrayOfReferences, [qw(this is array 4)];
push @arrayOfReferences, [qw(this is array 5)];
push @arrayOfReferences, [qw(this is array 6)];
my @arrayOfMessages = qw(Message1 Message2 Message3 Message4 Message5 Message6);
for (my $i=0; $i < @arrayOfMessages; $i++){
&printmsg($arrayOfReferences[$i],$arrayOfMessages[$i]);
}
sub printmsg{
($arrayRef, $message) = @_;
print "Array Value: " . join(" ",@$arrayRef) . "\n"; #dereference pointer here
print "Message String: $message\n\n";
}
Output looks like this
perl evalVariableInterpolation.pl
Array Value: this is array 1
Message String: Message1
Array Value: this is array 2
Message String: Message2
Array Value: this is array 3
Message String: Message3
Array Value: this is array 4
Message String: Message4
Array Value: this is array 5
Message String: Message5
Array Value: this is array 6
Message String: Message6