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

Re: Perl feature wish: quote-word array-ref operator Perl5 Porters<perl5-porters@perl.org>

Thread Previous | Thread Next
From:
=?UTF-8?Q?Salvador_Fandi=c3=b1o?=
Date:
March 10, 2021 09:15
Subject:
Re: Perl feature wish: quote-word array-ref operator Perl5 Porters<perl5-porters@perl.org>
Message ID:
ee1fa5d3-e707-82be-8bd1-6ec80d28dd6c@gmail.com
On 5/3/21 9:48, Neil Bowers via perl5-porters 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.

Am I the only one that doens't have a big love for the qw operator?

I mean, not having to write all these quotes around words is great, but 
I miss the commas. I don't like either that it is an all or nothing 
solution. There is no way to escape spaces or to allow interpolations.

Something I really dislike is using it to initialize hashes, as in...

   my %h = qw(foo bar doz ber noz cloz);

Because it is not obvious what is a key and what is a value.

Other pain point is when you want to create a list including some words 
and also some variables, as for instance:

   my @a = (qw(foo bar), $foo, $bar);

So, while thinking about the qa operator proposed by Neil, it has 
occurred to me that it doesn't solve most of the issues I have with qw. 
I would rather prefer having an operator (for instance, "qb") that just 
creates some kind of bareword context:

   my @a = qb(hello, world, bye, world);
   my %h = qb(hello => world, bye => world);

And also:

   my @a = qb("hello, world", bye, $world);
   # equivalent to ("hello, world", "bye", $world)

And as it is a new operator, we could let it handle [] and {} in special 
ways:

   my $aref = qb[hello, world, bye, world];
   my $href = qb{hello => world, bye => world};

Does this makes sense?


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