How to get the data开发者_如何学C output from a perl subroutine..? Pass an hash reference to subroutine "get_data"... fill the data inside the subroutine.. and it should reflect outside.
ex:
my %myhash = ();
get_data(\%myhash);
use strict;
use warnings;
use Data::Dumper;
my %myhash = ();
get_data(\%myhash); #pass hash ref
$myhash{k2} = "Hello SO"; #add one more key value
print Dumper($hash_ref); #Dump hash ref
sub get_data{
my $hash_ref = shift; #get hash ref
$hash_ref->{k1} = "adding one more key value"; #fill data
}
output:
$VAR1 = {
'k2' => 'Hello SO',
'k1' => 'adding one more key calue'
};
You are passing the hash by reference, any changes in the hash would be visible outside the subroutine as well.
Did you face any problem with this code?
精彩评论