wfsp has asked for the wisdom of the Perl Monks concerning the following question:
This snippet produces the error message:#!/usr/bin/perl use strict; use warnings; #my $number = get_number('one'); #print "$number\n"; for (qw(one two three)){ my $number = get_number($_); print "$number\n"; } { my $hash_ref; sub get_number{ my ($num) = @_; unless (defined $hash_ref){ while (<DATA>){ chomp; my ($str, $num) = split /\|/; $hash_ref->{$str} = $num; } } return $hash_ref->{$num}; } } __DATA__ one|1 two|2 three|3
Uncomment the two lines at the top of script and the output is as expected:Modification of a read-only value attempted at closure_test.pl line 19 +.
Can anyone help explain this behaviour?1 1 2 3
Update 2
Ack! The first update was rubbish! Apologies.
Update
Deleted.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Closure producing 'Modification of read-only value' error
by eyepopslikeamosquito (Archbishop) on Jun 11, 2005 at 08:47 UTC | |
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 | |
|
Re: Closure producing 'Modification of read-only value' error
by tlm (Prior) on Jun 11, 2005 at 11:24 UTC | |
by hv (Prior) on Jun 11, 2005 at 11:49 UTC |