in reply to read-only error

When perl dies/warns with a message that doesn't end in a newline, it displays the file name and the line number where die was called.

If an I/O read operation has been performed and the handle is still open, perl will also dipslay the name of the handle and the number of lines that have been read from that file handle. (Well, $. really.) This helps debug errors based on inputed data.

See Playing with "<FH> line 123" die messages.

Update: As for the "Modification of a read-only value attempted" part, that usually happens when a variable (usually $_) is aliased to a constant, and a value is assigned to that variable. For example,

sub trim { $_ = $_[0]; # Assigns to $_, but $_ is aliased to " abc ". s/^\s+//g; s/\s+$//g; return $_; } # Fix: # sub trim { # for ($_[0]) { # foreach (unlike while) localizes the loop var. # s/^\s+//g; # s/\s+$//g; # return $_; # } # } print trim($_) for " abc ", " def ", " ghi "

and

sub f { my $pos = tell(DATA); while (<DATA>) { # Assigns to $_, but $_ is aliased to 1. print; } seek(DATA, $pos, 0); } # sub f { # my $pos = tell(DATA); # while (defined(my $line = <DATA>)) { # print $line; # } # seek(DATA, $pos, 0); # } f() for 1,2,3; __DATA__ a b c

Replies are listed 'Best First'.
Re^2: read-only error
by imp (Priest) on Aug 24, 2006 at 19:38 UTC
    Another cause for the Modification of a read-only value error is using a sort block that dereferences $a and $b, and has gaps.

    This is ok:

    use strict; use warnings; my @list; $list[0] = 0; $list[2] = 2; @list = sort {$a <=> $b} @list;
    This isn't:
    use strict; use warnings; my @list; $list[0] = [0]; $list[2] = [2]; @list = sort {$$a[0] <=> $$b[0]} @list;

      $a and $b are read-only in a sort block.

      @list = sort { $a = 1; } @list;

      You're getting the error because you are attempting to auto-vivify them.

      I hate auto-vivification, or at least that it's on by default.