in reply to Prototype like sort()?

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";