http://www.perlmonks.org?node_id=672339

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

I had a question on a question I read here in Seekers of Perl Wisdom, and decided I'd better post it separately so as to not derail his thread with my noobism.

He asked:
Is it cool to populate a hash of arrays with qq or qw? e.g.
my %animals = ( pets => [ qw(cat dog) ], non => [ qq(rhino giraffe) ], );

Now, my question doesn't have to do with his exact question, as that was answered quite smartly in Hash of Arrays (populating and iterating).
Rather,
I'm curious what the qq( ) is doing in the above example. Populating an array with qw( ) I certainly understand, but from what I've been able to tell from the docs and googling, qq() is just a way to say " " without having to use those characters, in case your string has double quotes but you still want interpolation.

Does that mean his array (stored by reference in the hash with the 'non' key) only contains 1 element, e.g. "rhino giraffe"? Would it have been equivalent to do non => [ "rhino giraffe" ]? My gut says no, but my brain can't justify it :P

Thanks!

Replies are listed 'Best First'.
Re: Question on a question - qq
by jwkrahn (Abbot) on Mar 06, 2008 at 03:27 UTC
    Yes, qq(rhino giraffe) is exactly the same as "rhino giraffe".
      Yes, qq(rhino giraffe) is exactly the same as "rhino giraffe".
      Oh... well, right on... going to have to go tame me a wild rhino giraffe then :)

      Thanks Monks!
Re: Question on a question - qq
by grinder (Bishop) on Mar 06, 2008 at 14:09 UTC

    Hmm, that use of qq smells bad. Of course without knowing the context I cannot say it's wrong... but it really, really looks like the coder wrote qq by accident, instead of qw.

    Be that as it may, a reason why one would use qq and qw instead of "" and qw is on aesthetic grounds. It makes things line up more nicely. This in turn means less visual distractions, which lets the mind concentrate on other matters more important.

    In other contexts, I have been known to quote strings with q() (that is, strings with no variable interpolation). Should interpolation later be required, the opening delimiter is changed from q() to qq(). This is a bit of win, since you don't have to chase down the end of the string to change ' to ", had you been using the ordinary '' delimiters.

    • another intruder with the mooring in the heart of the Perl

      In other contexts, I have been known to quote strings with q() (that is, strings with no variable interpolation). Should interpolation later be required, the opening delimiter is changed from q() to qq(). This is a bit of win, since you don't have to chase down the end of the string to change ' to ", had you been using the ordinary '' delimiters.
      Hey, I never thought of that... I'll have to keep that in mind, that could save some annoyance
Re: Question on a question - qq
by Narveson (Chaplain) on Mar 06, 2008 at 03:36 UTC

    Yes, it does. Yes, it would have been.

    Side with your brain against your gut.

Re: Question on a question - qq
by ikegami (Patriarch) on Mar 06, 2008 at 13:37 UTC
    qq(He said "$quote") is the same as "He said \"$quote\"" q(It's $5) is the same as 'It\'s $5'