Hash::Ordered versus Tie::IxHash with JSON::XS encode
<p>I'm trying <a href="https://metacpan.org/pod/Hash::Ordered" rel="nofollow"><code>Hash::Ordered</code></a> instead of <a href="https://metacpan.org/pod/Tie::IxHash" rel="nofollow"><code>Tie::IxHash</code></a>, because it seems to be faster.</p>

<p>While <code>Tie::IxHash</code> is working fine, I struggle with some problems with <code>Hash::Ordered</code>. The point is to have the hashes ordered (which are usually random in Perl).</p>

<pre><code>use Hash::Ordered;
use JSON::XS;
use Data::Dumper;

use strict;
use warnings;

my $json = JSON::XS-&gt;new;

my $oh = Hash::Ordered-&gt;new;
$oh-&gt;push('result' =&gt; { 'counter' =&gt; "123" }, 'number' =&gt; { 'num' =&gt; '55' });

my @r = $oh-&gt;as_list;

$json-&gt;pretty(1);
my $jsondata = $json-&gt;encode(\@r);
print Dumper $jsondata;
</code></pre>

<p>The result is odd:</p>

<pre><code> [
   "result",
   {
      "counter" : "123"
   },
   "number",
   {
      "num" : "55"
   }
]
</code></pre>

<p>Here is the working example with <code>Tie::IxHash</code>, I try to get the same results with <code>Hash::Ordered</code>.</p>

<pre><code>use Data::Dumper;
use Tie::IxHash;
use JSON::XS;

use strict;
use warnings;

my $json = JSON::XS-&gt;new;

my %h;
tie(%h, 'Tie::IxHash', result =&gt; { counter =&gt; "123" }, number =&gt; { num =&gt; '55' });

  $json-&gt;pretty(1);
  my $pretty_json = $json-&gt;encode(\%h);

  print Dumper $pretty_json;
</code></pre>

<p>Output </p>

<pre><code>{
   "result" : {
      "counter" : "123"
   },
   "number" : {
      "num" : "55"
   }
}
</code></pre>