Re: Brainstorming: quoted identifier operator

[email protected] (Aristotle Pagaltzis via perl5-porters)
Newsgroups perl.perl5.porters
Message-ID <[email protected]>
* Branislav Zahradník <[email protected]> [2026-01-07 11:06]:
> ```
> sub qi (this is nicely readable test name) {
> }
> ```

If you really want this,

    *{'this is nicely readable test name'} = sub { say "hi" };
    &{'this is nicely readable test name'};
    __END__
    Output:
    hi

The fact that it is kinda ugly is IMO a feature. I don’t think heavy use
of this should be encouraged. And I actually find it more readable. IMO
it is much quicker to skim this:

    &{'recursive fibonacci'}($n - 1) + &{'recursive fibonacci'}($n - 2)

… than this:

    qi(recursive fibonacci)($n - 1) + qi(recursive fibonacci)($n - 2)

… where the visual distinction between parameter lists and function
names dissolves and `qi` itself looks like a function name.

Given that `qi` would be a quote-like operator, you can at least solve
the “what is going on” problem by using a different delimiter like

    qi{recursive fibonacci}($n - 1) + qi{recursive fibonacci}($n - 2)

but then then alphanumeric token `qi` still interferes more with my
skimming far more than the punctuation mark `&` in the first example,
which to me blends into the rest of the punctuation and lets the
function name stick out.

And if you don’t like how declaring such a function looks, you just
write a helper:

    sub testcase ( $name, $coderef ) {
        my $pkg = caller;
        *{"$pkg::$name"} = $coderef;
        # can easily throw in a Sub::Util::set_subname call here too,
        # then it will work exactly as the hypothetical `qi` operator would
    }

    testcase 'this is nicely readable test name' => sub { say "hi" };

In terms of readability a helper like `testcase` communicates intent
better than just a “this is some function with a non-identifier name”
operator too.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.