estreb has asked for the wisdom of the Perl Monks concerning the following question:
And here is the output:#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %records = (); my $ref = \%records; print "Before call to DoStuff\n"; print "Hash reference: '$ref'\n"; print Dumper(\%records); DoStuff(\%records); print "Back from call to DoStuff\n"; print Dumper(\%records); exit(0); sub DoStuff { my $hash_ref = shift; print("In DoStuff: "); print "Hash reference: '$hash_ref'\n"; my %temp_records = %{ $hash_ref }; $temp_records{"foo"} = "bar"; print Dumper(\%temp_records); return; }
After the call to DoStuff(), why is the "foo" key gone? How do I fix this?Before call to DoStuff Hash reference: 'HASH(0x1da6e78)' $VAR1 = {}; In DoStuff: Hash reference: 'HASH(0x1da6e78)' $VAR1 = { 'foo' => 'bar' }; Back from call to DoStuff $VAR1 = {};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sending a hash reference to a subroutine, and retain its values after returning from that subroutine
by Anonymous Monk on Dec 19, 2014 at 16:00 UTC | |
|
Re: Sending a hash reference to a subroutine, and retain its values after returning from that subroutine
by builat (Monk) on Dec 21, 2014 at 04:30 UTC | |
by GotToBTru (Prior) on Dec 30, 2014 at 02:50 UTC |