If your concern is $a and $b, then see other replies.

If your concern is the first line of your usage documentation:

reduce \&f @x

then you are out of luck. Part of the point of that syntax is that the function name is optional. And that is one of the "prototypes" that is available to built-ins but isn't available to user functions. Note that prototype("CORE::sort") returns undef to indicate that its calling conventions can't be expressed in a sub prototype.

Check opcode.pl in the Perl source and you'll see that sort's argument processing is defined by "dm@ C? L". User defined functions don't have access to all of those flags. The "?" means that you can just leave off the code part, which would make no sense for a "reduce" implementation.

If you use the typical "sub reduce(&@)" prototype, then you can always pass a code ref to your function via &reduce( \&f, ... ). You can also usually drop the & or even the parens, but I suspect that might vary between versions of Perl. See Algorithm::Loops for some comments on quirks in what types of arguments sort can deal with on different versions of Perl, since the quirks appear to be similar. But it looks like you can get away with that syntax except for leaving off the comma on all recent Perls. The lack of the comma has to do with sort making the subroutine name optional so you shouldn't emulate that part anyway.

- tye        


In reply to Re: How to get sort-like semantics? (\&f) by tye
in thread How to get sort-like semantics? by b4swine

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.