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

The awkwardness of spaces inside `qw` lists

Thread Next
From:
Paul "LeoNerd" Evans
Date:
March 14, 2022 12:09
Subject:
The awkwardness of spaces inside `qw` lists
Message ID:
20220314120842.2f5c1f44@shy.leonerd.org.uk
I've grown quote accustomed to Perl's :attribute syntax, which is
convenient for applying all sorts of useful extra metadata onto
constructions like sub declarations:

  sub f :x :y(val) :z(more values go here) {...}

It's the sort of syntax I'm starting to use more in module import lines
as well, perhaps to pass options to modules, e.g. to enable
experimental features. Since module imports almost always use `qw` to
list a bunch of symbol names to import, it's easy just to add the tag
there:

  use Some::Module qw( :experimental(feature) symbol names here );

It works well with a single value like that. I had imagined a nice plan
for enabling multiple experimental features at once:

  use Some::Module qw( :experimental(red blue green) );

But annoyingly, that doesn't work. `qw` just splits on spaces in a
simple way; this parses like three separate strings:

  use Some::Module ':experimental(red', 'blue', 'green)';

While I'm sure I -could- reconstruct the spaces by counting the parens
myself inside the `sub import` that feels like far too subtle magic for
my liking. Ohwell, lets not put spaces in and instead use some commas:

  use Some::Module qw( :experimental(red,blue,green) symbol names... );

Now, while that "works" it does provoke a warning that happens before I
can silence it:

  Possible attempt to separate words with commas at FILE line LINE.

Upsetting.

I'm sure there are other situations where this becomes a bit of a
limitation.


I begin to wonder: What if we had some different variant of qw that
does *not* split on the spaces that appear inside inner parens within
it? It has to count them anyway to know where the end is, so it can
just not split on a space when that count is > 0. Perhaps we'd call it
`qW`, for "quoted bigger words" (because these words can be bigger,
containing spaces).

  say for qW( :experimental(red blue green) symbols names here );

would then output:

  :experimental(red blue green)
  symbol
  names
  here

Other possible suggestions being: qp (quote parens) or
qb (quote brackets). Some as-yet unanswered questions: How are
bracketing chars other than the current delimiters handled? Are all of
these examples one string, or two?

  qW( brackets[go here] )
  qW[ parens(go here) ]
  qW( what<about cheverons?> )

Such complex questions lead to a complicated model to understand, and
explain,.. and implement and document.


Alternatively, is this all far too complex a situation to be thinking
about, and you'll all tell me just to use regular 'quotes' like a
sensible person?

  use Some::Module ':experimental(red green blue)', qw( symbols here );


Thoughts welcome,

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

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