Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Modification of a read-only value" Error. Why?

Status
Not open for further replies.

m4trix

Vendor
Jul 31, 2002
84
CA
if I have this code (using on a mysql database):
[tt]while (my $row = $sth->fetchrow_arrayref) {
foreach (@$row) { $_ = "NULL" unless defined }
my (@end) = splice(@$row, -2);
print join (", ",@$row)." - ".join(", ",@end)."\n";
}[/tt]

it will print the first row as it should, then it gives a "Modification of a read-only value attempted...." error.

Why is this? and how do I avoid it? I know I can avoid it by not using a reference, ie this works fine:

[tt]while (my @row = $sth->fetchrow_array) {
foreach (@row) { $_ = "NULL" unless defined }
my (@end) = splice(@row, -2);
print join (", ",@row)." - ".join(", ",@end)."\n";
}[/tt]

But I'm curious why the reference version gives the error. Is there any way I can do this without using the non-reference version? I know either way it's not a big deal.

thanks
 
because it is a reference to the value ie the memory location of the value not the value itself. Which is why you have to dereference data to work with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top