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

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

Thread Previous | Thread Next
From:
Lukas Mai
Date:
July 31, 2016 19:46
Subject:
Re: [perl #128786] making $^V a version object broke functionality
Message ID:
3a2cd356-042f-aa5d-d02e-058732af0ada@gmail.com
Am 31.07.2016 um 17:48 schrieb John Peacock:
> On 07/31/2016 10:04 AM, l.mai@web.de wrote:
>> # New Ticket Created by  l.mai@web.de
>> # Please include the string:  [perl #128786]
>> # in the subject line of all future correspondence about this issue.
>> # <URL: https://rt.perl.org/Ticket/Display.html?id=128786 >
>
> Perl objects are typically just hashes of stuff, blessed into a class, e.g.
>
>  $ perl -w -MData::Dumper -E 'say Dumper($^V);'
>    $VAR1 = bless( {
>                  'original' => 'v5.20.2',
>                  'version' => [
>                                 5,
>                                 20,
>                                 2
>                               ],
>                  'qv' => 1
>                }, 'version' );
>
> So your code could be rewritten as:
>
>    printf "use feature ':%d.$d;\n", @{$^V->{version}}[0,1];

That's not part of the public API. I'd have to rely on implementation 
details that might change at any time.

Can this code be written at all using just the public API and without 
reparsing the pretty-printed version number? (E.g. it's easy enough to 
do 'my @v = split /\./, sprintf "%vd", $^V;' but that's cheating. And a 
bit awful.)

> v-strings are intrinsically a binary format, so you MUST treat it
> specially when trying to print or compare.  Version objects can be
> treated as a normal scalar and printed/compared using non-special methods.

1. v-strings are normal scalars: they're strings.
2. No special treatment is required for comparing v-strings. v-string 
comparison is normal string comparison.
3. Special treatment is required for comparing version objects. version 
object comparison is not normal string comparison, and inadvertently 
stringifying a version object and then comparing it leads to wrong results.
4. Pretty-printing a version number is rare in my code. Most of the time 
I only want to compare versions, extract components, and construct new 
versions (e.g. for automatically incrementing a version number, etc).

> If you'd like to open a feature request for version.pm to include
> methods to extract the epoch/major/minor/release from a version objects,
> I'd be happy to add one.  This is the first time in nearly 10 years that
> anyone has requested such a thing, but it is easy enough to add.

I'm not directly concerned with version.pm. I've never had a use for it.

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.

In that case a method to return the numeric components would be nice, as 
well as a method to get the v-string representation:

     my @parts = $^V->parts;    # (5, 24, 0)
     my $str   = $^V->vstring;  # "\x05\x18\x00"

Would that be good addition to the version.pm API?

-- 
Lukas Mai <plokinom@gmail.com>

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