locked_user sundialsvc4 has asked for the wisdom of the Perl Monks concerning the following question:
I was surprised by the error message, “can’t modify constant item in scalar assignment” in relation to this (example) bit of code:
use strict; use warnings; use JSON; ... message = { result => JSON::false, message => "You messed up", };
Did you spot it? At first, neither did I. Initially, I assumed that it must have something to do with JSON::false, but it didn’t. The actual problem, of course, is that message should be $message. In my haste, I simply left out the dollar-sign. With the tpyo corrected, the message promptly went away, but I thought I’d document it here because it did surprise me. Given that I have specified use warnings; I would have expected to be notified about the bareword. To find the resolution, I had to “Google It™” and the initial responses I found were not terribly helpful. (Most of them had to do with the need to specify => instead of =, which of course is also true. I humbly confess that I didn’t search PerlMonks first.) So I thought to take a moment to “write it up,” so that: Now, You Know.™
(Perl 5.10.0; OS/X.)
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Surprise: "Can't modify constant item in scalar assignment" [SOLVED]
by LanX (Saint) on Jan 19, 2012 at 16:25 UTC | |
|
Re: Surprise: "Can't modify constant item in scalar assignment" [SOLVED]
by JavaFan (Canon) on Jan 19, 2012 at 16:26 UTC | |
|
Re: Surprise: "Can't modify constant item in scalar assignment"
by ikegami (Patriarch) on Jan 19, 2012 at 19:41 UTC | |
|
Re: Surprise: "Can't modify constant item in scalar assignment" [SOLVED]
by Anonymous Monk on Jan 19, 2012 at 16:26 UTC |