Mailing List Archive

Constant perl version - __PERLVER__ ?
The version of perl that is executing any particular piece of code is
really expected to remain constant. I think that is fairly
uncontentious ;)

However, because the value is only available in scalar variables like
$] and $^V, it means that conditional expressions that one would hope
should be "constant" are not in fact subject to const-folding. For
example:

$ perl -MO=Concise -E 'say "Old" if $] < 5.014'
a <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter v ->2
2 <;> nextstate(main 2 -e:1) v:%,us,{,fea=15 ->3
- <1> null vK/1 ->a
6 <|> and(other->7) vK/1 ->a
5 <2> lt sK/2 ->6
- <1> ex-rv2sv sK/1 ->4
3 <#> gvsv[*]] s ->4
4 <$> const[NV 5.01] s ->5
9 <@> say vK ->a
7 <0> pushmark s ->8
8 <$> const[PV "Old"] s ->9

and similar for `... if $^V lt v5.14`

I would like to propose a new double-under special symbol containing
the same value, that expands to a real constant in the code; and
therefore such a conditional expression would const-fold correctly.

Hypothetically then

perl ... -E 'say "Old" if __PERLVER__ lt v5.14'

would compile to either nothing at all, or a simple unconditional say()
statement.


Alternative ideas include: recognising that $^V or $] are readonly
during the peephole optimiser and inlining the values, but.. eugh.
That's a mess that easily leads to craziness...

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: Constant perl version - __PERLVER__ ? [ In reply to ]
* Paul "LeoNerd" Evans <leonerd@leonerd.org.uk> [2023-04-29 15:37:57 +0100]:

> The version of perl that is executing any particular piece of code is
> really expected to remain constant. I think that is fairly
> uncontentious ;)
>
> However, because the value is only available in scalar variables like
> $] and $^V, it means that conditional expressions that one would hope
> should be "constant" are not in fact subject to const-folding. For
> example:
>
> $ perl -MO=Concise -E 'say "Old" if $] < 5.014'
> a <@> leave[1 ref] vKP/REFC ->(end)
> 1 <0> enter v ->2
> 2 <;> nextstate(main 2 -e:1) v:%,us,{,fea=15 ->3
> - <1> null vK/1 ->a
> 6 <|> and(other->7) vK/1 ->a
> 5 <2> lt sK/2 ->6
> - <1> ex-rv2sv sK/1 ->4
> 3 <#> gvsv[*]] s ->4
> 4 <$> const[NV 5.01] s ->5
> 9 <@> say vK ->a
> 7 <0> pushmark s ->8
> 8 <$> const[PV "Old"] s ->9
>
> and similar for `... if $^V lt v5.14`
>
> I would like to propose a new double-under special symbol containing
> the same value, that expands to a real constant in the code; and
> therefore such a conditional expression would const-fold correctly.
>
> Hypothetically then
>
> perl ... -E 'say "Old" if __PERLVER__ lt v5.14'
>
> would compile to either nothing at all, or a simple unconditional say()
> statement.
>
>
> Alternative ideas include: recognising that $^V or $] are readonly
> during the peephole optimiser and inlining the values, but.. eugh.
> That's a mess that easily leads to craziness...

I like the idea in general, assuming I understand what you're suggesting, but
how would this work on "old" perls? You have "lt v5.14" but here's on v5.36:

$ perl --version

This is perl 5, version 36, subversion 0 (v5.36.0) built for x86_64-netbsd-thread-multi

Copyright 1987-2022, Larry Wall
...

With,

use strict;
use warnings;
print __PACKAGE__ . "\n";
print __PERLVERS__ . "\n";

I get,

Bareword "__PERLVERS__" not allowed while "strict subs" in use at ./test.pl line 5.
Execution of ./test.pl aborted due to compilation errors.

Cheers,
Brett

>
> --
> Paul "LeoNerd" Evans
>
> leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
> http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/

--
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native
Re: Constant perl version - __PERLVER__ ? [ In reply to ]
On Sat, Apr 29, 2023 at 4:10?PM Oodler 577 via perl5-porters <
perl5-porters@perl.org> wrote:

