http://www.perlmonks.org?node_id=977495


in reply to Still don't undersand "Modification of a read-only value attempted" error

OK, I understand the difference between a variable and a constant. But my problem is that I can't grasp exactly when Perl decides which one it is. My thoughts are..."abcdef" is a constant of type string. It looks like in both cases, it is eventually assigned to $item. If it is assigned to a element of @list first, then assigned to $item in the foreach statement, it still seems that $item contains a string. It seems like in the end, $item contains the same thing. Now does this have somehting to do with $_ and is the error msg really refering to $_ as the Read Only constant?

  • Comment on Re: Still don't undersand "Modification of a read-only value attempted" error

Replies are listed 'Best First'.
Re^2: Still don't undersand "Modification of a read-only value attempted" error
by Eliya (Vicar) on Jun 20, 2012 at 22:39 UTC
    in both cases, it is eventually assigned to $item.

    No, nothing is being assigned to $item. Rather, in the error case, $item becomes an alias, i.e. an alternate way to access the same instance of the constant string "abcdef".

      BING.....Light bulb just came on....Thank you so much. I was pulling my hair out...and I don't have any to spare....Thanks again

Re^2: Still don't undersand "Modification of a read-only value attempted" error
by gg48gg (Sexton) on Jun 21, 2012 at 04:18 UTC
    another way to put it is that a list is read only, while an array is not.
Re^2: Still don't undersand "Modification of a read-only value attempted" error
by gg48gg (Sexton) on Jun 21, 2012 at 04:15 UTC
    The error is telling you that you are trying to modify one of the constants in your list:
    ("readonlyval1","readonlyval2","readonlyval3")
    You are allowed to modify values in an array, which are not readonly, like:
    $item[0]=~s/foo/bar; $item[1]="something_else"; #no can do: something=something_else