breeze has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I use perl 5.10.1_5 at FreeBSD and try execute this code:

#!/usr/bin/perl use utf8; use Encode; use strict; use warnings; use Data::Dumper; my $locale = "EN"; my $setSwitchName = "UNINSTALL"; my $content = { 'EN' => { 'SWITCH' => { 'INSTALL' => { 'ACTION' => 'a1', 'ICONID' => 'i1', 'CAPTION' => 'c1', 'DIZ' => 'd1' }, 'UNINSTALL' => { 'ACTION' => 'a2', 'ICONID' => 'i2', 'CAPTION' => 'c2', 'DIZ' => 'd2' } }, 'ACTION' => 'a3', 'ICONID' => 'i3', 'CAPTION' => 'c3', 'DIZ' => 'd3' } }; my $switchValues = $content->{$locale}->{'SWITCH'}; print ("switchValues=".Dumper($switchValues)."\n"); my $setSwitchValue = $switchValues->{$setSwitchName}; my $tmpContent = $setSwitchValue; $tmpContent->{'SWITCH'} = $switchValues; my $newContent->{$locale} = $tmpContent; print ("newContent=".Dumper($newContent));

I wanna create new HASH with changed part defined by $setSwitchName, but when i look at th e dump of $newContent i see next:

newContent=$VAR1 = { 'EN' => { 'SWITCH' => { 'INSTALL' => { 'ACTION' => 'a1', 'ICONID' => 'i1', 'DIZ' => 'd1', 'CAPTION' => 'c1' }, 'UNINSTALL' => $VAR1->{'EN'} }, 'ACTION' => 'a2', 'ICONID' => 'i2', 'DIZ' => 'd2', 'CAPTION' => 'c2' } };

Why the value of 'UNINSTALL' is $VAR1->{'EN'} ???

I Am doing something wrong or is this a problem in Perl or what?

Replies are listed 'Best First'.
Re: Perl. Problem with HASH value
by choroba (Cardinal) on Dec 16, 2011 at 18:17 UTC
    This is not a problem in Perl. You told Perl what to do:
    $tmpContent->{'SWITCH'} = $switchValues;
    You probably meant this:
    my $tmpContent = { %$setSwitchValue };
    i.e. do not assign a reference to the old value to tmpContent, but a reference to a newly created hash with the values you want to keep.
      big thanks! this is what i need exactly!
Re: Perl. Problem with HASH value
by Anonymous Monk on Dec 16, 2011 at 18:04 UTC

    See Tutorials: References

    #!/usr/bin/perl -- use strict; use warnings; my @foo = 0..3; my @bar = 3..6; my %blah = ( foo => \@foo, stillfoo => \@foo, bar => \@bar ); $blah{"why YES, this too is foo"} = \@foo; $blah{"and this too is bar"} = \@bar; my $THIS_IS_FOO_TOO = \@foo; my $THIS_IS_BAR_TOO = \@bar; ## and now, lets modify $foo[1] five different ways $foo[1] .= ' you '; $blah{foo}[1] .= ' are '; $blah{stillfoo}[1] .= ' still '; $blah{"why YES, this too is foo"}[1] .= ' the '; $THIS_IS_FOO_TOO->[1] .= ' @foo '; print "$foo[1]\n"; __END__
    The output is 1 you are still the @foo

    See also http://learn.perl.org/books/beginning-perl/ and the Modern Perl Book