qw vs. q

Discussion in 'Perl Misc' started by John Smith, Feb 27, 2006.

  1. John Smith

    John Smith Guest

    I found the following example on CPAN:

    my $key = 'welcome';
    my %data = (
    'this' => qw(that),
    'tom' => qw(and jerry),
    'welcome' => q(Hello World),
    'zip' => q(welcome),
    );
    my @data = keys %data;

    my question is:
    1) what is the difference between qw and q?
    2) why is qw used in first 2 key-element; and q used in last 2
    key-element?

    after some googling, I found "quoted as a list (qw) instead of a string
    (q or qq), ", which make things more confusing.
     
    John Smith, Feb 27, 2006
    #1
    1. Advertising

  2. John Smith

    DJ Stunks Guest

    John Smith wrote:
    > I found the following example on CPAN:
    >
    > my $key = 'welcome';
    > my %data = (
    > 'this' => qw(that),
    > 'tom' => qw(and jerry),
    > 'welcome' => q(Hello World),
    > 'zip' => q(welcome),
    > );
    > my @data = keys %data;


    I think you have omitted some syntax from this example. This does not
    do what you think it does.

    > my question is:
    > 1) what is the difference between qw and q?


    perldoc perlop -- "Quote and Quote-like operators"

    > 2) why is qw used in first 2 key-element; and q used in last 2
    > key-element?


    assuming you have reproduced it accurately - which I doubt - there's no
    way of knowing what the author intended to show via this example.

    > after some googling, I found "quoted as a list (qw) instead of a string
    > (q or qq), ", which make things more confusing.


    you need to buy a book. and start posting at perl.beginners

    -jp
     
    DJ Stunks, Feb 27, 2006
    #2
    1. Advertising

  3. John Smith

    Guest

    "John Smith" <> wrote:
    > I found the following example on CPAN:


    Maybe you should tell us where you found it.

    >
    > my $key = 'welcome';
    > my %data = (
    > 'this' => qw(that),
    > 'tom' => qw(and jerry),
    > 'welcome' => q(Hello World),
    > 'zip' => q(welcome),
    > );
    > my @data = keys %data;
    >
    > my question is:
    > 1) what is the difference between qw and q?


    qw{} does this (from perldoc perlop):

    qw/STRING/
    Evaluates to a list of the words extracted out of
    STRING, using embedded whitespace as the word
    delimiters. It can be understood as being roughly
    equivalent to:

    split(' ', q/STRING/);

    While q{} does something else.


    > 2) why is qw used in first 2 key-element; and q used in last 2
    > key-element?


    It is bad style, and probably a bug. They should all be q or qq, not qw.
    (If I run the %data assignment under warnings, I get a warning about an
    odd number of elements.)

    In fact, where I found this example it was exactly that, an example of a
    bug.

    So, the reason that qw is used in the second key-element is to
    intentionally introduce a bug, so they can then tell you how to hunt down
    said bug.



    >
    > after some googling, I found "quoted as a list (qw) instead of a string
    > (q or qq), ", which make things more confusing.


    Well, then look someplace else. Google is nice, but it isn't the end all
    and be all.

    "perldoc -f qw" give me:

    qw/STRING/
    Generalized quotes. See "Regexp Quote-Like Opera-
    tors" in perlop.

    So I saw "Regexp Quote-Like Operators" in perlop, and give me the first
    blurb I posted above, plus a lot more things you might want to read as
    well.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 27, 2006
    #3
  4. John Smith

    Paul Lalli Guest

    John Smith wrote:
    > I found the following example on CPAN:
    >
    > my $key = 'welcome';
    > my %data = (
    > 'this' => qw(that),
    > 'tom' => qw(and jerry),
    > 'welcome' => q(Hello World),
    > 'zip' => q(welcome),
    > );
    > my @data = keys %data;
    >
    > my question is:
    > 1) what is the difference between qw and q?


    The statements:
    my $string = q{foo bar baz};
    my @list = qw{foo bar baz};

    are exactly equivalent to:
    my $string = 'foo bar baz';
    my @list = ('foo', 'bar', 'baz');

    q{} returns one single-quoted string.
    qw{} returns a list of single-quoted strings, where each string is from
    the space-separated barewords of the original argument.

    > 2) why is qw used in first 2 key-element; and q used in last 2
    > key-element?


    If you really did copy and paste that from CPAN, I would be *shocked*
    if it worked. It shouldn't.

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Data::Dumper;

    my %data = (
    'this' => qw(that),
    'tom' => qw(and jerry),
    'welcome' => q(Hello World),
    'zip' => q(welcome),
    );

    print Dumper(\%data);
    __END__

    Odd number of elements in hash assignment at ./clpm.pl line 6.
    $VAR1 = {
    'Hello World' => 'zip',
    'tom' => 'and',
    'welcome' => undef,
    'jerry' => 'welcome',
    'this' => 'that'
    };


    the part of that hash reading:
    'tom' => qw(and jerry)
    is identical to
    'tom', 'and', 'jerry'

    meaning that 'and' is the value for the key 'tom', and 'jerry' is the
    next key, which has the value 'welcome'. That explains the output
    above.

    > after some googling, I found "quoted as a list (qw) instead of a string
    > (q or qq), ", which make things more confusing.


    I hope this helps clear it up...

    Paul Lalli
     
    Paul Lalli, Feb 27, 2006
    #4
  5. John Smith

    tuser Guest

    John Smith wrote:
    > I found the following example on CPAN:
    >
    > my $key = 'welcome';
    > my %data = (
    > 'this' => qw(that),
    > 'tom' => qw(and jerry),
    > 'welcome' => q(Hello World),
    > 'zip' => q(welcome),
    > );
    > my @data = keys %data;
    >
    > my question is:
    > 1) what is the difference between qw and q?


    Here is a quote about "qw" from perldoc perlop:
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ++ qw/STRING/
    ++ Evaluates to a list of the words extracted out of
    ++ STRING, using embedded whitespace as the
    ++ word delimiters. It can be understood as being
    ++ roughly equivalent to:
    ++
    ++ split(' ', q/STRING/);
    ++
    ++ the differences being that it generates a real list
    ++ at compile time, and in scalar context it returns
    ++ the last element in the list. So this expression:
    ++
    ++ qw(foo bar baz)
    ++
    ++ is semantically equivalent to the list:
    ++
    ++ 'foo', 'bar', 'baz'
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Here is a quote about "q" from perldoc perlop:
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ++ q/STRING/
    ++ 'STRING'
    ++ A single-quoted, literal string. A backslash
    ++ represents a backslash unless followed by the
    ++ delimiter or another backslash, in which case the
    ++ delimiter or backslash is interpolated.
    ++
    ++ $foo = q!I said, "You said, 'She said it.'"!;
    ++ $bar = q('This is it.');
    ++ $baz = '\n'; # a two-character string
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++

    > 2) why is qw used in first 2 key-element; and q used in last 2
    > key-element?


    To understand what's going on, one needs to look also at the Comma
    Operator "=>":

    Here is a quote about "=>" from perldoc perlop:
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ++ Comma Operator
    ++ Binary "," is the comma operator. In scalar
    ++ context it evaluates its left argument, throws
    ++ that value away, then evaluates its right
    ++ argument and returns that value. This is just
    ++ like C's comma operator.
    ++
    ++ In list context, it's just the list argument
    ++ separator, and inserts both its arguments into
    ++ the list.
    ++
    ++ The "=>" operator is a synonym for the comma,
    ++ but forces any word (consisting entirely of word
    ++ characters) to its left to be interpreted as a string
    ++ (as of 5.001). If the argument on the left is not
    ++ a word, it is first interpreted as an expression,
    ++ and then the string value of that is used.
    ++
    ++ The "=>" operator is helpful in documenting the
    ++ correspondence between keys and values in
    ++ hashes, and other paired elements in lists.
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++

    So, the example basically says:
    my %data = ('this', 'that', 'tom', 'and', 'jerry',
    'welcome', 'Hello World', 'zip', 'welcome');

    That could equally be written as:
    my %data = (
    'this' => 'that',
    'tom' => 'and',
    'jerry' => 'welcome',
    'Hello World' => 'zip',
    'welcome'
    );

    Note that the number of elements in the list is not even, so you will
    get a warning "Odd number of elements in hash assignment" when you run
    this example.
     
    tuser, Feb 27, 2006
    #5
  6. John Smith

    Rick Scott Guest

    (John Smith <> uttered:)
    > I found the following example on CPAN:
    >
    > my $key = 'welcome';
    > my %data = (
    > 'this' => qw(that),
    > 'tom' => qw(and jerry),
    > 'welcome' => q(Hello World),
    > 'zip' => q(welcome),
    > );
    > my @data = keys %data;


    First of all, bear in mind that that's a contrived example from
    perldebtut, the debugging tutorial, and it has errors. Run it with
    strictures and warnings (use strict; use warnings;) and you get:

    Odd number of elements in hash assignment at ./foo.pl line 9.

    ....because what that code actually means is

    my %data = (
    'this', 'that',
    'tom', 'and', 'jerry',
    'welcome', 'Hello World',
    'zip', 'welcome',
    );


    > my question is:
    > 1) what is the difference between qw and q?


    qw is the word list quoting operator. Instead of giving you back
    one string like '' and "" do, it returns a list of the whitespace-
    separated `words' inside.

    q{foo} is just a more general way of saying 'foo' -- it works exactly
    like singlequotes. Similarly, qq{bar} is just a more general way of
    saying "bar" -- qq works just like doublequotes. See the "Quoting and
    Quote-like operators" section of perlop:

    perldoc perlop

    To wit:

    qw(that) # gives you one item: 'that'
    qw(and jerry) # gives you TWO items: 'and', 'jerry'
    q(Hello World) # gives you ONE item: 'Hello world'
    q(welcome) # gives you one item: 'welcome'



    > 2) why is qw used in first 2 key-element; and q used in last 2
    > key-element?


    Using qw to assign specifically to hash *values* like your example did
    doesn't make sense. Hash values have to be single values, and qw is
    generally used when you're looking for a list. Using qw to assign to
    a whole hash, on the other hand, is a common idiom:

    my %pt_number = qw{
    1 um 2 dois 3 tres 4 quatro 5 cinco
    6 seis 7 sete 8 oito 9 nove 10 dez
    }




    Cheers,
    Rick
    --
    key CF8F8A75 / print C5C1 F87D 5056 D2C0 D5CE D58F 970F 04D1 CF8F 8A75
    It is very little to me to have the right to vote,
    to own property, etcetera, if I may not keep my body,
    and its uses, in my absolute right. :Lucy Stone
     
    Rick Scott, Feb 27, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page