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

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

by Dan.Pinvidic (Initiate)
on Jun 20, 2012 at 22:24 UTC ( #977495=note: print w/ replies, xml ) Need Help??


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
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: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
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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-09-02 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (32 votes), past polls