develooper Front page | perl.perl5.porters | Postings from August 2016

Re: [perl #128786] making $^V a version object broke functionality

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
August 4, 2016 01:07
Subject:
Re: [perl #128786] making $^V a version object broke functionality
Message ID:
20160804010727.GB84380@plasmasturm.org
* Lukas Mai <plokinom@gmail.com> [2016-07-31 21:48]:
> My main concern is being able to introspect the version number of the
> currently running perl. If that has to go through version.pm, OK.

Just use $]. $^V is some hypey modernist nonsense boondoggle. $] is
simple and clearly defined, string comparisons of its values Just Work,
it’s plain and obvious. It’s even nicer than the niceness of v-strings
you seem quite taken with.

The only annoyance you might have is that you get '5.022001' instead of
'5.22.1' if you print it verbatim (which you can’t do with v-strings
either). That was the whole cause of a gigantic goose chase.

However, don’t listen to this:

* Zefram <zefram@fysh.org> [2016-08-01 08:48]:
>     @parts = map { 0+$_ } sprintf("%010.6f", "$]") =~ /([0-9]{3})/g;

… because $] is not a float, and you get in trouble (that the manual
explicitly warns you about!) if you try to treat it that way.

    $ perl -MDevel::Peek -e 'Dump $]'
    SV = PV(0x7fd55b801280) at 0x7fd55b826790
      REFCNT = 1
      FLAGS = (POK,READONLY,pPOK)
      PV = 0x7fd55b40ee20 "5.016001"\0
      CUR = 8
      LEN = 16

It’s a string. And always has been. Treat it that way and it will treat
you well. Specifically it’s always been a string with some digits – more
specifically, always a single digit '5' – plus a dot and then 6 digits
after that. So instead of that borderline-obfuscated stuff, you ought to
write some variation on (the maximally future-paranoid)

    @parts = map 0+$_, $] =~ /\A(\d+)\.(\d{3})(\d{3})\z/gaa;

But for your stated main concern, I would instead go with

    printf "use feature ':5.%d';\n", substr $], 2, 3;

That’s about as simple as anything gets, aye?

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About