1 package Module::Build::Version;
7 eval "use version $VERSION";
8 if ($@) { # can't locate version files, use our own
10 # Avoid redefined warnings if an old version.pm was available
11 delete $version::{$_} foreach keys %version::;
13 # first we get the stub version module
16 s/(\$VERSION)\s=\s\d+/\$VERSION = 0/;
21 # and now get the current version::vpp code
24 s/(\$VERSION)\s=\s\d+/\$VERSION = 0/;
29 # but we eval them in reverse order since version depends on
30 # version::vpp to already exist
31 eval $vpp; die $@ if $@;
32 $INC{'version/vpp.pm'} = 'inside Module::Build::Version';
33 eval $version; die $@ if $@;
34 $INC{'version.pm'} = 'inside Module::Build::Version';
37 # now we can safely subclass version, installed or not
43 # stub version module to make everything else happy
49 use vars qw(@ISA $VERSION $CLASS *qv);
55 push @ISA, "version::vpp";
56 *version::qv = \&version::vpp::qv;
58 # Preloaded methods go here.
61 my $callpkg = caller();
65 sub {return bless version::qv(shift), $class }
66 unless defined(&{"$callpkg\::qv"});
71 # replace everything from here to the end with the current version/vpp.pm
76 use vars qw ($VERSION @ISA @REGEXS);
80 ^v? # optional leading 'v'
81 (\d*) # major revision not required
82 \. # requires at least one decimal
92 'nomethod' => \&vnoop,
95 my $VERSION_MAX = 0x7FFFFFFF;
101 sub enabled {return $^W;}
108 my ($class, $value) = @_;
109 my $self = bless ({}, ref ($class) || $class);
111 if ( ref($value) && eval("$value->isa('version')") ) {
112 # Can copy the elements directly
113 $self->{version} = [ @{$value->{version} } ];
114 $self->{qv} = 1 if $value->{qv};
115 $self->{alpha} = 1 if $value->{alpha};
116 $self->{original} = ''.$value->{original};
121 my $currlocale = POSIX::setlocale(&POSIX::LC_ALL);
122 my $radix_comma = ( POSIX::localeconv()->{decimal_point} eq ',' );
124 if ( not defined $value or $value =~ /^undef$/ ) {
125 # RT #19517 - special case for undef comparison
126 # or someone forgot to pass a value
127 push @{$self->{version}}, 0;
128 $self->{original} = "0";
132 if ( $#_ == 2 ) { # must be CVS-style
136 $value = _un_vstring($value);
138 # exponential notation
139 if ( $value =~ /\d+.?\d*e-?\d+/ ) {
140 $value = sprintf("%.9f",$value);
144 # if the original locale used commas for decimal points, we
145 # just replace commas with decimal places, rather than changing
147 if ( $radix_comma ) {
151 # This is not very efficient, but it is morally equivalent
152 # to the XS code (as that is the reference implementation).
153 # See vutil/vutil.c for details
159 my ($start, $last, $pos, $s);
162 while ( substr($value,$s,1) =~ /\s/ ) { # leading whitespace is OK
166 if (substr($value,$s,1) eq 'v') {
168 $qv = 1; # force quoted version processing
171 $start = $last = $pos = $s;
173 # pre-scan the input string to check for decimals/underbars
174 while ( substr($value,$pos,1) =~ /[._\d]/ ) {
175 if ( substr($value,$pos,1) eq '.' ) {
177 Carp::croak("Invalid version format ".
178 "(underscores before decimal)");
183 elsif ( substr($value,$pos,1) eq '_' ) {
186 Carp::croak("Invalid version format ".
187 "(multiple underscores)");
190 $width = $pos - $last - 1; # natural width of sub-version
195 if ( $alpha && !$saw_period ) {
197 Carp::croak("Invalid version format ".
198 "(alpha without decimal)");
201 if ( $alpha && $saw_period && $width == 0 ) {
203 Carp::croak("Invalid version format ".
204 "(misplaced _ in number)");
207 if ( $saw_period > 1 ) {
208 $qv = 1; # force quoted version processing
222 if ( !$qv && $width < 3 ) {
223 $self->{width} = $width;
226 while ( substr($value,$pos,1) =~ /\d/ ) {
230 if ( substr($value,$pos,1) !~ /[a-z]/ ) { ### FIX THIS ###
237 # this is atoi() that delimits on underscores
242 # the following if() will only be true after the decimal
243 # point of a version originally created with a bare
244 # floating point number, i.e. not quoted in any way
245 if ( !$qv && $s > $start && $saw_period == 1 ) {
247 while ( $s < $end ) {
249 $rev += substr($value,$s,1) * $mult;
251 if ( abs($orev) > abs($rev)
252 || abs($rev) > abs($VERSION_MAX) ) {
253 if ( warnings::enabled("overflow") ) {
255 Carp::carp("Integer overflow in version");
261 if ( substr($value,$s,1) eq '_' ) {
267 while (--$end >= $s) {
269 $rev += substr($value,$end,1) * $mult;
271 if ( abs($orev) > abs($rev)
272 || abs($rev) > abs($VERSION_MAX) ) {
273 if ( warnings::enabled("overflow") ) {
275 Carp::carp("Integer overflow in version");
285 push @{$self->{version}}, $rev;
286 if ( substr($value,$pos,1) eq '.'
287 && substr($value,$pos+1,1) =~ /\d/ ) {
290 elsif ( substr($value,$pos,1) eq '_'
291 && substr($value,$pos+1,1) =~ /\d/ ) {
294 elsif ( substr($value,$pos,1) =~ /\d/ ) {
302 while ( substr($value,$pos,1) =~ /\d/ ) {
308 while (substr($value,$pos,1) =~ /[\d_]/ && $digits < 3) {
309 if ( substr($value,$pos,1) ne '_' ) {
317 if ( $qv ) { # quoted versions always get at least three terms
318 my $len = scalar @{$self->{version}};
321 push @{$self->{version}}, 0;
325 if ( substr($value,$pos) ) { # any remaining text
326 if ( warnings::enabled("misc") ) {
328 Carp::carp("Version string '$value' contains invalid data; ".
329 "ignoring: '".substr($value,$pos)."'");
333 # cache the original value for use when stringification
336 $self->{original} = 'v.Inf';
339 $self->{original} = substr($value,0,$pos);
348 unless (_verify($self)) {
350 Carp::croak("Invalid version object");
352 my $width = $self->{width} || 3;
353 my $alpha = $self->{alpha} || "";
354 my $len = $#{$self->{version}};
355 my $digit = $self->{version}[0];
356 my $string = sprintf("%d.", $digit );
358 for ( my $i = 1 ; $i < $len ; $i++ ) {
359 $digit = $self->{version}[$i];
361 my $denom = 10**(3-$width);
362 my $quot = int($digit/$denom);
363 my $rem = $digit - ($quot * $denom);
364 $string .= sprintf("%0".$width."d_%d", $quot, $rem);
367 $string .= sprintf("%03d", $digit);
372 $digit = $self->{version}[$len];
373 if ( $alpha && $width == 3 ) {
376 $string .= sprintf("%0".$width."d", $digit);
380 $string .= sprintf("000");
389 unless (_verify($self)) {
391 Carp::croak("Invalid version object");
393 my $alpha = $self->{alpha} || "";
394 my $len = $#{$self->{version}};
395 my $digit = $self->{version}[0];
396 my $string = sprintf("v%d", $digit );
398 for ( my $i = 1 ; $i < $len ; $i++ ) {
399 $digit = $self->{version}[$i];
400 $string .= sprintf(".%d", $digit);
404 $digit = $self->{version}[$len];
406 $string .= sprintf("_%0d", $digit);
409 $string .= sprintf(".%0d", $digit);
414 for ( $len = 2 - $len; $len != 0; $len-- ) {
415 $string .= sprintf(".%0d", 0);
425 unless (_verify($self)) {
427 Carp::croak("Invalid version object");
429 return exists $self->{original}
439 my ($left,$right,$swap) = @_;
440 my $class = ref($left);
441 unless ( UNIVERSAL::isa($right, $class) ) {
442 $right = $class->new($right);
446 ($left, $right) = ($right, $left);
448 unless (_verify($left)) {
450 Carp::croak("Invalid version object");
452 unless (_verify($right)) {
454 Carp::croak("Invalid version object");
456 my $l = $#{$left->{version}};
457 my $r = $#{$right->{version}};
458 my $m = $l < $r ? $l : $r;
459 my $lalpha = $left->is_alpha;
460 my $ralpha = $right->is_alpha;
463 while ( $i <= $m && $retval == 0 ) {
464 $retval = $left->{version}[$i] <=> $right->{version}[$i];
468 # tiebreaker for alpha with identical terms
471 && $left->{version}[$m] == $right->{version}[$m]
472 && ( $lalpha || $ralpha ) ) {
474 if ( $lalpha && !$ralpha ) {
477 elsif ( $ralpha && !$lalpha) {
482 # possible match except for trailing 0's
483 if ( $retval == 0 && $l != $r ) {
485 while ( $i <= $r && $retval == 0 ) {
486 if ( $right->{version}[$i] != 0 ) {
487 $retval = -1; # not a match after all
493 while ( $i <= $l && $retval == 0 ) {
494 if ( $left->{version}[$i] != 0 ) {
495 $retval = +1; # not a match after all
507 return vcmp($self,$self->new("0"),1);
512 Carp::croak("operation not supported with version object");
517 return (exists $self->{alpha});
523 $value = _un_vstring($value);
524 $value = 'v'.$value unless $value =~ /(^v|\d+\.\d+\.\d)/;
525 my $version = version->new($value); # always use base class
531 return (exists $self->{qv});
538 && eval { exists $self->{version} }
539 && ref($self->{version}) eq 'ARRAY'
551 if ( $] >= 5.006_000 && length($value) >= 3 && $value !~ /[._]/ ) {
552 my $tvalue = sprintf("v%vd",$value);
553 if ( $tvalue =~ /^v\d+\.\d+\.\d+$/ ) {
561 # Thanks to Yitzchak Scott-Thoennes for this mode of operation
564 *UNIVERSAL::VERSION # Module::Build::ModuleInfo doesn't see this now
566 my ($obj, $req) = @_;
567 my $class = ref($obj) || $obj;
570 eval "require $class" unless %{"$class\::"}; # already existing
571 return undef if $@ =~ /Can't locate/ and not defined $req;
573 if ( not %{"$class\::"} and $] >= 5.008) { # file but no package
575 Carp::croak( "$class defines neither package nor VERSION"
576 ."--version check failed");
579 my $version = eval "\$$class\::VERSION";
580 if ( defined $version ) {
581 local $^W if $] <= 5.008;
582 $version = version::vpp->new($version);
585 if ( defined $req ) {
586 unless ( defined $version ) {
589 ? "$class version $req required--this is only version "
590 : "$class does not define \$$class\::VERSION"
591 ."--version check failed";
593 if ( $ENV{VERSION_DEBUG} ) {
601 $req = version::vpp->new($req);
603 if ( $req > $version ) {
607 sprintf ("%s version %s required--".
608 "this is only version %s", $class,
609 $req->normal, $version->normal)
614 sprintf ("%s version %s required--".
615 "this is only version %s", $class,
616 $req->stringify, $version->stringify)
622 return defined $version ? $version->stringify : undef;
626 1; #this line is important and will help the module return a true value