Perl:解除引用哈希散列的哈希值

dre*_*mer 4 arrays perl hash perl-data-structures

考虑示例代码:

$VAR1 = {
      'en' => {
              'new' => {
                       'style' => 'defaultCaption',
                       'tts:fontStyle' => 'bold',
                       'id' => 'new'
                     },
              'defaultCaption' => {
                                  'tts:textAlign' => 'left',
                                  'tts:fontWeight' => 'normal',
                                  'tts:color' => 'white',

                                }
            },
      'es' => {
              'defaultSpeaker' => {
                                  'tts:textAlign' => 'left',
                                  'tts:fontWeight' => 'normal',

                                },
              'new' => {
                       'style' => 'defaultCaption',
                       'tts:fontStyle' => 'bold',
                       'id' => 'new'
                     },
              'defaultCaption' => {
                                  'tts:textAlign' => 'left',
                                  'tts:fontWeight' => 'normal',

                                }
            }
    };
Run Code Online (Sandbox Code Playgroud)

我将它作为参考返回,返回\%hash

我怎么解除这个?

rjh*_*rjh 7

%$hash.有关更多信息,请参见http://perldoc.perl.org/perlreftut.html.

如果函数调用返回了哈希值,则可以执行以下任一操作:

my $hash_ref = function_call();
for my $key (keys %$hashref) { ...  # etc: use %$hashref to dereference
Run Code Online (Sandbox Code Playgroud)

要么:

my %hash = %{ function_call() };   # dereference immediately
Run Code Online (Sandbox Code Playgroud)

要访问哈希值中的值,可以使用->运算符.

$hash->{en};  # returns hashref { new => { ... }. defaultCaption => { ... } }
$hash->{en}->{new};     # returns hashref { style => '...', ... }
$hash->{en}{new};       # shorthand for above
%{ $hash->{en}{new} };  # dereference
$hash->{en}{new}{style};  # returns 'defaultCaption' as string
Run Code Online (Sandbox Code Playgroud)