You can't see it from %ENV and event if you run declare -a in perl, it still won't pull the arrays defined in the immediate outer scope.
You might have to work both ends for this. And it's not that easy, but here's a start.
Passing to Perl
First, I created a bash function to ready this value to be passed.
function pass_array
{
# get the array name
declare array_name=$1;
# get all declarations for defined arrays.
# capture the line that corresponds with the array name
declare declaration=$(declare -a | grep --perl-regex --ignore-case "\\b$array_name\\b");
# - At this point this should look like:
# declare -a* array_name='([0]="value1", [1]="value2", [2]="value3")'
# - Next, we strip away everything until the '=', leaving:
# '([0]="value1", [1]="value2", [2]="value3")'
echo ${declaration#*=};
}
You could pass it to perl like so:
perl -Mstrict -Mwarnings -M5.014 -MData::Dumper -e 'say Data::Dumper->Dump( [ \@ARGV ], [ q[*ARGV] ] )' "$(pass_array locDbList)"
On the Perl side, you might have a convenience function like this:
sub array_from_bash {
return shift =~ m/\[\d+\]="([^"]*)"/g;
}
Named Array
Of course, you could want to retain the name of the array, to allow passing more than one, or locating it dynamically, say with other environment variables. (export DB_LIST_NAME=locDBList)
In that case, you might want to change what the pass_array bash function echoes.
echo echo ${declaration#*-a[b-z]* };
And might want to have a Perl function like this:
sub named_array_from_bash {
return unless my $array_name = ( shift // $_ );
my $arr_ref = [ @_ ? @_ : @ARGV ];
return unless @$arr_ref
or my ( $array_decl ) =
grep { index( $_, "$array_name='(" ) == 0 } @$arr_ref
;
return array_from_bash( substr( $array_decl, length( $array_name ) + 1 ));
}
Environment Variable
Yet, another idea is simply export a variable with the same information:
declare decl=$(declare -a | grep --perl-regex --ignore-case "\\b$array_name\\b");
export PERL_FROM_BASH_LIST1=${decl#*-a[a-z]* };
And read that from Perl.
my @array = array_from_bash( $ENV{PERL_FROM_BASH_LIST1} );
locDbList=(default default_test)