Perl::Version(3pm) | User Contributed Perl Documentation | Perl::Version(3pm) |
Perl::Version - Parse and manipulate Perl version strings
This document describes Perl::Version version 1.013
use Perl::Version; # Init from string my $version = Perl::Version->new( '1.2.3' ); # Stringification preserves original format print "$version\n"; # prints '1.2.3' # Normalised print $version->normal, "\n"; # prints 'v1.2.3' # Numified print $version->numify, "\n"; # prints '1.002003' # Explicitly stringified print $version->stringify, "\n"; # prints '1.2.3' # Increment the subversion (the third component) $version->inc_subversion; # Stringification returns the updated version formatted # as the original was print "$version\n"; # prints '1.2.4' # Normalised print $version->normal, "\n"; # prints 'v1.2.4' # Numified print $version->numify, "\n"; # prints '1.002004' # Refer to subversion component by position ( zero based ) $version->increment( 2 ); print "$version\n"; # prints '1.2.5' # Increment the version (second component) which sets all # components to the right of it to zero. $version->inc_version; print "$version\n"; # prints '1.3.0' # Increment the revision (main version number) $version->inc_revision; print "$version\n"; # prints '2.0.0' # Increment the alpha number $version->inc_alpha; print "$version\n"; # prints '2.0.0_001'
Perl::Version provides a simple interface for parsing, manipulating and formatting Perl version strings.
Unlike version.pm (which concentrates on parsing and comparing version strings) Perl::Version is designed for cases where you'd like to parse a version, modify it and get back the modified version formatted like the original.
For example:
my $version = Perl::Version->new( '1.2.3' ); $version->inc_version; print "$version\n";
prints
1.3.0
whereas
my $version = Perl::Version->new( 'v1.02.03' ); $version->inc_version; print "$version\n";
prints
v1.03.00
Both are representations of the same version and they'd compare equal but their formatting is different.
Perl::Version tries hard to guess and recreate the format of the original version and in most cases it succeeds. In rare cases the formatting is ambiguous. Consider
1.10.03
Do you suppose that second component '10' is zero padded like the third component? Perl::Version will assume that it is:
my $version = Perl::Version->new( '1.10.03' ); $version->inc_revision; print "$version\n";
will print
2.00.00
If all of the components after the first are the same length (two characters in this case) and any of them begins with a zero Perl::Version will assume that they're all zero padded to the same length.
The first component and any alpha suffix are handled separately. In each case if either of them starts with a zero they will be zero padded to the same length when stringifying the version.
Perl::Version supports a few different version string formats.
my $version = Perl::Version->new( 1.2 ); print "$version\n";
prints
1.2
In fact there is no special treatment for versions that resemble decimal numbers. This is worthy of comment only because it differs from version.pm which treats actual numbers used as versions as a special case and performs various transformations on the stored version.
For example
my $version = Perl::Version->new( 1.002003004005006 ); print $version->normal;
prints
v1.2.3.4.5.6
Perl::Version will successfully parse vstrings
my $version = Perl::Version->new( v1.2 ); print "$version\n";
prints
v1.2
Note that stringifying a Perl::Version constructed from a vstring will result in a regular string. Because it has no way of knowing whether the vstring constant had a 'v' prefix it always generates one when stringifying back to a version string.
$VERSION = version->new( qw$Revision: 2.7 $ );
Perl::Version does the right thing with such versions so that
my $version = Perl::Version->new( qw$Revision: 2.7 $ ); $version->inc_revision; print "$version\n";
prints
Revision: 3.0
Real Numbers
Real numbers are stringified before parsing. This has two implications: trailing zeros after the decimal point will be lost and any underscore characters in the number are discarded.
Perl allows underscores anywhere in numeric constants as an aid to formatting. These are discarded when Perl converts the number into its internal format. This means that
# Numeric version print Perl::Version->new( 1.001_001 )->stringify;
prints
1.001001
but
# String version print Perl::Version->new( '1.001_001' )->stringify;
prints
1.001_001
as expected.
In general you should probably avoid versions expressed either as decimal numbers or vstrings. The safest option is to pass a regular string to Perl::Version->new().
Alpha Versions
By convention if a version string has suffix that consists of an underscore followed by one or more digits it represents an alpha or developer release. CPAN treats modules with such version strings specially to reflect their alpha status.
This alpha notation is one reason why using decimal numbers as versions is a bad idea. Underscore is a valid character in numeric constants which is discarded by Perl when a program's source is parsed so any intended alpha suffix will become part of the version number.
To be considered alpha a version must have a non-zero alpha component like this
3.0.4_001
Generally the alpha component will be formatted with leading zeros but this is not a requirement.
A version number consists of a series of components. By Perl convention the first three components are named 'revision', 'version' and 'subversion':
$ perl -V Summary of my perl5 (revision 5 version 8 subversion 6) configuration: (etc)
Perl::Version follows that convention. Any component may be accessed by passing a number from 0 to N-1 to the component or increment but for convenience the first three components are aliased as revision, version and subversion.
$version->increment( 0 );
is the same as
$version->inc_revision;
and
my $subv = $version->subversion;
is the same as
my $subv = $version->component( 2 );
The alpha component is named 'alpha'.
If you're familiar with version.pm you'll notice that there's a certain amount of overlap between what it does and this module. I originally created this module as a mutable subclass of version.pm but the requirement to be able to reformat a modified version to match the formatting of the original didn't sit well with version.pm's internals.
As a result this module is not dependent or based on version.pm.
my @version = ( '1.3.0', 'v1.03.00', '1.10.03', '2.00.00', '1.2', 'v1.2.3.4.5.6', 'v1.2', 'Revision: 3.0', '1.001001', '1.001_001', '3.0.4_001', ); for my $v ( @version ) { my $version = Perl::Version->new( $v ); $version->inc_version; print "$version\n"; }
prints
1.4.0 v1.04.00 1.11.00 2.01.00 1.3 v1.3.0.0.0.0 v1.3 Revision: 3.1 1.002000 1.002 3.1.0
In each case the incremented version is formatted in the same way as the original.
If no arguments are passed an empty version intialised to 'v0' will be constructed.
In order to support CVS version syntax
my $version = Perl::Version->new( qw$Revision: 2.7 $ );
"new" may be passed an array in which case it concatenates all of its arguments with spaces before parsing the result.
If the string can't be parsed as a version "new" will croak with a suitable error. See DIAGNOSTICS for more information.
# Set the subversion $version->component( 2, 17 ); # Get the revision my $rev = $version->component( 0 );
Instead of a component number you may pass a name: 'revision', 'version', 'subversion' or 'alpha':
my $rev = $version->component( 'revision' );
# Set the number of components $version->components( 4 ); # Get the number of components my $parts = $version->components; # Get the individual components as an array my @parts = $version->components; # Set the components from an array $version->components( [ 5, 9, 2 ] );
Hmm. That's a lot of interface for one subroutine. Sorry about that.
# Set alpha $version->alpha( 12 ); # Get alpha my $alp = $version->alpha;
$version->set( $other_version );
You may also set the version from a literal string:
$version->set( '1.2.3' );
The version will be updated to the value of the version string but will retain its current formatting.
my $version = Perl::Version->new( '3.1.4' ); $version->increment( 1 ); print "$version\n";
prints
3.2.0
Components to the right of the incremented component will be set to zero as will any alpha component.
As an alternative to passing a component number one of the predefined component names 'revision', 'version', 'subversion' or 'alpha' may be passed.
my $version = Perl::Version->new( '5.008007_01' ); print $version->normal, "\n";
prints
v5.8.7_001
my $version = Perl::Version->new( '5.8.7_1' ); print $version->normal, "\n";
prints
5.008007_001
my $version = Perl::Version->new( '5.008007_01' ); $version->inc_alpha; print $version->stringify, "\n";
prints
5.008007_02
and
my $version = Perl::Version->new( '5.8.7_1' ); $version->inc_alpha; print $version->stringify, "\n";
prints
5.8.7_2
my $v1 = Perl::Version->new( '1.002001' ); my $v2 = Perl::Version->new( '1.1.3' ); print ($v1->vcmp( $v2 ) > 0 ? 'yes' : 'no'), "\n";
prints
yes
my $v1 = Perl::Version->new( '1.002001' ); my $v2 = Perl::Version->new( '1.1.3' ); print "OK!\n" if $v1 > $v2;
prints
OK!
my $version = 'v1.2.3.4_5'; my ($prefix, $main, $suffix) = ($version =~ Perl::Version::REGEX); print "$prefix\n$main\n$suffix\n";
prints
v 1.2.3.4 _5
my $version = ' v1.2.3.4_5 '; my ($before, $prefix, $main, $suffix, $after) = ($version =~ Perl::Version::MATCH); print "|$before|$prefix|$main|$suffix|$after|\n";
prints
| |v|1.2.3.4|_5| |
qr/ ( (?i: Revision: \s+ ) | v | ) ( \d+ (?: [.] \d+)* ) ( (?: _ \d+ )? ) /x;
$version_object->new( '1.2.3' );
or
Perl::Version->new( '1.2.3' );
instead of
Perl::Version::new( '1.2.3' );
# Fails my $version = Perl::Version->new( '1.4' ); $version->increment( 2 );
Slightly confusingly you'll see this message even if you specified the component number implicitly by using one of the named convenience accessors.
Perl::Version requires no configuration files or environment variables.
No non-core modules.
None reported.
No bugs have been reported.
Please report any bugs or feature requests to "bug-perl-version@rt.cpan.org", or through the web interface at <http://rt.cpan.org>.
Andy Armstrong "<andy@hexten.net>"
Hans Dieter Pearcey "<hdp@cpan.org>"
Copyright (c) 2007, Andy Armstrong "<andy@hexten.net>". All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
2018-05-06 | perl v5.26.2 |