|go ahead... be a heretic|
Re^2: 2 newby questionsby Athanasius (Canon)
|on Jun 14, 2012 at 04:44 UTC||Need Help??|
"Why not try it?"
Excellent advice, especially with the caveats given.
PS: the answer is "no."
Some clarification may help the OP:
There are 4 ways to increment a counter by 1:
First, note that the $ prefix is required in $word here (this was supplied in the OP’s code, but not in the OP’s question). The difference between (C) and (D) is detailed in perldoc.
The real difference arises when warnings are turned on. (A) produces warnings of the form:
Use of uninitialized value within %count in addition (+) at ... line ..., <STDIN> line 1.
whereas (B), (C), and (D) do not. In all 4 cases, Perl creates the hash entry by a process called autovivification (on which there is a useful tutorial by Uri Guttman), which creates a new hash entry with the specified key and assigns it a value of undef. The difference is that in (A), Perl initially sees you accessing this undefined hash entry in the addition on the right of the assignment and issues a warning, whereas in (B), (C), and (D), Perl is smart enough to know that the warning is not needed. But in their effects, (A), (B), (C), and (D) are the same, in that each increments by 1 the value of the %count entry which has $word as its key.
Update: SuicideJunkie (below) makes an interesting point. A good place to follow-up on it is in the thread begun by First JAPH - Spell perl in two hundred and eighty five thousand and seventy four easy steps, a 10-year-old post which turns up from time to time in Selected Best Nodes.
Athanasius <°(((>< contra mundum