1

単純な Hash of Hash 参照を検討してください。(内部の) ハッシュを参照解除し、その値の一部を変更すると、これはハッシュの元のハッシュに転送されません。しかし、矢印表記ではそうです。私がチェックした矢印表記はどこでも単にショートカットとして説明されているので、何が得られますか?

use Data::Dumper;

$HoH{"one"}={'f1' => "junk",
      'f2' => 0};

$href = $HoH{"one"};
%hh=%{$HoH{"one"}};

print Dumper($href);

$href->{'f2'}=1;
$href->{'f1'}="newJunk";

print Dumper($HoH{"one"});

$hh{'f2'}=0;
$hh{'f1'}="oldJunk";

print Dumper($HoH{"one"});
4

2 に答える 2

2

コードには 3 つのハッシュがあります。

  1. によって作成されたアノン 1 {}。(これからは %anon と呼びます)
  2. %HoH
  3. %hh

$HoH{"one"}={'f1' => "junk",'f2' => 0};
                            # $HoH{one} holds a ref to %anon

$href = $HoH{"one"};        # Copy the ref. $href holds a ref to %anon
%hh=%{$HoH{"one"}};         # Copy the hash referenced by $HoH{one} (i.e. %anon)

print Dumper($href);        # Dumps the hash referenced by $href (i.e. %anon)

$href->{'f2'}=1;            # Modifies the hash referenced by $href (i.e. %anon)
$href->{'f1'}="newJunk";    # Modifies the hash referenced by $href (i.e. %anon)

print Dumper($HoH{"one"});  # Dumps the hash referenced by $HoH{one} (i.e. %anon)

$hh{'f2'}=0;                # Modifies %hh
$hh{'f1'}="oldJunk";        # Modifies %hh

print Dumper($HoH{"one"});  # Dumps the hash referenced by $HoH{one} (i.e. %anon)

変更すると%hhに影響するのはなぜ%anonですか?

于 2013-08-15T03:12:08.570 に答える