freddo411 has asked for the wisdom of the Perl Monks concerning the following question:
Considering the test code below, I'm wondering why I can't replace a "passed-by-reference" hashref with another hashref in my sub routine.
$|++; my $href = { one => 1, two => 2 }; print "\nBefore:\n"; print Dumper($href); set_element($href); print "\nset_element:\n"; print Dumper($href); assign_new_href($href); print "\nAttempt to assign new hashref:\n"; print Dumper($href); ########## sub assign_new_href { my $href = shift; $href = { new_key => 4 } ; } ########## sub set_element { my $href = shift; $href->{baz} = 4; }
The output (below) shows that the attempt to assign a new hashref fails silently. ?@#$%?.
Before: $VAR1 = { 'one' => 1, 'two' => 2 }; set_element: $VAR1 = { 'baz' => 4, 'one' => 1, 'two' => 2 }; Attempt to assign new hashref: $VAR1 = { 'baz' => 4, 'one' => 1, 'two' => 2 };
-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pass by reference question...
by chromatic (Archbishop) on Mar 05, 2012 at 18:57 UTC | |
|
Re: Pass by reference question...
by JavaFan (Canon) on Mar 05, 2012 at 19:01 UTC |