in reply to sort AoH buggy?

Documented in sort. One of the worst mishaps in Perl.
Warning: syntactical care is required when sorting the list returned from a function. If you want to sort the list returned by the function call find_records(@key), you can use:
my @contact = sort { $a cmp $b } find_records @key; my @contact = sort +find_records(@key); my @contact = sort &find_records(@key); my @contact = sort(find_records(@key));
If instead you want to sort the array @key with the comparison routine find_records() then you can use:
my @contact = sort { find_records() } @key; my @contact = sort find_records(@key); my @contact = sort(find_records @key); my @contact = sort(find_records (@key));
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: sort AoH buggy?
by LanX (Saint) on Dec 09, 2021 at 16:15 UTC
    Thanks I get it.

    Sort sees the function call actually as a comparison routine.

    Tricky ...

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

Re^2: sort AoH buggy?
by LanX (Saint) on Dec 09, 2021 at 16:24 UTC
    honestly the difference by whitespace is horrible, I got bitten again...

    my @contact = sort(find_records(@key)); #vs my @contact = sort(find_records (@key));

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

Re^2: sort AoH buggy? (deprecation candidate)
by LanX (Saint) on Dec 09, 2021 at 16:54 UTC
    Well... I never ever see comparison routines in the form

    • sort my_comp LIST

    used.

    If you really need a special sort it's normally far easier to write

    • sub my_sort { sort {...} @_ }

    They are useful when combining them in multi_sorts with other comparisons, like ...

    • sort { my_comp or $a cmp $b } LIST

    ... , but in this case you are always free to surround single my_comps with curlies.

    • sort { my_comp } LIST

    I'd say this syntax should be deprecated.

    (Unless someone could show me useful applications)

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

      I'd say this syntax should be deprecated.

      I assume you're referring to the sort my_comp LIST syntax. Otherwise, you're just ... wrong.

      But what would be the point of deprecating? What harm does it cause? Deprecating it would only serve to break the few instances of code out there that use it.

      I reckon we are the only monastery ever to have a dungeon staffed with 16,000 zombies.
        It's causing more damage than benefit and fixing old code is easy. Plus a pragma could switch it back on.

        > I assume you're referring to the sort my_comp LIST syntax

        Obviously like discussed.

        See also the perldoc choroba quoted, one case is just distinguished by a whitespace.

        I just trained a new Perl programmer and gave him a set of orthogonal rules how to read and (de)construct the syntax, instead of relying on try-and-error ...

        Which was hard enough, the perldocs already read like spaghetti code.

        Now I have no idea how to justify that useless folly.

        The more logical Perl is constructed, the easier it is to promote.

        I'm still waiting for someone giving me a usecase were this syntax gives any benefit justifying the costs.

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

    A reply falls below the community's threshold of quality. You may see it by logging in.