in reply to Closure producing 'Modification of read-only value' error
The $_ of your for loop is aliased to the read-only strings qw(one two three). The while loop inside the sub overwrites $_, hence the "Modification of a read-only value attempted" error. If you add the line:
inside your sub get_number it works. Generally, mangling $_ inside a subroutine is poor form.local $_;
Update: a cleaner way to ensure the sub plays well with others is to simply change the while loop to use a lexical rather than the implicit $_:
while (defined(my $d = <DATA>)){ chomp $d; my ($str, $num) = split /\|/, $d; $hash_ref->{$str} = $num; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Closure producing 'Modification of read-only value' error
by nobull (Friar) on Jun 11, 2005 at 11:04 UTC | |
by tlm (Prior) on Jun 11, 2005 at 16:39 UTC | |
by nobull (Friar) on Jun 11, 2005 at 19:09 UTC | |
by kaif (Friar) on Jun 13, 2005 at 02:03 UTC |