title: Perl DBI fetchall_hashref
body: <p>Consider the following table:</p>
  
  <pre><code>mysql&gt; select * from vCountryStatus;
  +-------------+------------+------+---------+--------+-----------------+
  | CountryName | CountryISO | Code | Status  | Symbol | CurrencyName    |
  +-------------+------------+------+---------+--------+-----------------+
  | Brazil      | BR         |   55 | LIVE    | BRL    | Brazilian Real  |
  | France      | FR         |   33 | offline | EUR    | Euro            |
  | Philippines | PH         |   63 | LIVE    | PHP    | Philippino Peso |
  +-------------+------------+------+---------+--------+-----------------+
  3 rows in set (0.00 sec)
  </code></pre>
  
  <p>I am trying to construct a hash based on this table. For this I do the following:</p>
  
  <pre><code>#!/usr/bin/perl
  
  use DBI;
  use Data::Dumper;
  
  my $dbh = DBI-&gt;connect("dbi:mysql:database=db", "user", "password", {RaiseError =&gt; 1, AutoCommit =&gt; 0, FetchHashKeyName =&gt; "NAME_lc"}) || die "DB open error: $DBI::errstr";
  
  my $sth = $dbh-&gt;prepare("select * from vCountryStatus");
  $sth-&gt;execute;
  my $hash = $sth-&gt;fetchall_hashref('countryiso');
  print Dumper($hash);
  </code></pre>
  
  <p>Here is the output this generates:</p>
  
  <pre><code>$VAR1 = {
            'PH' =&gt; {
                      'symbol' =&gt; 'PHP',
                      'status' =&gt; 'LIVE',
                      'countryname' =&gt; 'Philippines',
                      'countryiso' =&gt; 'PH',
                      'currencyname' =&gt; 'Philippino Peso',
                      'code' =&gt; '63'
                    },
            'BR' =&gt; {
                      'symbol' =&gt; 'BRL',
                      'status' =&gt; 'LIVE',
                      'countryname' =&gt; 'Brazil',
                      'countryiso' =&gt; 'BR',
                      'currencyname' =&gt; 'Brazilian Real',
                      'code' =&gt; '55'
                    },
            'FR' =&gt; {
                      'symbol' =&gt; 'EUR',
                      'status' =&gt; 'offline',
                      'countryname' =&gt; 'France',
                      'countryiso' =&gt; 'FR',
                      'currencyname' =&gt; 'Euro',
                      'code' =&gt; '33'
                    }
          };
  </code></pre>
  
  <p>The question is: why is the key of the hash (countryiso) repeated in the values inside the hash?</p>
  
  <p>What I would prefer is the following output:</p>
  
  <pre><code>$VAR1 = {
            'PH' =&gt; {
                      'symbol' =&gt; 'PHP',
                      'status' =&gt; 'LIVE',
                      'countryname' =&gt; 'Philippines',
                      'currencyname' =&gt; 'Philippino Peso',
                      'code' =&gt; '63'
                    },
            'BR' =&gt; {
                      'symbol' =&gt; 'BRL',
                      'status' =&gt; 'LIVE',
                      'countryname' =&gt; 'Brazil',
                      'currencyname' =&gt; 'Brazilian Real',
                      'code' =&gt; '55'
                    },
            'FR' =&gt; {
                      'symbol' =&gt; 'EUR',
                      'status' =&gt; 'offline',
                      'countryname' =&gt; 'France',
                      'currencyname' =&gt; 'Euro',
                      'code' =&gt; '33'
                    }
          };
  </code></pre>
  
  <p>Is it possible using fetchall_hashref DBI method? Or do I have to go the traditional way, looping through each row and constructing the hash on the fly?</p>