Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modification of a read-only value attempted

Tags:

perl

I've googled the error (stated in the Question title) and can't find anything relevant.

#!/usr/bin/perl

use strict;

my %cc;

my @cc => (3,4,5,6,6,7,7);
$cc{key} = \@cc;

$0 =  $cc{key}[0] * 2;
$1 =  $cc{key}[1] * 1;  #error here

my $total = $0 + $1;

print "$1";
print "$total";

line 11 is my error...

like image 364
mrlayance Avatar asked Apr 06 '11 15:04

mrlayance


1 Answers

$0 and $1 are special variables in Perl (see perldoc perlvar). $1 is a read only variable.

Also, you should enable warnings to see many more problems with your code.

My guess is that you want something like this:

use strict;
use warnings;

my @cc = (3,4,5,6,6,7,7);
my %cc;
$cc{key} = \@cc;

my $k0 = $cc{key}[0] * 2;
my $k1 = $cc{key}[1] * 1;
my $total = $k0 + $k1;

print "$k1\n";
print "$total\n";

perldoc perldiag has more information on the error message: "Modification of a read-only value attempted".

like image 106
toolic Avatar answered Sep 22 '22 20:09

toolic