title: "Not an ARRAY reference" in Perl
body: <p>I'm new to Perl and trying to get used to data structures and references in Perl.</p>
  
  <p>I learned</p>
  
  <ul>
  <li><code>key %hash</code> returns an array of the keys in <code>%hash</code></li>
  <li><code>\{ @array }</code> returns the reference to the <code>@array</code></li>
  </ul>
  
  <p>So I combined these two and wrote something like this,</p>
  
  <pre class="lang-perl prettyprint-override"><code>use strict;
  use warnings;
  use Data::Dumper;
  
  my $hash = {
      key1 =&gt; 'value1',
      key2 =&gt; 'value2'
  };
  
  my $keys = \{ keys %$hash }; # Supposed to be an array reference?
  
  print Dumper $keys; # Output 1
  print Dumper $keys-&gt;[0]; # Output 2
  </code></pre>
  
  <p>which yielded the error <code>Not an ARRAY reference</code> at the line of <code>Output 2</code>. Also, <code>Output 1</code> shows something that looks like a hash reference though it's supposed to be an array reference. </p>
  
  <p>What's wrong with my code?</p>
  
  <p>Similarly, the following code didn't work with the same error.</p>
  
  <pre class="lang-perl prettyprint-override"><code>use strict;
  use warnings;
  
  my $array = [1, 2, 3, 4, 5];
  my $first_two = \{ @{ $array }[0..1] }; # Isn't it an array ref?
  my $first = $first_two-&gt;[0];
  </code></pre>
  
  <p>I guess I misunderstand something about array references.</p>