Perl qw Function

These sites are FOR SALE:,!
If you are interested, please use my Contact page.

The Perl qw function (quote word) uses embedded whitespace separator to split an expression passed to it into a list of elements and returns that list.

It is a handy shortcut that you can use to eliminate the quotes in a list area and make your script more readable. In other words, qw will automatically quote string values for you.

qw is either known as a function or an operator and it means the same thing. Its syntax form is as follows:

qw (EXPR)

In the EXPR it is not allowed to do any interpolation. You can use any set of delimiters, not just the parentheses (for example qw/EXPR/ is another popular way to use it). In the EXPR, the words can be separated with any whitespace, including newlines. You can use the Perl qw operator if each word of the list has no whitespace characters included.

Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!

In the following example we use single quotes, double quotes and the Perl qw operator to create an array. All the three forms are equivalent:

# initialize an array
my @array = ();

@array = ('sweet', 'bitter', 'sour', 'salty', 'hot');  # or

@array = ("sweet", "bitter", "sour", "salty", "hot");  # or

@array = qw/sweet bitter sour salty hot/;

As you can see in this example, if you have a longer list of words and you don’t use the Perl qw function, you need to type yourself a lot of commas and quotes to separate the words and your script can become difficult to read. But don’t mix the commas with the whitespace to separate the words because this will produce warnings if you will run the script with the warnings enabled.

You could also populate a hash using the Perl qw function, by alternating the strings in the expression argument, like in the following example:

# initialize a hash
my %hash = ();

# populate the hash with a few elements
%hash = qw(1 one 2 two 3 three 4 four);

# print the %hash
while (my ($key, $val) = each %hash) {
  print "$key=>$val\n"

This code will output:


You can use the qw function when you have a list of predefined strings, for example the text messages associated with some error codes:

# initialize a hash
my %errorCodes = ();

# populate the hash with a few elements
%errorCodes = qw(

# sort and print the %errorCodes hash

foreach my $key (sort keys %errorCodes) {
  print "$key=>$errorCodes{$key}\n";

Here’s the output:


Please click here to download the Perl qw script with all the above examples included.

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!

Check these how-to tutorial eBooks (PDF format):

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        qw (more)

return from Perl qw function to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!