File Coverage

blib/lib/Perl/Version.pm
Criterion Covered Total %
statement 195 195 100.0
branch 59 60 98.3
condition 25 29 86.2
subroutine 27 27 100.0
pod 10 10 100.0
total 316 321 98.4


line stmt bran cond sub pod time code
1 86     86   544541 use utf8;
  86         2366  
  86         456  
2 86     86   3744 use v5.10;
  86         342  
3              
4             package Perl::Version;
5              
6 86     86   551 use warnings;
  86         164  
  86         5113  
7 86     86   538 use strict;
  86         261  
  86         2681  
8 86     86   475 use Carp;
  86         186  
  86         9042  
9 86     86   593 use Scalar::Util qw( blessed );
  86         162  
  86         14320  
10              
11             our $VERSION = '1.019';
12              
13             use overload (
14 86         798 '""' => \&stringify,
15             '<=>' => \&vcmp,
16             'cmp' => \&vcmp,
17 86     86   57692 );
  86         170205  
18              
19 86         12913 use constant REGEX => qr/ ( (?i: Revision: \s+ ) | v | )
20             ( \d+ (?: [.] \d+)* )
21 86     86   18593 ( (?: _ \d+ )? ) /x;
  86         162  
22              
23 86     86   555 use constant MATCH => qr/ ^ ( \s* ) @{[ REGEX ]} ( \s* ) $ /x;
  86         167  
  86         161  
  86         26410  
24              
25             my %NORMAL_FORMAT = (
26             prefix => 'v',
27             printf => ['%d'],
28             extend => '.%d',
29             alpha => '_%02d',
30             suffix => '',
31             fields => 3,
32             );
33              
34             my %NUMERIC_FORMAT = (
35             prefix => '',
36             printf => [ '%d', '.%03d' ],
37             extend => '%03d',
38             alpha => '_%02d',
39             suffix => '',
40             fields => 2,
41             );
42              
43             my %COMPONENT_NAME;
44              
45             BEGIN {
46 86     86   631 %COMPONENT_NAME = (
47             revision => 0,
48             version => 1,
49             subversion => 2
50             );
51              
52             # Make accessors
53 86         394 my @fields = ( keys %COMPONENT_NAME, qw( alpha ) );
54              
55 86     86   656 no strict 'refs';
  86         527  
  86         14565  
56              
57 86         268 for my $field ( @fields ) {
58             *$field = sub {
59 164     164   69650 my $self = shift;
60 164         520 return $self->component( $field, @_ );
61 344         1864 };
62              
63 344         666 my $inc_func = "inc_$field";
64             *$inc_func = sub {
65 15     15   29 my $self = shift;
66 15         51 return $self->increment( $field );
67 344         217489 };
68             }
69             }
70              
71             sub new {
72 428     428 1 1996567 my $class = shift;
73 428   66     2570 my $self
74             = bless {}, ref $class
75             || $class
76             || croak "new must be called as a class or object method";
77              
78 427         3722 $self->{version} = [0];
79              
80 427 100       1763 $self->_parse( @_ ) if @_;
81              
82 426         1407 return $self;
83             }
84              
85             sub _resolve_component_name {
86 349     349   449 my $self = shift;
87 349         502 my $name = shift;
88              
89 349 100       1398 if ( $name =~ /^-?\d+$/ ) {
90             # Allow negative subscripts
91 308 100       603 $name += $self->components if $name < 0;
92 308         608 return $name;
93             }
94              
95             croak "Unknown component name: $name"
96 41 100       476 unless exists $COMPONENT_NAME{ lc( $name ) };
97              
98 39         96 return $COMPONENT_NAME{ lc( $name ) };
99             }
100              
101             sub _guess_num_format {
102 1171     1171   1580 my $self = shift;
103 1171         1596 my $num = shift;
104              
105 1171 100       2515 if ( $num =~ m{ ^ 0 \d }x ) {
106 247         818 return '%0' . length( $num ) . 'd';
107             }
108              
109 924         2972 return '%d';
110             }
111              
112             sub _parse {
113 425     425   636 my $self = shift;
114              
115             # Check for vstring before anything else happens
116 425 100 66     2405 if ( $] >= 5.008_001 && Scalar::Util::isvstring $_[0] ) {
117 3         31 $self->{format} = {%NORMAL_FORMAT};
118 3         15 my @parts = map { ord } split //, shift;
  9         23  
119 3         10 $self->{version} = \@parts;
120 3         7 return;
121             }
122              
123 422         870 my $version = join( ' ', map { "$_" } @_ );
  422         2555  
