1 package Module::Build::Version;
6 $VERSION = eval $VERSION;
8 eval "use version 0.70";
9 if ($@) { # can't locate version files, use our own
11 # Avoid redefined warnings if an old version.pm was available
12 delete $version::{$_} foreach keys %version::;
14 # first we get the stub version module
17 s/(\$VERSION)\s=\s\d+/\$VERSION = 0/;
22 # and now get the current version::vpp code
25 s/(\$VERSION)\s=\s\d+/\$VERSION = 0/;
30 # but we eval them in reverse order since version depends on
31 # version::vpp to already exist
32 eval $vpp; die $@ if $@;
33 $INC{'version/vpp.pm'} = 'inside Module::Build::Version';
34 eval $version; die $@ if $@;
35 $INC{'version.pm'} = 'inside Module::Build::Version';
38 # now we can safely subclass version, installed or not
47 my ($class, $value) = @_;
48 my $self = $class->SUPER::new($value);
49 $self->original($value);
55 $self->{original} = shift if @_;
56 return $self->{original};
61 return $self->original;
66 # stub version module to make everything else happy
72 use vars qw(@ISA $VERSION $CLASS *qv);
78 push @ISA, "version::vpp";
79 *version::qv = \&version::vpp::qv;
81 # Preloaded methods go here.
84 my $callpkg = caller();
88 sub {return bless version::qv(shift), $class }
89 unless defined(&{"$callpkg\::qv"});
94 # replace everything from here to the end with the current version/vpp.pm
100 use vars qw ($VERSION @ISA @REGEXS);
104 ^v? # optional leading 'v'
105 (\d*) # major revision not required
106 \. # requires at least one decimal
116 'nomethod' => \&vnoop,
121 my ($class, $value) = @_;
122 my $self = bless ({}, ref ($class) || $class);
124 my $currlocale = POSIX::setlocale(&POSIX::LC_ALL);
125 my $radix_comma = ( POSIX::localeconv()->{decimal_point} eq ',' );
127 if ( not defined $value or $value =~ /^undef$/ ) {
128 # RT #19517 - special case for undef comparison
129 # or someone forgot to pass a value
130 push @{$self->{version}}, 0;
134 if ( $#_ == 2 ) { # must be CVS-style
138 $value = _un_vstring($value);
140 # exponential notation
141 if ( $value =~ /\d+.?\d*e-?\d+/ ) {
142 $value = sprintf("%.9f",$value);
146 # if the original locale used commas for decimal points, we
147 # just replace commas with decimal places, rather than changing
149 if ( $radix_comma ) {
153 # This is not very efficient, but it is morally equivalent
154 # to the XS code (as that is the reference implementation).
155 # See vutil/vutil.c for details
160 my ($start, $last, $pos, $s);
163 while ( substr($value,$s,1) =~ /\s/ ) { # leading whitespace is OK
167 if (substr($value,$s,1) eq 'v') {
169 $qv = 1; # force quoted version processing
172 $start = $last = $pos = $s;
174 # pre-scan the input string to check for decimals/underbars
175 while ( substr($value,$pos,1) =~ /[._\d]/ ) {
176 if ( substr($value,$pos,1) eq '.' ) {
179 Carp::croak("Invalid version format ".
180 "(underscores before decimal)");
185 elsif ( substr($value,$pos,1) eq '_' ) {
188 Carp::croak("Invalid version format ".
189 "(multiple underscores)");
192 $width = $pos - $last - 1; # natural width of sub-version
197 if ( $alpha && !$saw_period ) {
199 Carp::croak("Invalid version format (alpha without decimal)");
202 if ( $alpha && $saw_period && $width == 0 ) {
204 Carp::croak("Invalid version format (misplaced _ in number)");
207 if ( $saw_period > 1 ) {
208 $qv = 1; # force quoted version processing
221 if ( !$qv && $width < 3 ) {
222 $self->{width} = $width;
225 while ( substr($value,$pos,1) =~ /\d/ ) {
229 if ( substr($value,$pos,1) !~ /[a-z]/ ) { ### FIX THIS ###
236 # this is atoi() that delimits on underscores
241 # the following if() will only be true after the decimal
242 # point of a version originally created with a bare
243 # floating point number, i.e. not quoted in any way
244 if ( !$qv && $s > $start && $saw_period == 1 ) {
246 while ( $s < $end ) {
248 $rev += substr($value,$s,1) * $mult;
250 if ( abs($orev) > abs($rev) ) {
252 Carp::croak("Integer overflow in version");
255 if ( substr($value,$s,1) eq '_' ) {
261 while (--$end >= $s) {
263 $rev += substr($value,$end,1) * $mult;
265 if ( abs($orev) > abs($rev) ) {
267 Carp::croak("Integer overflow in version");
274 push @{$self->{version}}, $rev;
275 if ( substr($value,$pos,1) eq '.'
276 && substr($value,$pos+1,1) =~ /\d/ ) {
279 elsif ( substr($value,$pos,1) eq '_'
280 && substr($value,$pos+1,1) =~ /\d/ ) {
283 elsif ( substr($value,$pos,1) =~ /\d/ ) {
291 while ( substr($value,$pos,1) =~ /\d/ ) {
297 while (substr($value,$pos,1) =~ /[\d_]/ && $digits < 3) {
298 if ( substr($value,$pos,1) ne '_' ) {
306 if ( $qv ) { # quoted versions always get at least three terms
307 my $len = scalar @{$self->{version}};
310 push @{$self->{version}}, 0;
314 if ( substr($value,$pos) ) { # any remaining text
315 warn "Version string '$value' contains invalid data; ".
316 "ignoring: '".substr($value,$pos)."'";
325 unless (_verify($self)) {
327 Carp::croak("Invalid version object");
329 my $width = $self->{width} || 3;
330 my $alpha = $self->{alpha} || "";
331 my $len = $#{$self->{version}};
332 my $digit = $self->{version}[0];
333 my $string = sprintf("%d.", $digit );
335 for ( my $i = 1 ; $i < $len ; $i++ ) {
336 $digit = $self->{version}[$i];
338 my $denom = 10**(3-$width);
339 my $quot = int($digit/$denom);
340 my $rem = $digit - ($quot * $denom);
341 $string .= sprintf("%0".$width."d_%d", $quot, $rem);
344 $string .= sprintf("%03d", $digit);
349 $digit = $self->{version}[$len];
350 if ( $alpha && $width == 3 ) {
353 $string .= sprintf("%0".$width."d", $digit);
357 $string .= sprintf("000");
366 unless (_verify($self)) {
368 Carp::croak("Invalid version object");
370 my $alpha = $self->{alpha} || "";
371 my $len = $#{$self->{version}};
372 my $digit = $self->{version}[0];
373 my $string = sprintf("v%d", $digit );
375 for ( my $i = 1 ; $i < $len ; $i++ ) {
376 $digit = $self->{version}[$i];
377 $string .= sprintf(".%d", $digit);
381 $digit = $self->{version}[$len];
383 $string .= sprintf("_%0d", $digit);
386 $string .= sprintf(".%0d", $digit);
391 for ( $len = 2 - $len; $len != 0; $len-- ) {
392 $string .= sprintf(".%0d", 0);
402 unless (_verify($self)) {
404 Carp::croak("Invalid version object");
406 if ( exists $self->{qv} ) {
407 return $self->normal;
410 return $self->numify;
417 my ($left,$right,$swap) = @_;
418 my $class = ref($left);
419 unless ( UNIVERSAL::isa($right, $class) ) {
420 $right = $class->new($right);
424 ($left, $right) = ($right, $left);
426 unless (_verify($left)) {
428 Carp::croak("Invalid version object");
430 unless (_verify($right)) {
432 Carp::croak("Invalid version object");
434 my $l = $#{$left->{version}};
435 my $r = $#{$right->{version}};
436 my $m = $l < $r ? $l : $r;
437 my $lalpha = $left->is_alpha;
438 my $ralpha = $right->is_alpha;
441 while ( $i <= $m && $retval == 0 ) {
442 $retval = $left->{version}[$i] <=> $right->{version}[$i];
446 # tiebreaker for alpha with identical terms
449 && $left->{version}[$m] == $right->{version}[$m]
450 && ( $lalpha || $ralpha ) ) {
452 if ( $lalpha && !$ralpha ) {
455 elsif ( $ralpha && !$lalpha) {
460 # possible match except for trailing 0's
461 if ( $retval == 0 && $l != $r ) {
463 while ( $i <= $r && $retval == 0 ) {
464 if ( $right->{version}[$i] != 0 ) {
465 $retval = -1; # not a match after all
471 while ( $i <= $l && $retval == 0 ) {
472 if ( $left->{version}[$i] != 0 ) {
473 $retval = +1; # not a match after all
485 return vcmp($self,$self->new("0"),1);
490 Carp::croak("operation not supported with version object");
495 return (exists $self->{alpha});
501 $value = _un_vstring($value);
502 $value = 'v'.$value unless $value =~ /^v/;
503 return version->new($value); # always use base class
508 return (exists $self->{qv});
515 && eval { exists $self->{version} }
516 && ref($self->{version}) eq 'ARRAY'
528 if ( $] >= 5.006_000 && length($value) >= 3 && $value !~ /[._]/ ) {
529 my $tvalue = sprintf("%vd",$value);
530 if ( $tvalue =~ /^\d+\.\d+\.\d+$/ ) {
538 # Thanks to Yitzchak Scott-Thoennes for this mode of operation
541 *UNIVERSAL::VERSION = sub {
542 my ($obj, $req) = @_;
543 my $class = ref($obj) || $obj;
546 eval "require $class" unless %{"$class\::"}; # already existing
547 return undef if $@ =~ /Can't locate/ and not defined $req;
549 if ( not %{"$class\::"} and $] >= 5.008) { # file but no package
551 Carp::croak( "$class defines neither package nor VERSION"
552 ."--version check failed");
555 my $version = eval "\$$class\::VERSION";
556 if ( defined $version ) {
557 local $^W if $] <= 5.008;
558 $version = version::vpp->new($version);
561 if ( defined $req ) {
562 unless ( defined $version ) {
565 ? "$class version $req required--this is only version "
566 : "$class does not define \$$class\::VERSION"
567 ."--version check failed";
569 if ( $ENV{VERSION_DEBUG} ) {
577 $req = version::vpp->new($req);
579 if ( $req > $version ) {
583 sprintf ("%s version %s required--".
584 "this is only version %s", $class,
585 $req->normal, $version->normal)
590 sprintf ("%s version %s required--".
591 "this is only version %s", $class,
592 $req->numify, $version->numify)
598 return defined $version ? $version->numify : undef;
602 1; #this line is important and will help the module return a true value