in reply to Clarifying the Comma Operator

Excellent suggestion. I wouldn't use "identifier", as that suggests other connotations to me and doesn't fit how I'm used to seeing Perl documented.

I would change "any word" to "a word". I find "any word" potentially misleading, as it could be interpretted as applying to words that are part of some larger expression to the left of the fat comma.

My first choice was to change "any word" to "a bareword", but the definition given for "bareword" in perldata is actually too narrow for this case. I define "bareword" as a "bare word", an unadorned word that Perl first tries to interpret as an operator or function call and then resorts to quoting if strict.pm doesn't prevent it. perldata defines "bareword" as "a bare word that doesn't mean something else", which I find unfortunate but acceptable.

Note that elsewhere in perldata it says:

The => operator is mostly just a more visually distinctive synonym for a comma, but it also arranges for its left-hand operand to be interpreted as a string -- if it's a bareword that would be a legal simple identifier (=> doesn't quote compound identifiers, that contain double colons).

which isn't using the "bareword" definition found in that same document (otherwise it would mean that x, for example, won't be turned into a string by => since x isn't a "bareword" according to the perldata documentation).

Update: Actually, even though x is an operator, it can also be a "bareword" that just gets quoted if strict.pm isn't in effect. Perhaps "if", "s", or "q" would've been better examples. I'm not absolutely certain that an unadorned ("bare") instance of "if", "s", or "q" can never turn into a bareword that gets stringified, but I can't think of any counterexamples. For example, try replacing "x" with other things in perl -MO=Deparse -e "0+x" vs in perl -MO=Deparse -e "print x=>0".

perldata also says:

In fact, an identifier within such curlies is forced to be a string, as is any simple identifier within a hash subscript. Neither need quoting.

just to note how the same situation is described for another case.

Finally, my example of x reminds me that the clarification should be expanded to "considered an operator, constant, or function call".

- tye        

Replies are listed 'Best First'.
Re^2: Clarifying the Comma Operator (bareword)
by CountZero (Bishop) on Jun 07, 2009 at 07:10 UTC
    it also arranges for its left-hand operand to be interpreted as a string
    Actually, that's where the problem lies: 08 is not seen as a string but (wrongly) as an octal number. Perl tries to numify this "word" whereas it should be stringified.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Hmm. You'll have to try much harder to explain your point. I'm fully aware of the facts you repeat above, but I'm completely lost as to why you felt it an appropriate response to my node. I also disagree with your use of "wrongly" and "should". You also appear to disagree with your own uses of "wrongly" and "should" in other replies in these threads.

      The rest of that (complex) sentence that you partially quoted includes:

      if it's a bareword that would be a legal simple identifier

      And 08 isn't a legal simple indentifier (and thus there is no "problem"). Nor is 08 a bareword (by either definition). And I know you know this because elsewhere you noted this (though your inclusion of "hyphen" in your explanation was erroneous).

      - tye        

        It is indeed confusing. Unless you read (and remember all) what is in separate parts of the docs, the statement I quoted looks as if the => operator will stringify its left hand argument, which it does ... sometimes and under strict conditions only. I find it surprising and having been bitten by it before, I always quote the left hand part of =>. It would be better --IMO-- if the docs would make a less broad statement in that respect.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Actually, that's where the problem lies: 08 is not seen as a string but (wrongly) as an octal number. Perl tries to numify this "word" whereas it should be stringified.
      But how should perl know? perl is tokenizing when it encounters the leading 0. Considering that it know it's now expecting a TERM, the leading 0 must mean it's going to encounter an octal number. It doesn't know about the following arrow yet, but it has to decide how to tokenize the next thing.

      And that's why all "bare words" look like identifiers. When encountering

      foo::bar => baz
      perl is expecting a TERM. Barewords can start terms (infix operators like the x tye mentions cannot - that's why infix operators can consist of letters, but prefix operators cannot), and that's why all barewords look like identifiers. Because only after tokenizing the bareword and looking at the next token, perl decides whether the bareword is an identifier (subroutine, filehandle), or a string.
        Perl doesn't know for the very good reasons you quote. But the (partial) sentence I quoted from the docs, gives the impression that the => operator somehow makes a string out of its left-hand argument, which it cannot do anymore as the "octal" error already occurred.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James