The following is an abstraction that shows a way to do what you want. It will handle "... a lot of values ..." and can be extended (e.g. you could add support for hashrefs at some later time).
[ ~/tmp ] $ cat return_multi_refs
# return_multi_refs
use strict;
use warnings;
use constant REF_SYM_MAP => {
'' => ['$', '', ''],
SCALAR => ['$', '${', '}'],
ARRAY => ['@', '@{', '}'],
};
my $rh_params = subroutine();
spawn_vars($rh_params);
{
no strict qw(vars);
no warnings qw(once);
print "scalar1 [$scalar1]\nscalar2 [$scalar2]\nscalar3 [$scalar3]\
+n",
"array1[1] [$array1[1]]\narray2[2] [$array2[2]]\n";
}
exit;
sub subroutine
{
my @array1 = ( qw(a b c ) );
my $ra_array2 = [ qw(x y z) ];
my $scalar2 = 'Scalar the Second';
my $scalar3 = 'Scalar the Third';
my %params = (
scalar1 => 'Scalar the First',
array1 => \@array1,
scalar2 => \$scalar2,
array2 => $ra_array2,
scalar3 => $scalar3,
);
return \%params;
}
sub spawn_vars
{
eval join '', map {(
REF_SYM_MAP->{ref($_[0]->{$_})}[0], __PACKAGE__, '::', $_,
+ ' = ',
REF_SYM_MAP->{ref($_[0]->{$_})}[1], '$_[0]->{', $_, '}',
REF_SYM_MAP->{ref($_[0]->{$_})}[2], ';',
)} keys(%{$_[0]});
die $@ if $@;
}
Test output:
[ ~/tmp ] $ perl return_multi_refs
scalar1 [Scalar the First]
scalar2 [Scalar the Second]
scalar3 [Scalar the Third]
array1[1] [b]
array2[2] [z]
[ ~/tmp ] $