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

Re: Perl feature wish: quote-word array-ref operator

Thread Previous | Thread Next
From:
Graham Knop
Date:
March 5, 2021 14:27
Subject:
Re: Perl feature wish: quote-word array-ref operator
Message ID:
CAM=m89G030oi3vHXs25QTQEozFg29hoMS+g+WrtOLKjuqXTHeA@mail.gmail.com
On Fri, Mar 5, 2021 at 9:47 AM Neil Bowers via perl5-porters
<perl5-porters@perl.org> wrote:
>
> 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//.

perl -e'use strict; sub qa {} sub one {} sub two {} sub three {}
qa[one two three]; print "works\n"'
works

It's currently valid syntax. A feature flag would be necessary.

>
> 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 Previous | 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