Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Surprise: "Can't modify constant item in scalar assignment" [SOLVED]

by sundialsvc4 (Abbot)
on Jan 19, 2012 at 16:16 UTC ( #948803=perlquestion: print w/ replies, xml ) Need Help??
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.)

Comment on Surprise: "Can't modify constant item in scalar assignment" [SOLVED]
Download Code
Re: Surprise: "Can't modify constant item in scalar assignment" [SOLVED]
by LanX (Canon) on Jan 19, 2012 at 16:25 UTC
    > Did you spot it?

    Yes, I did!

    If you don't, you might want consider using syntax-coloring of variables and an editor which highlights/jumps-to error-lines.

    UPDATE:

    Did you really use strict?

    Bareword "message" not allowed while "strict subs" in use at /home/lanx/tmp/tst.pl line 12.

    UPDATE:

    > I would have expected to be notified about the bareword.

    IMHO barewords can still be lvalue-subs, where your code has perfectly legal syntax.

    use strict; use warnings; use JSON; sub message :lvalue { my $a; } message = { result => JSON::false, message => "You messed up", };

    runs without problems.

    In your case it's a constant bareword.

    Cheers Rolf

Re: Surprise: "Can't modify constant item in scalar assignment" [SOLVED]
by JavaFan (Canon) on Jan 19, 2012 at 16:26 UTC
    Did you spot it?
    Spotted before reaching the Did you spot it?.

    I've been leaving off sigils for more than 15 years. Count your blessing you make that typo so infrequently, you're surprised by the error.

    I had to “Google It™”
    man perldiag
    and search for modify.
Re: Surprise: "Can't modify constant item in scalar assignment" [SOLVED]
by Anonymous Monk on Jan 19, 2012 at 16:26 UTC
    This code gives: Bareword "message" not allowed while "strict subs" in use at…

    If you don't see that, then you do already have a sub message in scope, so strict cannot help you without being able to read your mind.

Re: Surprise: "Can't modify constant item in scalar assignment"
by ikegami (Pope) on Jan 19, 2012 at 19:41 UTC

    I was thinking "what language is that?" before I even started reading, so yeah, I spotted it.

    Now you could say that was because you removed everything but the problem, but it's just as easy to discover analytically. What does an assignment try to modify? It's LHS. What's wrong with "message"? Well, that's obvious.

    Warnings don't care about bare words, it's strict.

    $ perl -ce'use strict; message = "foo";' Can't modify constant item in scalar assignment at -e line 1, near ""f +oo";" Bareword "message" not allowed while "strict subs" in use at -e line 1 +. -e had compilation errors.

    Note that strict only cares if the bareword is not allowed by some other rule, such as if it appears to be a sub call.

    $ perl -ce'use strict; sub message; message = "foo";' Can't modify non-lvalue subroutine call in scalar assignment at -e lin +e 1, near ""foo";" -e had compilation errors.

    The above code is parsed as message() = "foo";, which is valid syntax, and it's even valid semantically if message is an lvalue sub.

    $ perl -ce'use strict; sub message :lvalue; message = "foo";' -e syntax OK

    By the way, where's the my?

    $ perl -ce'use strict; my message = "foo";' No such class message at -e line 1, near "; my message" syntax error at -e line 1, near "my message =" -e had compilation errors.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://948803]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-11-27 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (187 votes), past polls