Search code examples
perlhashreferencemod-perlperl-data-structures

How can I return a whole hash map in Perl?


I have a hash called

%values

Now I want to return the whole hash in a subroutine

sub getvalues {
  return $values;
}

But then I got an error, because $value needs a definition and my program stops. If I'm using

sub getvalues {
  return %values;
}

it seems to work, but my program is very slow and don't get further... So how can I return the whole map?


Solution

  • It would be nice to return the hash reference instead of hash,what you need to do is

    First stote the hash into the hash ref then return it like

    sub getvalues {
    
        my %values = (test => "SO");
    
        my $values = \%values;
    
        return $values;
      }