This is the weirdest bug and I'm totally confused.

I created a buggy function but my tests miraculously passed none the less.

I narrowed it down to this demo where sort is magically expanding stringified hashrefs like "HASH(0x253f0c8)" ...

(see <--- WTF? in output)

Could someone please explain the difference between sort AoH and sort {$a cmp $b } AoH here

use v5.12; use warnings; use Data::Dump; my $AoH; debug_loop_join();exit; sub loop_join { my %H; $H{$_} = () for @$AoH; return keys %H; } sub debug_loop_join { $AoH = create_data (.2, 10); ddx [ loop_join ($AoH) ]; ddx [ sort loop_join ($AoH) ]; ddx [ sort {$a cmp $b } loop_join ($AoH) ]; } sub create_data { my ($density, $records) = @_; my @AoH; push @AoH, {map { rand 100 <= $density ? ( $_ => $_ ) : () } "A" . +. "ZZ"} for 1 .. $records; return \@AoH; }
OUTPUT:
# sort_hash_bug.pl:18: [ # "HASH(0x253f0c8)", # "HASH(0x25350c8)", # "HASH(0x268aa20)", # "HASH(0xeea828)", # "HASH(0x2535548)", # "HASH(0x2535128)", # "HASH(0x268aab0)", # "HASH(0x2535260)", # "HASH(0x268a840)", # "HASH(0x268a918)", # ] # sort_hash_bug.pl:19: [ # <--- +WTF? # [ # {}, # { AZ => "AZ", EM => "EM", RY => "RY" }, # {}, # { GT => "GT", JR => "JR", RZ => "RZ" }, # { HY => "HY" }, # { HT => "HT" }, # { VH => "VH", WU => "WU" }, # { GR => "GR", HH => "HH", HM => "HM", JC => "JC", WJ => "WJ" }, # {}, # { BK => "BK", UH => "UH", XX => "XX", ZN => "ZN" }, # ], # ] # sort_hash_bug.pl:20: [ # "HASH(0x25350c8)", # "HASH(0x2535128)", # "HASH(0x2535260)", # "HASH(0x2535548)", # "HASH(0x253f0c8)", # "HASH(0x268a840)", # "HASH(0x268a918)", # "HASH(0x268aa20)", # "HASH(0x268aab0)", # "HASH(0xeea828)", # ]

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to sort AoH buggy? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.