in reply to Clarifying the Comma Operator
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 | |
|
Re^2: Clarifying the Comma Operator
by Porculus (Hermit) on Jun 07, 2009 at 11:14 UTC | |
|
Re^2: Clarifying the Comma Operator
by JavaFan (Canon) on Jun 07, 2009 at 12:13 UTC | |
by graff (Chancellor) on Jun 07, 2009 at 19:09 UTC |