'How can I shorten a [ qw( ... ) ] construct in Perl?
Perl's quote-like operator qw() creates a word list from barewords, while square brackets [] can be used to create references to anonymous arrays. Now, I was wondering if Perl provides a way to somehow abbreviate this:
my $aref = [qw( a b c )];
using something like a non-existent qa() operator:
my $aref = qa( a b c );
I've been using qw() and [] together quite a lot recently, and all I want is to reduce cluttering.
Note: This is not what I'm looking for:
my @a = \( qw( a b c ) );
Solution 1:[1]
Not core Perl, but check out Syntax::Feature::Qwa
Solution 2:[2]
No, there is no shorter way with core Perl to express:
my $aref = [qw( a b c )];
Solution 3:[3]
One suggestion someone made that makes this slightly more readable is to say [qw[ a b c ]];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Diab Jerius |
| Solution 2 | Andy Lester |
| Solution 3 | khw |
