As I understand it, the comments in the following two subs are correct.
sub foo (@array) {
my $x = @array[0]; # ok, allowed to read
@array[0] = "something"; # compile fails because '@array is ro'
}
sub bar (@array_2d) {
my $x = @array[0]; # ok, allowed to read
$x[0] = "something"; # ok, said nothing about contents
}
That is, you can't STORE into a read-only variable, but if the thing
you FETCH has its own STORE method then that method will work fine.
What I would like is something like the following to prevent corruption
of complex data structures passed to subroutines.
sub baz (@array_2d is very_read_only) {
my $x = @array[0]; # ok
$x[0] = "something"; # would croak at runtime but ...
@array[0][0] = "something"; # ... this already failed to compile
}
Are there any plans for such a trait or would it be easy for a user to
implement? I have no idea how to implement it; presumably the
variable's FETCH method would return an alias with the same
very_read_only trait attached. I'm sure this ain't even close:
method FETCH ($var, $index) {
my $val = $var[$index];
return $val unless $val.can("STORE");
my $x is very_read_only := $val;
return $x;
}
And how the trait connects to the FETCH-replacing I don't even know
where to begin.
--
Rick Delaney
[EMAIL PROTECTED]