开发者

How to get the output data from perl subroutine passing a reference

开发者 https://www.devze.com 2023-02-17 22:05 出处:网络
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.

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消