124              
125 422 100       6487 croak "Illegal version string: $version"
126             unless $version =~ MATCH;
127              
128 421         1087 my $format = { fields => 1 };
129 421         2109 my ( $pad, $pfx, $ver, $alp, $sfx ) = ( $1, $2, $3, $4, $5 );
130              
131 421         1458 $self->{original}{args} = [ @_ ];
132 421         852 $self->{original}{version} = $version;
133 421         995 $self->{original}{pad} = $pad;
134 421         891 $self->{original}{pfx} = $pfx;
135 421         843 $self->{original}{ver} = $ver;
136 421         880 $self->{original}{alp} = $alp;
137 421         1133 $self->{original}{sfx} = $sfx;
138              
139             # Decode version into format
140 421         961 $format->{prefix} = $pad . $pfx;
141 421         791 $format->{suffix} = $sfx;
142              
143 421         1359 my @parts = split( /[.]/, $ver );
144 421         1171 my @ver = ( shift( @parts ) + 0 );
145              
146 421         1253 my @fmt = ( $self->_guess_num_format( $ver[0] ) );
147              
148 421 100 100     1575 if ( @parts == 1 && length( $parts[0] ) >= 3 ) {
149              
150 98         181 my $threes = pop @parts;
151 98         578 my @cluster = ( $threes =~ /(\d{1,3})/g );
152              
153             # warn "# $threes <", join( '>, <', @cluster ), ">\n";
154 98         195 push @fmt, map { $self->_guess_num_format( $_ ) } @cluster;
  169         329  
155 98         206 $fmt[1] = '.' . $fmt[1];
156 98         208 $format->{extend} = '%03d';
157              
158 98         160 push @parts, map { 0 + $_ } @cluster;
  169         434  
159             }
160             else {
161              
162             # Parts with leading zeros
163 323         619 my @lz = grep { m{ ^ 0 \d }x } @parts;
  508         1121  
164              
165             # Work out how many different lengths we have
166 323         557 my %le = map { length( $_ ) => 1 } @parts;
  508         1429  
167              
168 323 100 100     929 if ( @lz && keys %le == 1 ) {
169 18         44 push @fmt,
170             ( '.' . $self->_guess_num_format( shift @lz ) ) x @parts;
171             }
172             else {
173 305         559 push @fmt, map { '.' . $self->_guess_num_format( $_ ) } @parts;
  475         953  
174             }
175              
176 323 100       1151 $format->{extend} = ( @parts ? '' : '.' ) . $fmt[-1];
177             }
178              
179 421         882 $format->{printf} = \@fmt;
180              
181 421 100       853 if ( length( $alp ) ) {
182 88 50       365 die "Badly formatted alpha got through"
183             unless $alp =~ m{ _ (\d+) }x;
184              
185 88         182 my $alpha = $1;
186              
187 88         202 $self->{alpha} = $alpha + 0;
188 88         200 $format->{alpha} = '_' . $self->_guess_num_format( $alpha );
189             }
190             else {
191 333         883 $format->{alpha} = $NORMAL_FORMAT{alpha};
192             }
193              
194 421         817 $self->{format} = $format;
195              
196 421         695 push @ver, map { $_ + 0 } @parts;
  677         1384  
