1 package Module::Build::Version;
4 eval "use version 0.661";
5 if ($@) { # can't locate version files, use our own
7 # Avoid redefined warnings if an old version.pm was available
8 delete $version::{$_} foreach keys %version::;
10 # first we get the stub version module
13 s/(\$VERSION)\s=\s\d+/\$VERSION = 0/;
18 # and now get the current version::vpp code
21 s/(\$VERSION)\s=\s\d+/\$VERSION = 0/;
26 # but we eval them in reverse order since version depends on
27 # version::vpp to already exist
28 eval $vpp; die $@ if $@;
29 $INC{'version/vpp.pm'} = 'inside Module::Build::Version';
30 eval $version; die $@ if $@;
31 $INC{'version.pm'} = 'inside Module::Build::Version';
34 # now we can safely subclass version, installed or not
43 my ($class, $value) = @_;
44 my $self = $class->SUPER::new($value);
45 $self->original($value);
51 $self->{original} = shift if @_;
52 return $self->{original};
57 return $self->original;
62 # stub version module to make everything else happy
68 use vars qw(@ISA $VERSION $CLASS *qv);
74 push @ISA, "version::vpp";
75 *version::qv = \&version::vpp::qv;
77 # Preloaded methods go here.
80 my $callpkg = caller();
84 sub {return bless version::qv(shift), $class }
85 unless defined(&{"$callpkg\::qv"});
90 # replace everything from here to the end with the current version/vpp.pm
96 use vars qw ($VERSION @ISA @REGEXS);
100 ^v? # optional leading 'v'
101 (\d*) # major revision not required
102 \. # requires at least one decimal
114 my ($class, $value) = @_;
115 my $self = bless ({}, ref ($class) || $class);
117 if ( not defined $value or $value =~ /^undef$/ ) {
118 # RT #19517 - special case for undef comparison
119 # or someone forgot to pass a value
120 push @{$self->{version}}, 0;
124 if ( $#_ == 2 ) { # must be CVS-style
129 if ( $] >= 5.006_002 && length($value) >= 3 && $value !~ /[._]/ ) {
130 my $tvalue = sprintf("%vd",$value);
131 if ( $tvalue =~ /^\d+\.\d+\.\d+$/ ) {
137 # exponential notation
138 if ( $value =~ /\d+e-?\d+/ ) {
139 $value = sprintf("%.9f",$value);
143 # This is not very efficient, but it is morally equivalent
144 # to the XS code (as that is the reference implementation).
145 # See vutil/vutil.c for details
150 my ($start, $last, $pos, $s);
153 while ( substr($value,$s,1) =~ /\s/ ) { # leading whitespace is OK
157 if (substr($value,$s,1) eq 'v') {
159 $qv = 1; # force quoted version processing
162 $start = $last = $pos = $s;
164 # pre-scan the input string to check for decimals/underbars
165 while ( substr($value,$pos,1) =~ /[._\d]/ ) {
166 if ( substr($value,$pos,1) eq '.' ) {
167 die "Invalid version format (underscores before decimal)"
172 elsif ( substr($value,$pos,1) eq '_' ) {
173 die "Invalid version format (multiple underscores)"
176 $width = $pos - $last - 1; # natural width of sub-version
181 if ( $alpha && !$saw_period ) {
182 die "Invalid version format (alpha without decimal)";
185 if ( $saw_period > 1 ) {
186 $qv = 1; # force quoted version processing
199 if ( !$qv && $width < 3 ) {
200 $self->{width} = $width;
203 while ( substr($value,$pos,1) =~ /\d/ ) {
207 if ( substr($value,$pos,1) !~ /[a-z]/ ) { ### FIX THIS ###
214 # this is atoi() that delimits on underscores
219 # the following if() will only be true after the decimal
220 # point of a version originally created with a bare
221 # floating point number, i.e. not quoted in any way
222 if ( !$qv && $s > $start && $saw_period == 1 ) {
224 while ( $s < $end ) {
226 $rev += substr($value,$s,1) * $mult;
228 if ( abs($orev) > abs($rev) ) {
229 die "Integer overflow in version";
232 if ( substr($value,$s,1) eq '_' ) {
238 while (--$end >= $s) {
240 $rev += substr($value,$end,1) * $mult;
242 if ( abs($orev) > abs($rev) ) {
243 die "Integer overflow in version";
250 push @{$self->{version}}, $rev;
251 if ( substr($value,$pos,1) eq '.'
252 && substr($value,$pos+1,1) =~ /\d/ ) {
255 elsif ( substr($value,$pos,1) eq '_'
256 && substr($value,$pos+1,1) =~ /\d/ ) {
259 elsif ( substr($value,$pos,1) =~ /\d/ ) {
267 while ( substr($value,$pos,1) =~ /\d/ ) {
273 while (substr($value,$pos,1) =~ /[\d_]/ && $digits < 3) {
274 if ( substr($value,$pos,1) ne '_' ) {
282 if ( $qv ) { # quoted versions always get at least three terms
283 my $len = scalar @{$self->{version}};
286 push @{$self->{version}}, 0;
290 if ( substr($value,$pos) ) { # any remaining text
291 warn "Version string '$value' contains invalid data; ".
292 "ignoring: '".substr($value,$pos)."'";
301 unless (_verify($self)) {
302 die "Invalid version object";
304 my $width = $self->{width} || 3;
305 my $alpha = $self->{alpha} || "";
306 my $len = $#{$self->{version}};
307 my $digit = $self->{version}[0];
308 my $string = sprintf("%d.", $digit );
310 for ( my $i = 1 ; $i < $len ; $i++ ) {
311 $digit = $self->{version}[$i];
313 my $denom = 10**(3-$width);
314 my $quot = int($digit/$denom);
315 my $rem = $digit - ($quot * $denom);
316 $string .= sprintf("%0".$width."d_%d", $quot, $rem);
319 $string .= sprintf("%03d", $digit);
324 $digit = $self->{version}[$len];
325 if ( $alpha && $width == 3 ) {
328 $string .= sprintf("%0".$width."d", $digit);
332 $string .= sprintf("000");
341 unless (_verify($self)) {
342 die "Invalid version object";
344 my $alpha = $self->{alpha} || "";
345 my $len = $#{$self->{version}};
346 my $digit = $self->{version}[0];
347 my $string = sprintf("v%d", $digit );
349 for ( my $i = 1 ; $i < $len ; $i++ ) {
350 $digit = $self->{version}[$i];
351 $string .= sprintf(".%d", $digit);
355 $digit = $self->{version}[$len];
357 $string .= sprintf("_%0d", $digit);
360 $string .= sprintf(".%0d", $digit);
365 for ( $len = 2 - $len; $len != 0; $len-- ) {
366 $string .= sprintf(".%0d", 0);
376 unless (_verify($self)) {
377 die "Invalid version object";
379 if ( exists $self->{qv} ) {
380 return $self->normal;
383 return $self->numify;
390 my ($left,$right,$swap) = @_;
391 my $class = ref($left);
392 unless ( UNIVERSAL::isa($right, $class) ) {
393 $right = $class->new($right);
397 ($left, $right) = ($right, $left);
399 unless (_verify($left)) {
400 die "Invalid version object";
402 unless (_verify($right)) {
403 die "Invalid version object";
405 my $l = $#{$left->{version}};
406 my $r = $#{$right->{version}};
407 my $m = $l < $r ? $l : $r;
408 my $lalpha = $left->is_alpha;
409 my $ralpha = $right->is_alpha;
412 while ( $i <= $m && $retval == 0 ) {
413 $retval = $left->{version}[$i] <=> $right->{version}[$i];
417 # tiebreaker for alpha with identical terms
420 && $left->{version}[$m] == $right->{version}[$m]
421 && ( $lalpha || $ralpha ) ) {
423 if ( $lalpha && !$ralpha ) {
426 elsif ( $ralpha && !$lalpha) {
431 # possible match except for trailing 0's
432 if ( $retval == 0 && $l != $r ) {
434 while ( $i <= $r && $retval == 0 ) {
435 if ( $right->{version}[$i] != 0 ) {
436 $retval = -1; # not a match after all
442 while ( $i <= $l && $retval == 0 ) {
443 if ( $left->{version}[$i] != 0 ) {
444 $retval = +1; # not a match after all
456 return (exists $self->{alpha});
462 my $eval = eval 'Scalar::Util::isvstring($value)';
463 if ( !$@ and $eval ) {
464 $value = sprintf("v%vd",$value);
467 $value = 'v'.$value unless $value =~ /^v/;
469 return version->new($value); # always use base class
474 if ( Scalar::Util::reftype($self) eq 'HASH'
475 && exists $self->{version}
476 && ref($self->{version}) eq 'ARRAY'
485 # Thanks to Yitzchak Scott-Thoennes for this mode of operation
488 *UNIVERSAL::VERSION = sub {
489 my ($obj, $req) = @_;
490 my $class = ref($obj) || $obj;
493 eval "require $class" unless %{"$class\::"}; # already existing
494 die "$class defines neither package nor VERSION--version check failed"
495 if $@ or not %{"$class\::"};
497 my $version = eval "\$$class\::VERSION";
498 if ( defined $version ) {
499 $version = version::vpp->new($version);
502 if ( defined $req ) {
503 unless ( defined $version ) {
504 my $msg = "$class does not define ".
505 "\$$class\::VERSION--version check failed";
506 if ( $ENV{VERSION_DEBUG} ) {
515 $req = version::vpp->new($req);
517 if ( $req > $version ) {
518 die sprintf ("%s version %s (%s) required--".
519 "this is only version %s (%s)", $class,
520 $req->numify, $req->normal,
521 $version->numify, $version->normal);
525 return defined $version ? $version->numify : undef;
529 1; #this line is important and will help the module return a true value