I am passing an array of references by reference to a subroutine. When I try to deference it in a sub-routine, it gives a flattened hash. How can I fix this? I don't want to have a flat hash and I am unable determine the reason for this.
I am sure that I am making a mistake somewhere but not able to spot it. Any comments/suggestions are totally welcome! Looking forward to hear from this wonderful community! Thanks in advance.
updated problem statement: Basically I am looking to pass a hash by reference to a sub-routine. And my issue is that when I accept it in the subroutine with a scalar variable, and then I try to de-reference it with % symbol, I still get a flat hash.
update: There was a confusion.As I was checking whether my hash is flat or not - I checked only with print Dumper %hash when I should actually have actually checked with print Dumper \%hash. Lack of this piece of information caused this issue.
Script:
#!/usr/bin/perl
use strict ;
use warnings ;
use Data::Dumper ;
my %h = (moe => "joe", toe => "poe") ;
my @a = (1,2,3,4) ;
my @refs = \(%h,@a) ;
sub sample()
{
my $ref = shift ;
my @refs = @{$ref} ;
print "What I got in the sub! Seems OK!!\n" ;
print Dumper @refs, "\n" ;
my %h = %{$refs[0]} ;
my @a = @{$refs[1]} ;
print "I am not able to dereference this :(. Please help!! Hash is flat :(\n" ;
print Dumper %h ;
print Dumper @a ;
}
&sample(\@refs) ;
OUTPUT:
23:34:17[Host@User]$ ./test.pl
What I got in the sub! Seems OK!!
$VAR1 = {
'moe' => 'joe',
'toe' => 'poe'
};
$VAR2 = [
1,
2,
3,
4
];
$VAR3 = '
';
I am not able to dereference this :(. Please help!! Hash is flat :(
$VAR1 = 'moe';
$VAR2 = 'joe';
$VAR3 = 'toe';
$VAR4 = 'poe';
$VAR1 = 1;
$VAR2 = 2;
$VAR3 = 3;
$VAR4 = 4;
&. I can only imagine that this has been written as a "find all the errors" homework task. That would be fine, but please say so in your question. - BorodinData::Printer, can you explain? I've always preferred the output ofData::Dumpto that ofData::Dumper, but only because its default output is more concise. There is also the option ofJSONandYAML, and I don't see why yet another unparseable format is better than any of those. - Borodinpis nicer. Maybe it's a preference, I'm not sure. But the other ones you mentioned are all for transferring data. DDP is for you reading it quickly, and only for that. - simbabque<pre>around it I'm good with most if them. Whatever is available and gets the job done. :-) - simbabque