Perl qw(): Quote-Words List Literal (How It Works, When to Use It, and Pitfalls)
Question
what is qw in perl
Difficulty: Beginner (with intermediate details)
Tags: perl, syntax, qw, lists, quoting-operators, best-practices
What is qw in Perl?
In Perl, qw// (often written as qw(...)) is the quote-words operator. It is a convenient way to write a literal list of words without having to quote each word individually.
For example, these two are equivalent:
my @colors = ('red', 'green', 'blue');
my @colors = qw(red green blue);
qw is primarily used for fixed, compile-time lists such as:
- Lists of constant strings (names, keywords, field names)
- Hash key lists
- Sets implemented as hashes (
map { $_ => 1 }over aqwlist) - Arguments to functions like
grep,join,push, etc.
Basic syntax and delimiters
qw is a quoting-like operator, so it supports many delimiter styles, similar to q// and qq//. Common forms:
qw(red green blue)qw/red green blue/qw{red green blue}qw<red green blue>qw|red green blue|
The contents are split into words using whitespace (spaces, tabs, and newlines). Commas are not separators for qw; commas are treated as normal characters.
What exactly does it produce?
qw// produces a list of strings. Each “word” becomes one element in that list.
This means context matters:
- List context: you get the list elements (great for array assignment).
- Scalar context: you get the number of elements (like
scalar(@array)).
Example 1: Array initialization and scalar context
use strict;
use warnings;
my @colors = qw(red green blue);
print "colors=@colors\n";
my $count = scalar qw(red green blue);
print "count=$count\n";
Expected output:
colors=red green blue
count=3
Important behavior details
1) No interpolation (it behaves like single quotes)
qw// is closer to q// than qq//. That means variables are not interpolated, and escape sequences like \n are not turned into newlines. They stay as literal characters.
If you need interpolation, qw is not the right tool. Use ordinary quoting ("...") or build the list another way.
2) Whitespace splits items
Spaces/newlines/tabs separate words. This is a feature: you can format long lists neatly across multiple lines.
3) Escaping whitespace and delimiters
Sometimes you need a word that contains a space, or you need to include the chosen delimiter character. In qw, you can escape certain characters with backslashes. A common trick is escaping a space to keep it inside one element:
my @x = qw(hello\ world);
# @x contains one element: "hello world"
In practice, if you find yourself needing lots of escaping, that’s often a sign that qw isn’t the best representation for the data (see Best Practices).
Real-world usage patterns
1) Building a “set” (fast membership tests)
Perl doesn’t have a built-in set type, but a hash is commonly used as a set. You can create it from qw and then do O(1)-ish membership tests with exists or a direct lookup.
Example 2: Allowed HTTP methods set
use strict;
use warnings;
my %allowed = map { $_ => 1 } qw(GET POST PUT DELETE);
for my $method (qw(GET PATCH DELETE)) {
print "$method => ", ($allowed{$method} ? "allowed" : "not allowed"), "\n";
}
Expected output:
GET => allowed
PATCH => not allowed
DELETE => allowed
Why this is useful: it’s readable, fast, and keeps your “constant list” in one place.
2) Passing fixed lists to functions
qw shines when you want to call a function with a fixed list of string arguments without visual clutter:
my @fields = qw(id title slug published_at);
my $csv_header = join ",", @fields;
3) Declaring lists of constants (without extra modules)
Even without using use constant or an enum-like module, qw is commonly used to keep “named things” together: roles, permissions, supported formats, environment names, feature flags, etc.
Common pitfalls
Pitfall 1: Thinking commas separate items
This is a classic mistake:
my @x = qw(red, green, blue);
This produces ('red,', 'green,', 'blue') — the commas become part of the words. If you want commas, that’s fine; if you don’t, remove them:
my @x = qw(red green blue);
Pitfall 2: Expecting interpolation
This does not substitute $name:
my $name = "Alice";
my @x = qw(Hello $name);
You’ll literally get ("Hello", "$name"). If you wanted interpolation, you need something like:
my @x = ("Hello", $name);
Pitfall 3: Confusing a list with an array reference
qw(...) is a list, not an array reference. If you need an array reference, wrap it in brackets:
my $aref = [ qw(red green blue) ];
Pitfall 4: Overusing qw for non-words
qw is best when the elements are “word-like” (no spaces, minimal punctuation). If your strings contain lots of punctuation, quotes, JSON, SQL, or paths that require escaping, normal quotes are often clearer and safer.
Best practices
- Use
qwfor simple, static word lists: identifiers, field names, tokens, labels. - Pick a delimiter that minimizes escaping: e.g., use
qw{...}if your list contains/, orqw/.../if it contains parentheses. - Use
[ qw(...) ]when you need an array reference. - Remember scalar context returns a count:
scalar qw(a b c)is3. - Avoid commas in
qwlists unless you intend them to be part of the strings. - Prefer readability over cleverness: if escaping gets heavy, switch to explicit quotes.
Example 3: Delimiters, escaped space, and no interpolation
use strict;
use warnings;
my @paths = qw{/usr/bin /usr/local/bin};
print join("\n", @paths), "\n";
my @with_space = qw(hello\ world);
print "with_space=$with_space[0]\n";
my $name = "Alice";
my @no_interp = qw(Hello $name \n);
print "no_interp=", join("|", @no_interp), "\n";
Expected output:
/usr/bin
/usr/local/bin
with_space=hello world
no_interp=Hello|$name|\n
Summary
qw is Perl’s “quote words” operator: a compact, readable way to write a literal list of whitespace-separated strings. It’s ideal for static word lists, supports many delimiters, does not interpolate variables, and can be used to build arrays, hashes, and set-like lookups. The main things to watch out for are commas becoming part of words, lack of interpolation, and the difference between a list and an array reference.
Verified Code
Executed in a sandbox to capture real output. • 14ms
Example 1
colors=red green blue
count=blue
Example 2
GET => allowed
PATCH => not allowed
DELETE => allowed
Example 3
/usr/bin
/usr/local/bin
with_space=hello\
no_interp=Hello|$name|\n
Useless use of a constant ("red") in void context at - line 8.
Useless use of a constant ("green") in void context at - line 8.