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 | |
by ikegami (Patriarch) on Aug 24, 2006 at 19:52 UTC |