demerphq wrote:
>I think it is better to avoid making judgements like this until you
>actually know what code is involved.
The issue isn't that it's a lot of core code, but the extent of the
code written in Perl that could potentially be affected. local has been
around for a long time, including the ability to localise elements of a
read-only container. More of an issue for arrays than hashes, though,
I think, because restricted hashes aren't quite so old or widely used.
>Needs tests, but it pretty much works afaict:
>
>https://github.com/Perl/perl5/pull/20963
That prevents unpermitted changes being made by the inward phase of
localisation, but it's still possible for the restoration to make an
unpermitted change if the hash became more restricted during the scope
of the localisation:
$ perl -lwe '%a=qw(a z c d); { local $a{a} = "b"; Internals::SvREADONLY(%a, 1); Internals::SvREADONLY($a{a}, 1); print %a; } print %a'
cdab
cdaz
There's some question about the legitimacy of that particular test case:
local specifically supplies a mutable scalar, and is the user implicitly
promising not to make it readonly? In general I think it's legitimate
to make a thing readonly after a mutable construction phase, and in this
case that goes for both the hash and the scalar. But I don't have a
feel for what sort of things ought to be possible with a restricted hash.
I find them a very weird concept, especially in the way that mutability
of the hash container depends on mutability of the scalar object that
it contains.
There are many other permutations to consider. A particularly neat one,
because it doesn't require any explicit readonly flag setting within
the local scope, comes from combining local and refaliasing:
$ perl -Mexperimental=refaliasing,declared_refs -lwe '%a=qw(a z c d); Internals::SvREADONLY(%a, 1); { local \$a{a} = \"b"; print %a; } print %a'
abcd
azcd
It's arguable that that one is a distinct bug. If one makes sense of a
restricted hash by saying that the readonlyness of the element scalars is
semantically part of the container, then it is wrong for refaliasing to
be willing to put a readonly scalar into such a hash, whether localised
or not. But that's probably the wrong view of restricted hashes.
-zefram