--- name: qw// signature: 'qw(LIST)' since: 5.0 status: documented categories: ["Lists"] --- ```{index} single: qw//; Perl built-in ``` *[Lists](../perlfunc-by-category)* # qw// Build a list of barewords by splitting a delimited string on whitespace, without quoting or comma-separating each word. `qw//` is a compile-time quote-like operator, not a runtime function. The parser sees `qw(foo bar baz)` and emits the constant list `("foo", "bar", "baz")` — no splitting happens at run time, and the list is assembled once regardless of how often the code is executed. Reach for it wherever you would otherwise type a list of short, simple string literals separated by commas: import lists, `@ISA`, `@EXPORT`, hash-key sets, test-name tables. ## Synopsis ```perl qw(foo bar baz) qw[foo bar baz] qw{foo bar baz} qw qw!foo bar baz! qw/foo bar baz/ ``` Any non-alphanumeric character works as a delimiter. The four bracket pairs (`()`, `[]`, `{}`, `<>`) nest; all other delimiters do not. ## What you get back A list of strings in list context — one element per run of non-whitespace characters in the body, with leading and trailing whitespace discarded and runs of internal whitespace treated as a single separator. In scalar context `qw//` evaluates to the **last** element of the list, which is almost never what the author intended. `use warnings` flags the common scalar-context mistakes (see *Edge cases*). ## Examples Import list for a module — the canonical use: ```perl use POSIX qw(setlocale localeconv strftime); ``` `@EXPORT` / `@EXPORT_OK` / `@ISA` tables: ```perl our @ISA = qw(Exporter); our @EXPORT = qw(sum min max); ``` Build a small hash where every key maps to `1` (a set): ```perl my %is_keyword = map { $_ => 1 } qw(if unless while until for); if ($is_keyword{$word}) { ... } ``` Compare the three ways of writing the same list — commas-and-quotes, fat-comma auto-quoting, and `qw//`: ```perl my @a = ("foo", "bar", "baz"); # explicit strings my @b = (foo => "1", bar => "2", baz => "3"); # fat comma only my @c = qw(foo bar baz); # qw// ``` Array initialisation with many short tokens: ```perl my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); ``` Positional argument list to a function: ```perl run_tests(qw(smoke integration regression)); ``` ## Edge cases - **No commas inside the body.** A comma is taken as part of a word: `qw(foo, bar)` yields the two-element list `("foo,", "bar")`, not three elements. `use warnings` emits `Possible attempt to separate words with commas`. - **No comment syntax.** `#` is a literal character inside the body. `qw(foo # comment\n bar)` contains the word `#` and the word `comment`. `use warnings` emits `Possible attempt to put comments in qw() list`. - **No backslash escapes.** `\` is literal — you cannot escape a whitespace character to include it in a word, and you cannot `\`-escape the delimiter. `use warnings` emits a warning for any `\` in the body. - **No interpolation.** `qw($x @y)` produces the two literal strings `"$x"` and `"@y"`. Use `("$x", @y)` (or `q()`/`qq()`) when you want a variable's value. - **Whitespace is the only separator.** Tabs and newlines split words exactly like spaces, which is why multi-line `qw(...)` lists are idiomatic for long tables. - **Words with embedded whitespace are impossible.** If you need `"hello world"` as a single element, `qw//` is the wrong tool — use `("hello world", ...)` with real quotes. - **Empty body.** `qw()` is the empty list, equivalent to `()`. - **Scalar context** yields the last element, not the count. Wrap in `scalar(() = qw(...))` if you genuinely want the count, or assign to an array first: ```perl my @w = qw(foo bar baz); print scalar @w; # 3 ``` - **Parser ambiguity with `//`.** `qw//` uses `/` as a delimiter just like `m//` and `s///`. Inside the body, no regex metacharacters apply — it is a flat string split on whitespace. The visual similarity to a regex is a reading hazard, not a semantic one. ## Differences from upstream Fully compatible with upstream Perl 5.42. ## See also - [`q//`](q) — single-quote literal string; no interpolation, unlike `qq//` - [`qq//`](qq) — double-quote literal string with full interpolation - [`qr//`](qr) — compile a regex literal; the `//` family member for pattern matching - [`split`](split) — the runtime equivalent when your word list is not known until run time: `split(" ", $str)` behaves almost exactly like a dynamic `qw//` - [`join`](join) — the inverse operation, gluing a list back into a single delimited string