develooper Front page | perl.perl5.porters | Postings from March 2021

Perl feature wish: quote-word array-ref operator

Thread Next
From:
Neil Bowers via perl5-porters
Date:
March 5, 2021 08:47
Subject:
Perl feature wish: quote-word array-ref operator
Message ID:
0e66195d-3166-46d5-840d-0fdf69d9134d@Spark
It would be handy to have a "quote word arrayref operator" in Perl, that combines [] with qw.

So instead of writing:

    $a = [ qw/ one two three/ ];

You could write:

    $a = qa[ one two three ];

As with qw, you could either use bracketing delimiters, or use the same character:

    $a = qa/ one two three /;

But I'd expect most people would use qa[ … ] as it visually reinforces what's going on.

This reduces line-noise, making code more readable, and the intention clearer.

The quote-word arrayref construct is widely used. For example, as of 16th Feb 2021, 12681 distributions on CPAN (36%) use it either in modules, scripts, tests, or build scripts.

The proposed syntax doesn't currently compile, so maybe this could just be added to the language, without an experimental or feature guard? Because there's already qwa// on CPAN (see below), I'm suggesting qa//.

I'm not proposing an operator for hashrefs, as I've never wanted one, and there's no real evidence of a need for one on CPAN.

Prior Art on CPAN

There are a number of modules that provide an operator like this, but (a)  I don't want to add a dependency for this syntax, and (b) they introduce additional dependencies.

Syntax::Feature::Qwa defines qwa// (and qwh// and qwk//  for hashes).

Quote::Ref also add qwa// (and qwh// for hashes)

Quote::Reference defines qwr// for arrayrefs and qhr// for hashrefs.

There's also PerlX::QuoteOperator, which lets you define your own quote operator.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About