in reply to Modification of a read-only value attempted?!?
Solutions:
perl -e'for ( 1 ) { my $x = pack "I", $_; }'
perl -e'for my $x ( 1 ) { my $y = pack "I", $x; }'
perl -e'for my $x ( 1 ) { my $x = pack "I", $x; }'
perl -e'for my $x ( my $tmp = 1 ) { $x = pack "I", $x; }'
Explanation:
1 returns a read-only scalar. It's read-only because a given 1 literal always returns the same scalar.
Foreach aliases the variable, so $x is an alias of that read-only scalar.
To understand what that means, it might be clearer if we take the foreach out of the equation.
use feature qw( say ); use experimental qw( refaliasing declared_refs ); my $y = 1; # Make `$y` have the same value as `1`. say $y; # 1 $y = 2; # Make `$y` have the same value as `2`. say $y; # 2 my \$x = \1; # Make `$x` an alias of `1`. say $x; # 1 $x = 2; # Modification of a read-only value attempted say $x;
Just like in your code, the last assignment in this program dies because it attempts to modify $x and thus the scalar to which 1 evaluates. It would be bad if 1 started to evaluate to a scalar with a value other than one.
|
|---|