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; }
In reply to Re: Closure producing 'Modification of read-only value' error
by eyepopslikeamosquito
in thread Closure producing 'Modification of read-only value' error
by wfsp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |