What is this qw I often see in Perl code?

qw stands for quote word. It looks like some other operators such as q and qq, or qr, but actually it works more like split.

If you want to include a list of strings in your code, for example to initialize an array as in this example:

my @name = ('foo', 'bar', 'zorg');

Instead of that you can use the qw operator:

my @name = qw(foo bar zorg);

Which is be the same as:

my @name = qw(foo bar    zorg);

or even

my @name = qw(
    foo
    bar
    zorg
);

qw will take the values provided in the parentheses and split them up where it finds one or more spaces.

So it has the same result as this code:

my @name = split /\s+/,  q(foo bar      zorg);

Of course, there is no point in using split where we can use a qw. (And the split version would give different result if there was a whitespace or newline before the first word.)

Why is it called quote word?

As it split the give string on spaces if any of the strings in the original list of strings has a space in it, we cannot use qw.

For example this creates a 3 element array:

my @name = ('foo', 'bar', 'zorg morg');

On the other hand this is going to be a 4-element array:

my @name = qw(foo bar zorg morg);

So qw does not allow spaces in the string as it looks for space-delimited "words".

On the other hand the "letters" can be any other character.

For example:

my @name = qw(#foo $bar      @zorg);

Is the same as this:

my @name = ('#foo',  '$bar', '@zorg');

So you can use any other characters as part of the "words".

qw is not a function

Although at first it might look like a function having parentheses after it, but in fact it is not and instead of parentheses we can write other delimiters as well.

Slashes seem to be popular:

my @name = qw/foo bar zorg/;

Any of the parentheses can be used:

my @name = qw{foo bar zorg};
my @name = qw[foo bar zorg];
my @name = qw(foo bar zorg);

Quotes can be used:

my @name = qw'foo bar zorg';
my @name = qw"foo bar zorg";

Even some other characters can be used:

my @name = qw!foo bar zorg!;
my @name = qw@foo bar zorg@;

Tough I like to stick to parentheses and I'd recommend you do that too.

Possible attempt to separate words with commas

A common mistake when switching from a list of words to a qw construct is forgetting to remove the separating commas between the elements:

my @name = qw(foo, bar, zorg);

If your code has use warnings turned on, as it should, then you'll get a warning about Possible attempt to separate words with commas.

Importing function selectively

One of the places where you'll often see the use of qw is where modules are loaded. You might see something like this:

use Module::Name qw/foo $bar/;

From the above explanation you'll know that this is the same as

use Module::Name ('foo', '$bar');

This basically means we pass this list as parameters to the import function of Module::Name which will import the 'foo' function and the '$bar' scalar variable into our code.

See also on-demand import, for further explanation.