We are trying to pass multiple hashes together with bunch of scalars as arguments to a subroutine. The problem is in multiple calls of this subroutine, ( and if we print the two hashes after we retrieved them inside the function), only one of them ( the first) is getting proper value. The second one is coming off as null. We tried bunch of things -
1 ) two separate hashes with different syntaxes from Stackoverflow/PerlMonks etc with pass on as reference.
&mySub(\%hash0, \%hash1, $var0, $var1, $var2);
sub mySub{
my (%hash0, %hash1, $var0, $var1, $var2) = (@_);
}
OR
&mySub(\%hash0, \%hash1, $var0, $var1, $var2);
sub mySub{
my %hash0 = %{$_[0]};
my %hash1 = %{$_[1]};
my $var0 = $[2]; my $var1 = $[3]; my $var3 = $[4];
}
2 ) Create array of two hashes and pass
my @joined_arr = (\%hash0, \%hash1);
&mySub (\@joined_arr, $var0, $var1, $var2);
sub mySub{
my (@joined_arr, $var0, $var1, $var2) = (@_);
my %hash0 = %{joined_arr[0]};
my %hash1 = %{joined_arr[1]};
# rest of the variables.
}
4 ) Create hash of hashes and pass
my %joined_hash;
%joined_hash{"first_one"} = %hash0;
%joined_hash{"second_one"} = %hash1;
&mySub (\%joined_hash, $var0, $var1, $var2);
sub mySub{
my %joined_hash, %hash0, %hash1;
%joined_hash = %{$_[0]};
%hash0 = %joined_hash{"first_one"};
%hash1 = %joined_arr{"second_one"};
# rest of the variables.
}
- Try them in Perl 5.16 ( default distribution of CentOS 7) and hen in Perl 5.30.
Till this point, this hasn't been a success. If anybody has an idea and like to share, it will be great help.
EDIT
Following the suggestion of @zdim and @Polar Bear, I have tried these things ->
Syntax for offloading @_ into scalars inside function ->
a)
my ($ref_to_hash0, $ref_to_hash1, $var0, $var1, $var2) = @_;
b)
$ref_to_hash0 = shift;
$ref_to_hash1 = shift;
$var0 = shift;
$var1 = shift;
$var2 = shift;
I have also tried these 3 style of hash-reference to hash assignment.
a)
my %local_hash_shallow_copy = %$ref_to_hash0;
b)
my $local_hashref_deep_copy = dclone $ref_to_hash0;
c)
my %local_hash_shallow_copy = %{$ref_to_hash0};
It seems out of 9 iterations of this sub call, I am getting right hash inside the sub 2 times. At other times I simply get a pointer dumped -
$VAR1 = {
'HASH(0x1e32cc8)' => undef
};
I am using Dumper to dump the hashes just Outside - right before the sub call, and just Inside - right after I transferred the value from ref to actual hash. This should avoid any silly mistake.
Either I might be doing a very basic mistake here or hit upon an uncanny issue. Am debugging it.
FYI.