wannabeboy has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I'm new to Perl (I've started learning with O'reilly Perl Programming). The "qw" operator is quite complicated for me. I don't understand it. Can you explain a bit? thanks in advance

Replies are listed 'Best First'.
Re: qw operator
by brian_d_foy (Abbot) on Feb 24, 2005 at 10:01 UTC

    The quote words operator is just a typing convenience to let you omit the commas and quoting. Instead of typing:

    @array = ( "dog", "cat", "bird" );

    You type:

    @array = qw( dog cat bird );

    The qw() will break up the thing inside it and give you back a list of the things between the whitespace (and because of that, you can't use qw() with values that have whitespace in them.

    That's it.

    --
    brian d foy <bdfoy@cpan.org>
Re: qw operator
by gopalr (Priest) on Feb 24, 2005 at 10:14 UTC

    qw stands for "quoted words" or quoted by whitespace

    The qw shortcut makes it easy to generate without typing a lot of extra quote marks

    @arr=("fred", "barney", "betty", "wilma", "dino")


    same as above, but less typing

    @arr=qw/ fred barney betty wilma dino /
Re: qw operator
by lamp (Chaplain) on Feb 24, 2005 at 10:12 UTC
Re: qw operator
by Anonymous Monk on Feb 24, 2005 at 11:42 UTC
    qw is not really an operator, as it does all its work at compile time, and doesn't take an expression as argument, but only literal.

    Anyway,

    qw{some string here};
    is more or less equivalent to:
    warn "Possible attempt to put comments in qw() list" if q{some string here} =~ /#/ if $^W; warn "Possible attempt to separate words with commas" if q{some string here} =~ /,/ if $^W; split(' ', q{some string here});
    but then done at compile time.