> * Paul "LeoNerd" Evans <leonerd@leonerd.org.uk> [2023-04-29 15:37:57
> +0100]:
>
> > The version of perl that is executing any particular piece of code is
> > really expected to remain constant. I think that is fairly
> > uncontentious ;)
> >
> > However, because the value is only available in scalar variables like
> > $] and $^V, it means that conditional expressions that one would hope
> > should be "constant" are not in fact subject to const-folding. For
> > example:
> >
> > $ perl -MO=Concise -E 'say "Old" if $] < 5.014'
> > a <@> leave[1 ref] vKP/REFC ->(end)
> > 1 <0> enter v ->2
> > 2 <;> nextstate(main 2 -e:1) v:%,us,{,fea=15 ->3
> > - <1> null vK/1 ->a
> > 6 <|> and(other->7) vK/1 ->a
> > 5 <2> lt sK/2 ->6
> > - <1> ex-rv2sv sK/1 ->4
> > 3 <#> gvsv[*]] s ->4
> > 4 <$> const[NV 5.01] s ->5
> > 9 <@> say vK ->a
> > 7 <0> pushmark s ->8
> > 8 <$> const[PV "Old"] s ->9
> >
> > and similar for `... if $^V lt v5.14`
> >
> > I would like to propose a new double-under special symbol containing
> > the same value, that expands to a real constant in the code; and
> > therefore such a conditional expression would const-fold correctly.
> >
> > Hypothetically then
> >
> > perl ... -E 'say "Old" if __PERLVER__ lt v5.14'
> >
> > would compile to either nothing at all, or a simple unconditional say()
> > statement.
> >
> >
> > Alternative ideas include: recognising that $^V or $] are readonly
> > during the peephole optimiser and inlining the values, but.. eugh.
> > That's a mess that easily leads to craziness...
>
> I like the idea in general, assuming I understand what you're suggesting,
> but
> how would this work on "old" perls? You have "lt v5.14" but here's on
> v5.36:
>
> $ perl --version
>
> This is perl 5, version 36, subversion 0 (v5.36.0) built for
> x86_64-netbsd-thread-multi
>
> Copyright 1987-2022, Larry Wall
> ...
>
> With,
>
> use strict;
> use warnings;
> print __PACKAGE__ . "\n";
> print __PERLVERS__ . "\n";
>
> I get,
>
> Bareword "__PERLVERS__" not allowed while "strict subs" in use at ./
> test.pl line 5.
> Execution of ./test.pl aborted due to compilation errors.
>

Like most new features, it sadly will not be able to time travel. But it
would be trivial to provide a compatibility keyword via a module.
Re: Constant perl version - __PERLVER__ ? [ In reply to ]
On Sat, 29 Apr 2023 at 16:38, Paul "LeoNerd" Evans
<leonerd@leonerd.org.uk> wrote:
>
> The version of perl that is executing any particular piece of code is
> really expected to remain constant. I think that is fairly
> uncontentious ;)
>
> However, because the value is only available in scalar variables like
> $] and $^V, it means that conditional expressions that one would hope
> should be "constant" are not in fact subject to const-folding. For
> example:
>
> $ perl -MO=Concise -E 'say "Old" if $] < 5.014'
> a <@> leave[1 ref] vKP/REFC ->(end)
> 1 <0> enter v ->2
> 2 <;> nextstate(main 2 -e:1) v:%,us,{,fea=15 ->3
> - <1> null vK/1 ->a
> 6 <|> and(other->7) vK/1 ->a
> 5 <2> lt sK/2 ->6
> - <1> ex-rv2sv sK/1 ->4
> 3 <#> gvsv[*]] s ->4
> 4 <$> const[NV 5.01] s ->5
> 9 <@> say vK ->a
> 7 <0> pushmark s ->8
> 8 <$> const[PV "Old"] s ->9
>
> and similar for `... if $^V lt v5.14`
>
> I would like to propose a new double-under special symbol containing
> the same value, that expands to a real constant in the code; and
> therefore such a conditional expression would const-fold correctly.
>
> Hypothetically then
>
> perl ... -E 'say "Old" if __PERLVER__ lt v5.14'
>
> would compile to either nothing at all, or a simple unconditional say()
> statement.
>
>
> Alternative ideas include: recognising that $^V or $] are readonly
> during the peephole optimiser and inlining the values, but.. eugh.
> That's a mess that easily leads to craziness...

I dont think we need this. Anyone can just write:

use constant PERL_VERSION => $^V;

I dont think we need to add a new symbol for this, and it would just
be a backwards compatibility trap with little benefit over writing
this out.

You can write a module and call it Perl::Version and put it on CPAN
which does this, no need for special magic tokens in Perl itself.

cheers,
Yves


--
perl -Mre=debug -e "/just|another|perl|hacker/"
Re: Constant perl version - __PERLVER__ ? [ In reply to ]
On Sun, 30 Apr 2023 12:03:58 +0200
demerphq <demerphq@gmail.com> wrote:

> I dont think we need this. Anyone can just write:
>
> use constant PERL_VERSION => $^V;
>
> I dont think we need to add a new symbol for this, and it would just
> be a backwards compatibility trap with little benefit over writing
> this out.

Ahyes; in fact to be fair nothing stops code right now doing

use constant __PERLVER__ => $^V;

so maybe that's sufficient ;)

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/