perlancar, it appears many fellow monks are having trouble understanding your requirements. johngg++ and marshall++ had reasonable interpretations, but not the same I had. I will describe my interpretation below, but (unfortunately) I don't have an exact solution if my interpretation is right, though I come close to what I believe your requirements are. Please let us know if this is what you want. If not, you will have to give more detail.

The builtin sort has three alternate syntaxes:

  1. sort SUBNAME LIST
  2. sort CODEREF LIST
  3. sort LIST

From your quoted code example, I infer that you want to be able to mimic syntaxes #2 and #3 (since your example doesn't show #1, I will assume you don't want it), but have some different functionality. Thus, I think you are asking if there is a subroutine prototype that will make the first CODEREF argument optional, but still without having to have the prefix sub to initiate the anonymous subroutine, and without having to have the comma between the closing } of the CODEREF and the start of the LIST.

By using something like the following, I can come close to the syntaxes listed, but not exactly replicate them:

use warnings; use strict; sub myfunc(@) { my $default_subfunc = sub { local $"=','; return "my default code block: (@_)\n"; }; my $subfunc = ( 'CODE' eq ref $_[0] ) ? shift : $default_subfunc; return $subfunc->(@_); } #3: no CODEREF print myfunc 1, 2, 3; #2a: two-line version, with comma: my $alternate = sub { "alt: sizeof LIST = " . scalar(@_) . "\n" }; print myfunc $alternate, 1..10; #2b: one-line version, with comma: print myfunc sub { local $"=';'; return "oneliner: @_\n" }, qw/hello w +orld/; #2fail: one-line version, no comma, no sub: eval qq| # this eval-wrapper prevents the compiler from crashing, so +the previous examples will still run print myfunc { local $"=';'; return "oneliner: @_\n" } qw/hello world/ +; |; if($@) { my $x = $@; $x =~ s/^/\t/gm; print "ERROR:{\n$x\n}\n"; } # I assume you already knew the & prototype, to mimic _just_ #2's synt +ax sub otherfunc(&@) { my $subfunc = shift; return $subfunc->(@_); } print otherfunc { "otherfunc always needs a coderef: @_\n" } 5..7; print otherfunc sub { $alternate->(@_) }, 2,3,5,7,11; print "END\n";

In reply to Re: Prototype like sort()? by pryrt
in thread Prototype like sort()? by perlancar

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.