197              
198 421         987 $self->{version} = \@ver;
199              
200 421         1007 return;
201             }
202              
203             sub _format {
204 596     596   920 my $self = shift;
205 596         849 my $format = shift;
206              
207 596         999 my @result = ();
208              
209 596         799 my @parts = @{ $self->{version} };
  596         2301  
210 596         925 my @fmt = @{ $format->{printf} };
  596         1603  
211              
212 596         1719 push @parts, 0 while @parts < $format->{fields};
213              
214             # Adjust the format to be the same length as the number of fields
215 596         1402 pop @fmt while @fmt > @parts;
216 596         1734 push @fmt, $format->{extend} while @parts > @fmt;
217              
218             my $version
219             = ( $format->{prefix} )
220             . sprintf( join( '', @fmt ), @parts )
221 596         2638 . ( $format->{suffix} );
222              
223             $version .= sprintf( $format->{alpha}, $self->{alpha} )
224 596 100       1627 if defined $self->{alpha};
225              
226 596         1320 push @result, $version;
227              
228 596         3958 return join( ' ', @result );
229             }
230              
231             sub stringify {
232 386     386 1 183497 my $self = shift;
233 386   100     1598 return $self->_format( $self->{format} || \%NORMAL_FORMAT );
234             }
235              
236             sub normal {
237 136     136 1 13573 return shift->_format( \%NORMAL_FORMAT );
238             }
239              
240             sub numify {
241 74     74 1 3808 return shift->_format( \%NUMERIC_FORMAT );
242             }
243              
244             sub is_alpha {
245 13     13 1 1708 my $self = shift;
246 13         60 return exists $self->{alpha};
247             }
248              
249             sub vcmp {
250 426     426 1 7062 my ( $self, $other, $rev ) = @_;
251              
252             # Promote to object
253 426 100       1022 $other = __PACKAGE__->new( $other ) unless ref $other;
254              
255 426 100 100     2789 croak "Can't compare with $other"
256             unless blessed $other && $other->isa( __PACKAGE__ );
257              
258 424 100       961 return $other->vcmp( $self, 0 ) if $rev;
259              
260 421         620 my @this = @{ $self->{version} };
  421         1026  
261 421         632 my @that = @{ $other->{version} };
  421         845  
262              
263 421         1135 push @this, 0 while @this < @that;
264 421         1024 push @that, 0 while @that < @this;
265              
266 421         923 while ( @this ) {
267 1166 100       3330 if ( my $cmp = ( shift( @this ) <=> shift( @that ) ) ) {
268 56         361 return $cmp;
269             }
270             }
271              
272 365   100     2569 return ( $self->{alpha} || 0 ) <=> ( $other->{alpha} || 0 );
      100        
273             }
274              
275             sub components {
276 482     482 1 1682 my $self = shift;
277              
278 482 100       840 if ( @_ ) {
279 30         59 my $fields = shift;
280              
281 30 100       176 if ( ref $fields eq 'ARRAY' ) {
282 24         117 $self->{version} = [@$fields];
283             }
284             else {
285 6 100       265 croak "Can't set the number of components to 0"
286             unless $fields;
287              
288             # Adjust the number of fields
289 5         11 pop @{ $self->{version} }, while @{ $self->{version} } > $fields;
  6         26  
  1         3  
290 4         13 push @{ $self->{version} }, 0,
291 5         12 while @{ $self->{version} } < $fields;
  9         46  
292             }
293             }
294             else {
295 452         592 return @{ $self->{version} };
  452         1216  
296             }
297             }
298              
299             sub component {
300 463     463 1 10168 my $self = shift;
301 463         640 my $field = shift;
302              
303 463 100       1015 defined $field or croak "You must specify a component number";
304              
305 462 100       1102 if ( lc( $field ) eq 'alpha' ) {
306 151 100       335 if ( @_ ) {
307 67         158 my $alpha = shift;
308 67 100       218 if ( $alpha ) {
309 9         35 $self->{alpha} = $alpha;
310             }
311             else {
312 58         213 delete $self->{alpha};
313             }
314             }
315             else {
316 84   100     550 return $self->{alpha} || 0;
317             }
318             }
319             else {
320 311         881 $field = $self->_resolve_component_name( $field );
321 310         652 my $fields = $self->components;
322              
323 310 100       561 if ( @_ ) {
324 71 100       154 if ( $field >= $fields ) {
325              
326             # Extend array if necessary
327 2         8 $self->components( $field + 1 );
328             }
329              
330 71         172 $self->{version}->[$field] = shift;
331             }
332             else {
333 239 100 66     992 return unless $field >= 0 && $field < $fields;
334 181         597 return $self->{version}->[$field];
335             }
336             }
337             }
338              
339             sub increment {
340 44     44 1 841 my $self = shift;
341 44         68 my $field = shift;
342 44         151 my $fields = $self->components;
343              
344 44 100       164 if ( lc( $field ) eq 'alpha' ) {
345 6         16 $self->alpha( $self->alpha + 1 );
346             }
347             else {
348 38         132 $field = $self->_resolve_component_name( $field );
349              
350 37 100 66     329 croak "Component $field is out of range 0..", $fields - 1
351             if $field < 0 || $field >= $fields;
352              
353             # Increment the field
354 36         162 $self->component( $field, $self->component( $field ) + 1 );
355              
356             # Zero out any following fields
357 36         108 while ( ++$field < $fields ) {
358 28         48 $self->component( $field, 0 );
359             }
360 36         89 $self->alpha( 0 );
361             }
362             }
363              
364             sub set {
365 23     23 1 48 my $self = shift;
366 23         44 my $other = shift;
367              
368 23 100       105 $other = $self->new( $other ) unless ref $other;
369              
370 23         101 my @comp = $other->components;
371 23         100 $self->components( \@comp );
372 23         104 $self->alpha( $other->alpha );
373 23         105 $self->{format}{alpha} = $other->{format}{alpha};
374             }
375              
376             1;
377             __END__
378              
379             =encoding utf8
380              
381             =head1 NAME
382              
383             Perl::Version - Parse and manipulate Perl version strings
384              
385             =head1 VERSION
386              
387             This document describes Perl::Version version 1.019
388              
389             =head1 SYNOPSIS
390              
391             use Perl::Version;
392              
393             # Init from string
394             my $version = Perl::Version->new( '1.2.3' );
395              
396             # Stringification preserves original format
397             print "$version\n"; # prints '1.2.3'
398              
399             # Normalised
400             print $version->normal, "\n"; # prints 'v1.2.3'
401              
402             # Numified
403             print $version->numify, "\n"; # prints '1.002003'
404              
405             # Explicitly stringified
406             print $version->stringify, "\n"; # prints '1.2.3'
407              
408             # Increment the subversion (the third component)
409             $version->inc_subversion;
410              
411             # Stringification returns the updated version formatted
412             # as the original was
413             print "$version\n"; # prints '1.2.4'
414              
415             # Normalised
416             print $version->normal, "\n"; # prints 'v1.2.4'
417              
418             # Numified
419             print $version->numify, "\n"; # prints '1.002004'
420              
421             # Refer to subversion component by position ( zero based )
422             $version->increment( 2 );
423              
424             print "$version\n"; # prints '1.2.5'
425              
426             # Increment the version (second component) which sets all
427             # components to the right of it to zero.
428             $version->inc_version;
429              
430             print "$version\n"; # prints '1.3.0'
431              
432             # Increment the revision (main version number)
433             $version->inc_revision;
434              
435             print "$version\n"; # prints '2.0.0'
436              
437             # Increment the alpha number
438             $version->inc_alpha;
439              
440             print "$version\n"; # prints '2.0.0_001'
441              
442             =head1 DESCRIPTION
443              
444             Perl::Version provides a simple interface for parsing, manipulating and
445             formatting Perl version strings.
446              
447             Unlike version.pm (which concentrates on parsing and comparing version
448             strings) Perl::Version is designed for cases where you'd like to
449             parse a version, modify it and get back the modified version formatted
450             like the original.
451              
452             For example:
453              
454             my $version = Perl::Version->new( '1.2.3' );
455             $version->inc_version;
456             print "$version\n";
457              
458             prints
459              
460             1.3.0
461              
462             whereas
463              
464             my $version = Perl::Version->new( 'v1.02.03' );
465             $version->inc_version;
466             print "$version\n";
467              
468             prints
469              
470             v1.03.00
471              
472             Both are representations of the same version and they'd compare equal
473             but their formatting is different.
474              
475             Perl::Version tries hard to guess and recreate the format of the
476             original version and in most cases it succeeds. In rare cases the
477             formatting is ambiguous. Consider
478              
479             1.10.03
480              
481             Do you suppose that second component '10' is zero padded like the third
482             component? Perl::Version will assume that it is:
483              
484             my $version = Perl::Version->new( '1.10.03' );
485             $version->inc_revision;
486             print "$version\n";
487              
488             will print
489              
490             2.00.00
491              
492             If all of the components after the first are the same length (two
493             characters in this case) and any of them begins with a zero
494             Perl::Version will assume that they're all zero padded to the
495             same length.
496              
497             The first component and any alpha suffix are handled separately. In each
498             case if either of them starts with a zero they will be zero padded to
499             the same length when stringifying the version.
500              
501             =head2 Version Formats
502              
503             Perl::Version supports a few different version string formats.
504              
505             =over
506              
507             =item Z<> 1, 1.2
508              
509             Versions that look like a number. If you pass a numeric value its string
510             equivalent will be parsed:
511              
512             my $version = Perl::Version->new( 1.2 );
513             print "$version\n";
514              
515             prints
516              
517             1.2
518              
519             In fact there is no special treatment for versions that resemble decimal
520             numbers. This is worthy of comment only because it differs from
521             version.pm which treats actual numbers used as versions as a special
522             case and performs various transformations on the stored version.
523              
524             =item Z<> 1.2.3, 1.2.3.4
525              
526             Simple versions with three or more components.
527              
528             =item Z<> v1.2.3
529              
530             Versions with a leading 'v'.
531              
532             =item Z<> 5.8, 5.08, 5.008006
533              
534             Fielded numeric versions. If a version string has a single decimal
535             point, it extracts digits in groups of three after the decimal point.
536             If there are fewer than three digits in the final group, the field is
537             left-padded with zeros to make it three digits (for example, 5.8
538             becomes 5.008 and 5.08 becomes 5.008 too). This is opposite of how
539             Perl and CPAN has historically treated versions by right padding
540             groups of three on.
541              
542             For example
543              
544             my $version = Perl::Version->new( 1.002003004005006 );
545             print $version->normal;
546              
547             prints
548              
549             v1.2.3.4.5.6
550              
551             =item vstring
552              
553             Perls later than 5.8.1 support vstring format. A vstring looks like a
554             number with more than one decimal point and (optionally) a leading
555             'v'. The 'v' is mandatory for vstrings containing fewer than two
556             decimal points.
557              
558             Perl::Version will successfully parse vstrings
559              
560             my $version = Perl::Version->new( v1.2 );
561             print "$version\n";
562              
563             prints
564              
565             v1.2
566              
567             Note that stringifying a Perl::Version constructed from a vstring will
568             result in a regular string. Because it has no way of knowing whether the
569             vstring constant had a 'v' prefix it always generates one when
570             stringifying back to a version string.
571              
572             =item CVS version
573              
574             A common idiom for users of CVS is to use keyword replacement to
575             generate a version automatically like this:
576              
577             $VERSION = version->new( qw$Revision: 2.7 $ );
578              
579             Perl::Version does the right thing with such versions so that
580              
581             my $version = Perl::Version->new( qw$Revision: 2.7 $ );
582             $version->inc_revision;
583             print "$version\n";
584              
585             prints
586              
587             Revision: 3.0
588              
589             =back
590              
591             =head3 Real Numbers
592              
593             Real numbers are stringified before parsing. This has two implications:
594             trailing zeros after the decimal point will be lost and any underscore
595             characters in the number are discarded.
596              
597             Perl allows underscores anywhere in numeric constants as an aid to
598             formatting. These are discarded when Perl converts the number into its
599             internal format. This means that
600              
601             # Numeric version
602             print Perl::Version->new( 1.001_001 )->stringify;
603              
604             prints
605              
606             1.001001
607              
608             but
609              
610             # String version
611             print Perl::Version->new( '1.001_001' )->stringify;
612              
613             prints
614              
615             1.001_001
616              
617             as expected.
618              
619             In general you should probably avoid versions expressed either as
620             decimal numbers or vstrings. The safest option is to pass a regular
621             string to Perl::Version->new().
622              
623             =head3 Alpha Versions
624              
625             By convention if a version string has suffix that consists of an
626             underscore followed by one or more digits it represents an alpha or
627             developer release. CPAN treats modules with such version strings
628             specially to reflect their alpha status.
629              
630             This alpha notation is one reason why using decimal numbers as versions
631             is a bad idea. Underscore is a valid character in numeric constants
632             which is discarded by Perl when a program's source is parsed so any
633             intended alpha suffix will become part of the version number.
634              
635             To be considered alpha a version must have a non-zero alpha
636             component like this
637              
638             3.0.4_001
639              
640             Generally the alpha component will be formatted with leading zeros but
641             this is not a requirement.
642              
643             =head2 Component Naming
644              
645             A version number consists of a series of components. By Perl convention
646             the first three components are named 'revision', 'version' and
647             'subversion':
648              
649             $ perl -V
650             Summary of my perl5 (revision 5 version 8 subversion 6) configuration:
651              
652             (etc)
653              
654             Perl::Version follows that convention. Any component may be accessed by
655             passing a number from 0 to N-1 to the L<component> or L<increment> but for
656             convenience the first three components are aliased as L<revision>,
657             L<version> and L<subversion>.
658              
659             $version->increment( 0 );
660              
661             is the same as
662              
663             $version->inc_revision;
664              
665             and
666              
667             my $subv = $version->subversion;
668              
669             is the same as
670              
671             my $subv = $version->component( 2 );
672              
673             The alpha component is named 'alpha'.
674              
675             =head2 Comparison with version.pm
676              
677             If you're familiar with version.pm you'll notice that there's a certain
678             amount of overlap between what it does and this module. I originally
679             created this module as a mutable subclass of version.pm but the
680             requirement to be able to reformat a modified version to match the
681             formatting of the original didn't sit well with version.pm's internals.
682              
683             As a result this module is not dependent or based on version.pm.
684              
685             =head1 INTERFACE
686              
687             =over
688              
689             =item C<< new >>
690              
691             Create a new Perl::Version by parsing a version string. As discussed
692             above a number of different version formats are supported. Along with
693             the value of the version formatting information is captured so that the
694             version can be modified and the updated value retrieved in the same
695             format as the original.
696              
697             my @version = (
698             '1.3.0', 'v1.03.00', '1.10.03', '2.00.00',
699             '1.2', 'v1.2.3.4.5.6', 'v1.2', 'Revision: 3.0',
700             '1.001001', '1.001_001', '3.0.4_001',
701             );
702              
703             for my $v ( @version ) {
704             my $version = Perl::Version->new( $v );
705             $version->inc_version;
706             print "$version\n";
707             }
708              
709             prints
710              
711             1.4.0
712             v1.04.00
713             1.11.00
714             2.01.00
715             1.3
716             v1.3.0.0.0.0
717             v1.3
718             Revision: 3.1
719             1.002000
720             1.002
721             3.1.0
722              
723             In each case the incremented version is formatted in the same way as the original.
724              
725             If no arguments are passed an empty version intialised to 'v0' will be
726             constructed.
727              
728             In order to support CVS version syntax
729              
730             my $version = Perl::Version->new( qw$Revision: 2.7 $ );
731              
732             C<new> may be passed an array in which case it concatenates all of its
733             arguments with spaces before parsing the result.
734              
735             If the string can't be parsed as a version C<new> will croak with a
736             suitable error. See L<DIAGNOSTICS> for more information.
737              
738             =back
739              
740             =head2 Accessors
741              
742             =over
743              
744             =item C<< component >>
745              
746             Set or get one of the components of a version.
747              
748             # Set the subversion
749             $version->component( 2, 17 );
750              
751             # Get the revision
752             my $rev = $version->component( 0 );
753              
754             Instead of a component number you may pass a name: 'revision',
755             'version', 'subversion' or 'alpha':
756              
757             my $rev = $version->component( 'revision' );
758              
759             =item C<< components >>
760              
761             Get or set all of the components of a version.
762              
763             # Set the number of components
764             $version->components( 4 );
765              
766             # Get the number of components
767             my $parts = $version->components;
768              
769             # Get the individual components as an array
770             my @parts = $version->components;
771              
772             # Set the components from an array
773             $version->components( [ 5, 9, 2 ] );
774              
775             Hmm. That's a lot of interface for one subroutine. Sorry about that.
776              
777             =item C<< revision >>
778              
779             Alias for C<< component( 0 ) >>. Gets or sets the revision component.
780              
781             =item C<< version >>
782              
783             Alias for C<< component( 1 ) >>. Gets or sets the version component.
784              
785             =item C<< subversion >>
786              
787             Alias for C<< component( 2 ) >>. Gets or sets the subversion component.
788              
789             =item C<< alpha >>
790              
791             Get or set the alpha component of a version. Returns 0 for versions with no alpha.
792              
793             # Set alpha
794             $version->alpha( 12 );
795              
796             # Get alpha
797             my $alp = $version->alpha;
798              
799             =item C<< is_alpha >>
800              
801             Return true if a version has a non-zero alpha component.
802              
803             =item C<< set >>
804              
805             Set the version to match another version preserving the formatting of this version.
806              
807             $version->set( $other_version );
808              
809             You may also set the version from a literal string:
810              
811             $version->set( '1.2.3' );
812              
813             The version will be updated to the value of the version string but will
814             retain its current formatting.
815              
816             =back
817              
818             =head2 Incrementing
819              
820             =over
821              
822             =item C<< increment >>
823              
824             Increment a component of a version.
825              
826             my $version = Perl::Version->new( '3.1.4' );
827             $version->increment( 1 );
828             print "$version\n";
829              
830             prints
831              
832             3.2.0
833              
834             Components to the right of the incremented component will be set to zero
835             as will any alpha component.
836              
837             As an alternative to passing a component number one of the predefined
838             component names 'revision', 'version', 'subversion' or 'alpha' may be
839             passed.
840              
841             =item C<< inc_alpha >>
842              
843             Increment a version's alpha component.
844              
845             =item C<< inc_revision >>
846              
847             Increment a version's revision component.
848              
849             =item C<< inc_subversion >>
850              
851             Increment a version's subversion component.
852              
853             =item C<< inc_version >>
854              
855             Increment a version's version component.
856              
857             =back
858              
859             =head2 Formatting
860              
861             =over
862              
863             =item C<< normal >>
864              
865             Return a normalised representation of a version.
866              
867             my $version = Perl::Version->new( '5.008007_01' );
868             print $version->normal, "\n";
869              
870             prints
871              
872             v5.8.7_01
873              
874             =item C<< numify >>
875              
876             Return a numeric representation of a version. The numeric form is most
877             frequently used for versions of Perl itself.
878              
879             my $version = Perl::Version->new( '5.8.7_1' );
880             print $version->numify, "\n";
881              
882             prints
883              
884             5.008007_001
885              
886             =item C<< stringify >>
887              
888             Return the version formatted as closely as possible to the version from
889             which it was initialised.
890              
891             my $version = Perl::Version->new( '5.008007_01' );
892             $version->inc_alpha;
893             print $version->stringify, "\n";
894              
895             prints
896              
897             5.008007_02
898              
899             and
900              
901             my $version = Perl::Version->new( '5.8.7_1' );
902             $version->inc_alpha;
903             print $version->stringify, "\n";
904              
905             prints
906              
907             5.8.7_2
908              
909             =back
910              
911             =head2 Comparison
912              
913             =over
914              
915             =item C<< vcmp >>
916              
917             Perform 'spaceship' comparison between two version and return -1, 0 or 1
918             depending on their ordering. Comparisons are semantically correct so that
919              
920             my $v1 = Perl::Version->new( '1.002001' );
921             my $v2 = Perl::Version->new( '1.1.3' );
922              
923             print ($v1->vcmp( $v2 ) > 0 ? 'yes' : 'no'), "\n";
924              
925             prints
926              
927             yes
928              
929             =back
930              
931             =head2 Overloaded Operators
932              
933             =over
934              
935             =item C<< <=> >> and C<< cmp >>
936              
937             The C<< <=> >> and C<< cmp >> operators are overloaded (by the L<vcmp>
938             method) so that comparisons between versions work as expected. This
939             means that the other numeric and string comparison operators also work
940             as expected.
941              
942             my $v1 = Perl::Version->new( '1.002001' );
943             my $v2 = Perl::Version->new( '1.1.3' );
944              
945             print "OK!\n" if $v1 > $v2;
946              
947             prints
948              
949             OK!
950              
951             =item C<< "" >> (stringification)
952              
953             Perl::Version objects are converted to strings by calling the
954             L<stringify> method. This usually results in formatting close to that
955             of the original version string.
956              
957             =back
958              
959             =head2 Constants
960              
961             =over
962              
963             =item C<< REGEX >>
964              
965             An unanchored regular expression that matches any of the version formats
966             supported by Perl::Version. Three captures get the prefix part, the main
967             body of the version and any alpha suffix respectively.
968              
969             my $version = 'v1.2.3.4_5';
970             my ($prefix, $main, $suffix) = ($version =~ Perl::Version::REGEX);
971             print "$prefix\n$main\n$suffix\n";
972              
973             prints
974              
975             v
976             1.2.3.4
977             _5
978              
979             =item C<< MATCH >>
980              
981             An anchored regular expression that matches a correctly formatted
982             version string. Five captures get any leading whitespace, the prefix
983             part, the main body of the version, any alpha suffix and any
984             trailing spaces respectively.
985              
986             my $version = ' v1.2.3.4_5 ';
987             my ($before, $prefix, $main, $suffix, $after)
988             = ($version =~ Perl::Version::MATCH);
989             print "|$before|$prefix|$main|$suffix|$after|\n";
990              
991             prints
992              
993             | |v|1.2.3.4|_5| |
994              
995             =back
996              
997             =head1 DIAGNOSTICS
998              
999             =head2 Error messages
1000              
1001             =over
1002              
1003             =item C<< Illegal version string: %s >>
1004              
1005             The version string supplied to C<new> can't be parsed as a valid
1006             version. Valid versions match this regex:
1007              
1008             qr/ ( (?i: Revision: \s+ ) | v | )
1009             ( \d+ (?: [.] \d+)* )
1010             ( (?: _ \d+ )? ) /x;
1011              
1012             =item C<< new must be called as a class or object method >>
1013              
1014             C<new> can't be called as a normal subroutine. Use
1015              
1016             $version_object->new( '1.2.3' );
1017              
1018             or
1019              
1020             Perl::Version->new( '1.2.3' );
1021              
1022             instead of
1023              
1024             Perl::Version::new( '1.2.3' );
1025              
1026             =item C<< Unknown component name: %s >>
1027              
1028             You've attempted to access a component by name using a name that isn't
1029             recognised. Valid component names are 'revision', 'version', 'subversion'
1030             and 'alpha'. Case is not significant.
1031              
1032             =item C<< Can't compare with %s >>
1033              
1034             You've tried to compare a Perl::Version with something other than a
1035             version string, a number or another Perl::Version.
1036              
1037             =item C<< Can't set the number of components to 0 >>
1038              
1039             Versions must have at least one component.
1040              
1041             =item C<< You must specify a component number >>
1042              
1043             You've called L<component> or L<increment> without specifying the number (or
1044             name) of the component to access.
1045              
1046             =item C<< Component %s is out of range 0..%s >>
1047              
1048             You've attempted to increment a component of a version but you've
1049             specified a component that doesn't exist within the version:
1050              
1051             # Fails
1052             my $version = Perl::Version->new( '1.4' );
1053             $version->increment( 2 );
1054              
1055             Slightly confusingly you'll see this message even if you specified the
1056             component number implicitly by using one of the named convenience
1057             accessors.
1058              
1059             =back
1060              
1061             =head1 CONFIGURATION AND ENVIRONMENT
1062              
1063             Perl::Version requires no configuration files or environment variables.
1064              
1065             =head1 DEPENDENCIES
1066              
1067             No non-core modules.
1068              
1069             =head1 INCOMPATIBILITIES
1070              
1071             None reported.
1072              
1073             =head1 BUGS AND LIMITATIONS
1074              
1075             Please report any bugs or feature requests to the GitHub issues queue:
1076              
1077             https://github.com/briandfoy/perl-version/issues
1078              
1079             =head1 AUTHOR
1080              
1081             This module is currently maintained by brian d foy C<< <briandfoy@pobox.com> >>.
1082              
1083             Andy Armstrong C<< <andy@hexten.net> >>
1084              
1085             Hans Dieter Pearcey C<< <hdp@cpan.org> >>
1086              
1087             =head1 LICENCE AND COPYRIGHT
1088              
1089             Copyright © 2007-2021, Andy Armstrong C<< <andy@hexten.net> >>. All rights reserved.
1090              
1091             This module is free software; you can redistribute it and/or
1092             modify it under the same terms as Perl itself. See L<perlartistic>.
1093              
1094             =head1 DISCLAIMER OF WARRANTY
1095              
1096             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
1097             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
1098             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
1099             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
1100             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1101             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
1102             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
1103             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
1104             NECESSARY SERVICING, REPAIR, OR CORRECTION.
1105              
1106             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
1107             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
1108             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
1109             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
1110             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
1111             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
1112             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
1113             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
1114             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
1115             SUCH DAMAGES.