frazap has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use Data::Dumper; my $data = {status=>"ok",'message-type'=>"member",'message-version'=>" +1.0.0"}; my $result_hr; unfold_hash($data, $result_hr); print Dumper $result_hr; sub unfold_hash { my ( $raw_hr, $res_hr ) = @_; for my $k ( keys %$raw_hr ) { $res_hr->{$k} = $raw_hr->{$k}; } print "unfold_hash ", $res_hr ? scalar %$res_hr : 0, "\n"; }
Here, unfold_hash is rather stupid since it does nothing interesting. But the problem remains: $result_hr is undef.
To correct this I have to say
Now $result_hr holds the content of $data. How comes that the hash auto vivification is lost if the variable $result_hr is undef ?my $result_hr={}; unfold_hash($data, $result_hr); print Dumper $result_hr;
Thanks
F.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: passing a hash ref in a sub
by davido (Cardinal) on Oct 31, 2018 at 15:13 UTC | |
by jimpudar (Pilgrim) on Nov 01, 2018 at 17:13 UTC | |
|
Re: passing a hash ref in a sub
by haukex (Archbishop) on Oct 31, 2018 at 15:19 UTC | |
by davido (Cardinal) on Oct 31, 2018 at 15:21 UTC | |
by haukex (Archbishop) on Oct 31, 2018 at 15:34 UTC | |
by frazap (Monk) on Oct 31, 2018 at 15:43 UTC | |
|
Re: passing a hash ref in a sub
by tybalt89 (Monsignor) on Oct 31, 2018 at 15:11 UTC | |
|
Re: passing a hash ref in a sub
by Lotus1 (Vicar) on Oct 31, 2018 at 15:46 UTC | |
|
Re: passing a hash ref in a sub
by pDaleC (Sexton) on Nov 01, 2018 at 13:54 UTC | |
|
Re: passing a hash ref in a sub
by pwagyi (Monk) on Nov 02, 2018 at 04:15 UTC |