On Wed, 17 Jan 2024 11:23:28 +0000
"Paul \"LeoNerd\" Evans" <
leo...@leonerd.org.uk> wrote:
> A while ago [1] we discussed the idea of getting e.g.
>
> use v5.40;
>
> to imply
>
> use builtin ':5.40';
>
> so as to import all the stable builtins at that version. The
> discussion got a bit derailed in the details of how to do lexical
> unimport, but I believe all that is now resolved and we can get back
> to this.
Hmmm. In writing the documentation for this I have hit upon a snag.
Implementing the first bit was really easy; it's basically a 3-line
addition into op.c to just activate the builtin bundle. So now this
works:
$ ./perl -e 'use v5.39.6; say "True is ", true'
True is 1
I then looked at updating the documentation about `use VERSION` and
found the wording about what happens when you reset back down to a
lower version number again. It specifically says of the strict,
warnings and feature pragmata, that it resets them down again and
removes whatever shouldn't be visible. If builtin bundles were to work
the same way, then this ought to fail:
$ ./perl -ce 'use v5.39; use v5.20; my $t = true'
-e syntax OK
But, it currently succeeds. The behaviour here ought to be that the
`use v5.20` throws out the imported builtin functions. Currently that
kind of behaviour doesn't exist.
Back in Perl 5.36 we added a trapdoor to `use VERSION` to warn when
downgrading back before the 5.11 boundary (when the implicit 'strict'
flags were enabled), in order to give us some room to get rid of those
flags and free up some hints bits. I wonder if a similar warning or
outright-error should happen if you ask to downgrade over the 5.39
boundary. I.e. that upgrades of the prevailing `use VERSION` are always
permitted, but going back down again is not permitted past that.
This would make theabove example into an outright error:
$ ./perl -ce 'use v5.39; use v5.20;'
Downgrading a use VERSION declaration to below v5.39 is not permitted
at -e line 1.
Since the effects are entirely lexically scoped, you're still perfectly
permitted to have small islands of higher-versions within a file if you
need it, because the scoping rules already handle how to tidy up:
use v5.20;
{
use v5.39.6;
my $t = true; # perfectly fine
}
# now we're back in 5.20-land with none of that visible anyway.
Adding such a trapdoor would simplify a lot of similar situations that
might come up in future, when `use VERSION` enables yet more things.
Not needing to answer the question of "how do you turn it off again?"
generally tidies things up.
In summary, what I'm asking is: How do we feel about disallowing
downgrades of `use VERSION` within a single scope? That the only way to
step backwards is to fall out of an inner scope with higher version,
back outside into a lower version again.