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

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

Thread Previous | Thread Next
Aristotle Pagaltzis
August 4, 2016 01:07
Re: [perl #128786] making $^V a version object broke functionality
Message ID:
* Lukas Mai <> [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, 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 <> [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
      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?

Aristotle Pagaltzis // <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About