in reply to Clarifying the Comma Operator

In response to your suggestion:
The "=>" operator is a synonym for the comma, but forces any word (beginning with an underscore or alphabetic character and consisting entirely of word characters) to its left to be interpreted as a string (as of 5.001). This includes words that might otherwise be considered a constant or function call but does not include numeric literals.

I think saying it like this would make it easier to understand:

The "=>" operator is a synonym for the comma, but forces any word to its left to be interpreted as a string (as of 5.001); this includes words that might otherwise be considered a constant or function call. Here, a "word" is anything beginning with an underscore, hyphen or alphabetic letter and consisting entirely of word characters (see description of "\w" in perlre). Any "word" that can be interpreted as a number will be converted to a string after evaluating its numeric value (e.g. "1.20" and "-034" to the left of "=>" will yield the strings "1.2" and "-34" "-28", respectively -- the latter involves an octal-to-decimal conversion).

UPDATE: Thanks to Porculus for pointing out the error with -034. As for what CountZero said, I think a negative number is consistent with the definition, so the example is relevant and useful. While "1.20" in fact fails to match the definition, I thought it was better to include it as an example anyway, to demonstrate the effect -- as well as the fact that stepping outside the definition in this way does not cause a compile-time error.

(And I just noticed what happens when you do:  %h = ( 7-6 => "one", 1+1 => "two" ), which some might consider to be a useful feature, but would need to be used with care...)

Replies are listed 'Best First'.
Re^2: Clarifying the Comma Operator
by CountZero (Bishop) on Jun 07, 2009 at 07:06 UTC
    Your number examples do not fall within the definition of "word" as they do not begin with an underscore, hyphen or alphabetic letter.

    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

Re^2: Clarifying the Comma Operator
by Porculus (Hermit) on Jun 07, 2009 at 11:14 UTC
    (e.g. "1.20" and "-034" to the left of "=>" will yield the strings "1.2" and "-34", respectively).

    Are you certain about this? Have you tested your expectation? (Looking pointedly at "-034"...)

Re^2: Clarifying the Comma Operator
by JavaFan (Canon) on Jun 07, 2009 at 12:13 UTC
    Any "word" that can be interpreted as a number will be converted to a string after evaluating its numeric value (e.g. "1.20" and "-034" to the left of "=>" will yield the strings "1.2" and "-34", respectively).
    No, it doesn't.
    perl -MDevel::Peek -we '@f = (1.20 => "1.2"); Dump $f[0]; Dump $f[1]' SV = NV(0x88238e8) at 0x87fc230 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 1.2 SV = PV(0x87f9044) at 0x87fc424 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x8806fc8 "1.2"\0 CUR = 3 LEN = 4
    As you can see, the 1.20 is not a string (there's no PV value, nor is POK or pPOK set), while "1.2" is a string (and not a number).
      In  ( 1.20 => 'foo' ) I'm not saying that the "1.20" is a string. The point is that in order to build a string from a "word" like 1.20 that sits to the left of "=>", perl determines its numeric value (NV), which it then "stringifies" to the simplest possible form ("1.2") -- and this is what the OP was struggling with.