in reply to quoting style for lists

First of all, in Perl there are no "string constants" (whatever that is). Perl doesn't require you to quote your strings, but it's considered good form to do. In fact, if you don't quote, -w will issue a warning (usually, it's Perl so there are exceptions), and with "use strict", you will get a compile time error. (Note that you can only use unquoted strings if the strings look like valid identifiers, possibly preceeded by a minus sign).

However, there are two exceptions. If a string looks like a valid identifier it doesn't have to be quoted if it's either used as a hash key, or on the left side of a fat arrow (=>). This is done to help the programmer; there is little room for mistakes here, when used as a hash key, you usually mean to use the string, not to call a possible function with that name (called without arguments). If you do want to call such a function, you can always preceed it with a +, or a put () behind it.

the qw() function turns whitespace-delimited unquoted string literals into a list of quoted string literals, if I'm not mistaken.
You are very mistaken. You are confusing syntax with semantics. qw returns a list of strings. Strings are values. The different forms of string literals (bareword, single quoted, double quoted, here document) are syntax constructs, but once compiled, there's no difference.

Abigail

Replies are listed 'Best First'.
Re: Re: quoting style for lists
by erikprice (Initiate) on Aug 01, 2002 at 17:26 UTC
    If a string looks like a valid identifier it doesn't have to be quoted if it's either used as a hash key, or on the left side of a fat arrow (=>).

    Isn't a string on the left side of a fat arrow a hash key? Or is there a subtle difference that I'm missing...

    You are very mistaken. You are confusing syntax with semantics. qw returns a list of strings.

    Forgive me, but this is still confusing to me. My understanding is that qw accepts whitespace-separated "words" and returns a list of quoted "words". I'm not sure how this differs from what you just wrote.

    Thanks for everyone's input on this,

    Erik

      $, = " "; print I => do => not => think => so => "\n"; __END__ I do not think so
      As for qw, it is a quoting construct, like q, qq, etc. It's "argument" (which doesn't include the delimiters - the delimiters are eaten by the parser) is treated as a string and split on whitespace. What results is a list of strings.

      Note that as soon as the parser consumed it, the delimiters are gone, and they never return. Delimiters are there just there to tell the parser, "start here" and "stop here". It doesn't make sense for qw to return "quoted words", just like it doesn't make sense to let qw return yellow.

      Abigail

        It doesn't make sense for qw to return "quoted words", just like it doesn't make sense to let qw return yellow.

        If we define "quoted words" as "strings with no whitespace" then it makes perfect sense (and if we define "yellow" as ... well, nevermind...). Anyway, I'd more accurately call it 'quoted words' since qw has more or less the effect of single-quoting those words, and I've occasionally wished for perhaps a 'qqw' which did some interpolation of the arguments.

      Isn't a string on the left side of a fat arrow a hash key? Or is there a subtle difference that I'm missing...

      There is a subtle difference. The fat arrow is idiomatically used when declaring hashes, but it's not necessary, nor is that its only purpose.

      my %hash = ('foo', 1, 'bar', 2); my @args = ( foo => bar => baz ); use Data::Dumper; print Data::Dumper->Dump([ \%hash, \@args ], [ hash => 'args' ]);

        I love the fat arrow for things like this:

        $element->paste( before => $some_other_element); $element->set_att( id => "JB007");

        But I really _HATE_ it when it silently turns constants into strings:

        use constant (CONSTANT => 'foo'); my %hash=( PCDATA => 'the key is the string "PCDATA", not "foo"');

        --
        The Error Message is GOD - MJD

      On =>

      Technically, => is a "funky comma". Yes, it is true that most of the time it is used for hash declarations, but you could use it for other purposes as well. And on the other hand, you can also declare a hash using

      %a = ('key', 'value');

      On qw

      Let's use perlspeak. qw returns a list of scalars (that is then stored in an array). Also, the quoting is only used for perl to recognize the string. When perl stores the string in memory, the quotes are of course not used anymore.

      @a = ('a', 'b', 'c'); @b = qw(a b c); # a and b contain the same elements %hash{key}++; %hash{'key2'}++; print "$_\n" foreach (keys %hash); # prints (not necessarily in this order): # key # key2

      Does this clarify your problem?

      Update: I didn't close my <b> tags, and I didn't use perlspeak myself -- perl uses lists rather than arrays. Thanks, Chmrr!

      Update 2:
      Chmrr says: Hrm -- it's not that perl uses lists rather than arrays; they're two different things, and perl has both. An array is @foo; a list is (1,2,3). Arrays are mutable, lists are not.
      Never argue with a saint ;-). Yes, he's right. And I learned something new. Thanks!

        Thanks be to all of you monks. I will resume my studies.

        Erik