perlfan has asked for the wisdom of the Perl Monks concerning the following question:
I was expecting the underlying anonymous hashes to be replaced, but they are not.use strict; use warnings; require Data::Dumper; my $foo = { # blessed one => 1, two => 2, six => 6, }; bless $foo, q{Number::3ch}; my $bar = { # not blessed one => 1, two => 2, six => 6, }; sub foo { # pass by ref my $ref = shift; $ref->{one} = q{One}; $ref = {}; # why won't this replace the underlying hash? return $ref; } # try on blessed reference print Data::Dumper::Dumper($foo); foo $foo; print Data::Dumper::Dumper($foo); # try on unblessed reference print Data::Dumper::Dumper($bar); foo $bar; print Data::Dumper::Dumper($bar);
Results:
Questions:$VAR1 = bless( { 'six' => 6, 'two' => 2, 'one' => 1 }, 'Number::3ch' ); $VAR1 = bless( { 'six' => 6, 'two' => 2, 'one' => 'One' }, 'Number::3ch' ); $VAR1 = { 'six' => 6, 'one' => 1, 'two' => 2 }; $VAR1 = { 'six' => 6, 'one' => 'One', 'two' => 2 };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: unexpected behavior on hash passed by reference
by haukex (Archbishop) on Feb 26, 2022 at 18:40 UTC | |
by perlfan (Parson) on Feb 26, 2022 at 18:47 UTC | |
by haukex (Archbishop) on Feb 26, 2022 at 18:56 UTC | |
by LanX (Saint) on Feb 27, 2022 at 00:15 UTC | |
|
Re: unexpected behavior on hash passed by reference
by LanX (Saint) on Feb 27, 2022 at 00:29 UTC |