francisongsw has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have been receiving this error and I do not see what is wrong. Can't modify constant item in addition (+) at Y:\Perl2\GSI.pl line 216, near "}" Execution of Y:\Perl2\GSI.pl aborted due to compilation errors. This is my code:
my ($alarmed,$flawed,$inspected); open (FILE2, "<$path2") or die "Cannot open $path2"; $record2 = <FILE2>; #skip first line while ($record2 = <FILE2>){ my @temporary = split(",",$record2); if ($temporary[4] eq "alarmed") { alarmed += $temporary[5] ### ERROR LINE ### #print "$temporary[5]\n"; } } close(FILE2);
I do not see why as I did not declare 'alarmed' as a constant. Any help is very much appreciated. Thanks you!

Replies are listed 'Best First'.
Re: HELP!!! Can't modify constant item in addition (+) at .....
by jdporter (Paladin) on Apr 04, 2005 at 09:30 UTC
    Um... You're missing the dollar sign in front of that alarmed. I.e. $alarmed

    You might also want to put a semicolon at the end of that statement, in case you ever need to uncomment the following print statement. :-)

Re: HELP!!! Can't modify constant item in addition (+) at .....
by gellyfish (Monsignor) on Apr 04, 2005 at 09:32 UTC

    Er, you missed off the sigil off $alarmed. use strict would have indicated this typo at compile time.

    /J\<?p>

Re: HELP!!! Can't modify constant item in addition (+) at .....
by Zaxo (Archbishop) on Apr 04, 2005 at 09:33 UTC

    The 'alarmed' you're modifying needs a sigil. You're trying to add to the constant 'alarmed'. use strict; would have caught that.

    $alarmed += $temporary[5] ### ERROR LINE ### # ^

    After Compline,
    Zaxo

Re: HELP!!! Can't modify constant item in addition (+) at .....
by ysth (Canon) on Apr 04, 2005 at 10:30 UTC
    alarmed there is what's called a bareword: since alarmed isn't a built-in function of perl or a function you've declared, it is treated as a string constant "alarmed". And trying to add to a constant gives you a Can't modify constant error. (Using strict turns use of a bareword into a fatal error, but in this case the Can't modify constant error is given first anyway.)