Domanda

Questo codice funziona:

  my $href = shift @_;    # get reference to hash
  my %h = %{$href};       # dereference hash

Questo non:

  my %h = %{shift @_};

Oltre a questo:

  my %h = ${$_[0]}

Perché?

=============================

Ancora una volta di essere precisly:

 1 #!/usr/bin/perl -w
  2 use strict;
  3 use warnings;
  4 
  5 my %h;
  6 sub a {
  7     
  8     # that works - result 1
  9     my $href = $_[0] || shift;
 10     %h = %{$href};
 11     
 12     # that does not work - result 0
 13     # my %h = %{$_[0]};
 14     
 15     # as well as that one - result 0
 16     # my %h = %{shift @_};
 17     $h{1}=2;
 18 }
 19 
 20 a(\%h);
 21 print scalar (keys %h) . "\n";

In altre parole linea 16 - non

.
È stato utile?

Soluzione

Questo lavoro.

  my %h = %{shift @_};

Questa non sarà.

  my %h = ${$_[0]} # not ${$_[0]}

Questo sigillo si suppone che sia %

  my %h = %{$_[0]}

Inoltre, use warnings;, e use strict;


Suggerimento: Il motivo per il tuo esempio di cui sopra non funziona, è solo un esempio non dichiarare una %h = %{$href}; variabile lessicale non è my %h = %{$href};

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top