blib/lib/ExtUtils/MakeMaker/version/vpp.pm untested

filter: Statementstmt 0.0% 0 / 607 Branchbran 0.0% 0 / 282 Conditioncond 0.0% 0 / 180 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 61 Podpod 0.0% 0 / 34 totaltotal 0.0% 0 / 1164 CC 141 SCAR 63.1
CC
141
Cov
0%
prescan_version
1980.0
scan_version
600.0
import
182.0
CRAP 549.7
1 #--------------------------------------------------------------------------#
2 # This is a modified copy of version.pm 0.9909, bundled exclusively for
3 # use by ExtUtils::Makemaker and its dependencies to bootstrap when
4 # version.pm is not available. It should not be used by ordinary modules.
5 #--------------------------------------------------------------------------#
6  
7 package ExtUtils::MakeMaker::charstar;
8 # a little helper class to emulate C char* semantics in Perl
9 # so that prescan_version can use the same code as in C
10  
11 use overload (
12 '""' => \&thischar,
13 '0+' => \&thischar,
14 '++' => \&increment,
15 '--' => \&decrement,
16 '+' => \&plus,
17 '-' => \&minus,
18 '*' => \&multiply,
19 'cmp' => \&cmp,
20 '<=>' => \&spaceship,
21 'bool' => \&thischar,
22 '=' => \&clone,
23 );
24  
25 sub new {
26 my ($self, $string) = @_;
27 my $class = ref($self) || $self;
28  
29 my $obj = {
30 string => [split(//,$string)],
31 current => 0,
32 };
33 return bless $obj, $class;
34 }
35  
36 sub thischar {
37 my ($self) = @_;
38 my $last = $#{$self->{string}};
39 my $curr = $self->{current};
40 if ($curr >= 0 && $curr <= $last) {
41 return $self->{string}->[$curr];
42 }
43 else {
44 return '';
45 }
46 }
47  
48 sub increment {
49 my ($self) = @_;
50 $self->{current}++;
51 }
52  
53 sub decrement {
54 my ($self) = @_;
55 $self->{current}--;
56 }
57  
58 sub plus {
59 my ($self, $offset) = @_;
60 my $rself = $self->clone;
61 $rself->{current} += $offset;
62 return $rself;
63 }
64  
65 sub minus {
66 my ($self, $offset) = @_;
67 my $rself = $self->clone;
68 $rself->{current} -= $offset;
69 return $rself;
70 }
71  
72 sub multiply {
73 my ($left, $right, $swapped) = @_;
74 my $char = $left->thischar();
75 return $char * $right;
76 }
77  
78 sub spaceship {
79 my ($left, $right, $swapped) = @_;
80 unless (ref($right)) { # not an object already
81 $right = $left->new($right);
82 }
83 return $left->{current} <=> $right->{current};
84 }
85  
86 sub cmp {
87 my ($left, $right, $swapped) = @_;
88 unless (ref($right)) { # not an object already
89 if (length($right) == 1) { # comparing single character only
90 return $left->thischar cmp $right;
91 }
92 $right = $left->new($right);
93 }
94 return $left->currstr cmp $right->currstr;
95 }
96  
97 sub bool {
98 my ($self) = @_;
99 my $char = $self->thischar;
100 return ($char ne '');
101 }
102  
103 sub clone {
104 my ($left, $right, $swapped) = @_;
105 $right = {
106 string => [@{$left->{string}}],
107 current => $left->{current},
108 };
109 return bless $right, ref($left);
110 }
111  
112 sub currstr {
113 my ($self, $s) = @_;
114 my $curr = $self->{current};
115 my $last = $#{$self->{string}};
116 if (defined($s) && $s->{current} < $last) {
117 $last = $s->{current};
118 }
119  
120 my $string = join('', @{$self->{string}}[$curr..$last]);
121 return $string;
122 }
123  
124 package ExtUtils::MakeMaker::version::vpp;
125  
126 use 5.006001;
127 use strict;
128  
129 use Config;
130 use vars qw($VERSION $CLASS @ISA $LAX $STRICT);
131 $VERSION = '7.13_01';
132 $VERSION = eval $VERSION;
133 $CLASS = 'ExtUtils::MakeMaker::version::vpp';
134  
135 require ExtUtils::MakeMaker::version::regex;
136 *ExtUtils::MakeMaker::version::vpp::is_strict = \&ExtUtils::MakeMaker::version::regex::is_strict;
137 *ExtUtils::MakeMaker::version::vpp::is_lax = \&ExtUtils::MakeMaker::version::regex::is_lax;
138 *LAX = \$ExtUtils::MakeMaker::version::regex::LAX;
139 *STRICT = \$ExtUtils::MakeMaker::version::regex::STRICT;
140  
141 use overload (
142 '""' => \&stringify,
143 '0+' => \&numify,
144 'cmp' => \&vcmp,
145 '<=>' => \&vcmp,
146 'bool' => \&vbool,
147 '+' => \&vnoop,
148 '-' => \&vnoop,
149 '*' => \&vnoop,
150 '/' => \&vnoop,
151 '+=' => \&vnoop,
152 '-=' => \&vnoop,
153 '*=' => \&vnoop,
154 '/=' => \&vnoop,
155 'abs' => \&vnoop,
156 );
157  
158 eval "use warnings";
159 if ($@) {
160 eval '
161 package
162 warnings;
163 sub enabled {return $^W;}
164 1;
165 ';
166 }
167  
168 sub import {
169 no strict 'refs';
170 my ($class) = shift;
171  
172 # Set up any derived class
173 unless ($class eq $CLASS) {
174 local $^W;
175 *{$class.'::declare'} = \&{$CLASS.'::declare'};
176 *{$class.'::qv'} = \&{$CLASS.'::qv'};
177 }
178  
179 my %args;
180 if (@_) { # any remaining terms are arguments
181 map { $args{$_} = 1 } @_
182 }
183 else { # no parameters at all on use line
184 %args =
185 (
186 qv => 1,
187 'UNIVERSAL::VERSION' => 1,
188 );
189 }
190  
191 my $callpkg = caller();
192  
193 if (exists($args{declare})) {
194 *{$callpkg.'::declare'} =
195 sub {return $class->declare(shift) }
196 unless defined(&{$callpkg.'::declare'});
197 }
198  
199 if (exists($args{qv})) {
200 *{$callpkg.'::qv'} =
201 sub {return $class->qv(shift) }
202 unless defined(&{$callpkg.'::qv'});
203 }
204  
205 if (exists($args{'UNIVERSAL::VERSION'})) {
206 local $^W;
207 *UNIVERSAL::VERSION
208 = \&{$CLASS.'::_VERSION'};
209 }
210  
211 if (exists($args{'VERSION'})) {
212 *{$callpkg.'::VERSION'} = \&{$CLASS.'::_VERSION'};
213 }
214  
215 if (exists($args{'is_strict'})) {
216 *{$callpkg.'::is_strict'} = \&{$CLASS.'::is_strict'}
217 unless defined(&{$callpkg.'::is_strict'});
218 }
219  
220 if (exists($args{'is_lax'})) {
221 *{$callpkg.'::is_lax'} = \&{$CLASS.'::is_lax'}
222 unless defined(&{$callpkg.'::is_lax'});
223 }
224 }
225  
226 my $VERSION_MAX = 0x7FFFFFFF;
227  
228 # implement prescan_version as closely to the C version as possible
229 use constant TRUE => 1;
230 use constant FALSE => 0;
231  
232 sub isDIGIT {
233 my ($char) = shift->thischar();
234 return ($char =~ /\d/);
235 }
236  
237 sub isALPHA {
238 my ($char) = shift->thischar();
239 return ($char =~ /[a-zA-Z]/);
240 }
241  
242 sub isSPACE {
243 my ($char) = shift->thischar();
244 return ($char =~ /\s/);
245 }
246  
247 sub BADVERSION {
248 my ($s, $errstr, $error) = @_;
249 if ($errstr) {
250 $$errstr = $error;
251 }
252 return $s;
253 }
254  
255 sub prescan_version {
256 my ($s, $strict, $errstr, $sqv, $ssaw_decimal, $swidth, $salpha) = @_;
257 my $qv = defined $sqv ? $$sqv : FALSE;
258 my $saw_decimal = defined $ssaw_decimal ? $$ssaw_decimal : 0;
259 my $width = defined $swidth ? $$swidth : 3;
260 my $alpha = defined $salpha ? $$salpha : FALSE;
261  
262 my $d = $s;
263  
264 if ($qv && isDIGIT($d)) {
265 goto dotted_decimal_version;
266 }
267  
268 if ($d eq 'v') { # explicit v-string
269 $d++;
270 if (isDIGIT($d)) {
271 $qv = TRUE;
272 }
273 else { # degenerate v-string
274 # requires v1.2.3
275 return BADVERSION($s,$errstr,"Invalid version format (dotted-decimal versions require at least three parts)");
276 }
277  
278 dotted_decimal_version:
279 if ($strict && $d eq '0' && isDIGIT($d+1)) {
280 # no leading zeros allowed
281 return BADVERSION($s,$errstr,"Invalid version format (no leading zeros)");
282 }
283  
284 while (isDIGIT($d)) { # integer part
285 $d++;
286 }
287  
288 if ($d eq '.')
289 {
290 $saw_decimal++;
291 $d++; # decimal point
292 }
293 else
294 {
295 if ($strict) {
296 # require v1.2.3
297 return BADVERSION($s,$errstr,"Invalid version format (dotted-decimal versions require at least three parts)");
298 }
299 else {
300 goto version_prescan_finish;
301 }
302 }
303  
304 {
305 my $i = 0;
306 my $j = 0;
307 while (isDIGIT($d)) { # just keep reading
308 $i++;
309 while (isDIGIT($d)) {
310 $d++; $j++;
311 # maximum 3 digits between decimal
312 if ($strict && $j > 3) {
313 return BADVERSION($s,$errstr,"Invalid version format (maximum 3 digits between decimals)");
314 }
315 }
316 if ($d eq '_') {
317 if ($strict) {
318 return BADVERSION($s,$errstr,"Invalid version format (no underscores)");
319 }
320 if ( $alpha ) {
321 return BADVERSION($s,$errstr,"Invalid version format (multiple underscores)");
322 }
323 $d++;
324 $alpha = TRUE;
325 }
326 elsif ($d eq '.') {
327 if ($alpha) {
328 return BADVERSION($s,$errstr,"Invalid version format (underscores before decimal)");
329 }
330 $saw_decimal++;
331 $d++;
332 }
333 elsif (!isDIGIT($d)) {
334 last;
335 }
336 $j = 0;
337 }
338  
339 if ($strict && $i < 2) {
340 # requires v1.2.3
341 return BADVERSION($s,$errstr,"Invalid version format (dotted-decimal versions require at least three parts)");
342 }
343 }
344 } # end if dotted-decimal
345 else
346 { # decimal versions
347 my $j = 0;
348 # special $strict case for leading '.' or '0'
349 if ($strict) {
350 if ($d eq '.') {
351 return BADVERSION($s,$errstr,"Invalid version format (0 before decimal required)");
352 }
353 if ($d eq '0' && isDIGIT($d+1)) {
354 return BADVERSION($s,$errstr,"Invalid version format (no leading zeros)");
355 }
356 }
357  
358 # and we never support negative version numbers
359 if ($d eq '-') {
360 return BADVERSION($s,$errstr,"Invalid version format (negative version number)");
361 }
362  
363 # consume all of the integer part
364 while (isDIGIT($d)) {
365 $d++;
366 }
367  
368 # look for a fractional part
369 if ($d eq '.') {
370 # we found it, so consume it
371 $saw_decimal++;
372 $d++;
373 }
374 elsif (!$d || $d eq ';' || isSPACE($d) || $d eq '}') {
375 if ( $d == $s ) {
376 # found nothing
377 return BADVERSION($s,$errstr,"Invalid version format (version required)");
378 }
379 # found just an integer
380 goto version_prescan_finish;
381 }
382 elsif ( $d == $s ) {
383 # didn't find either integer or period
384 return BADVERSION($s,$errstr,"Invalid version format (non-numeric data)");
385 }
386 elsif ($d eq '_') {
387 # underscore can't come after integer part
388 if ($strict) {
389 return BADVERSION($s,$errstr,"Invalid version format (no underscores)");
390 }
391 elsif (isDIGIT($d+1)) {
392 return BADVERSION($s,$errstr,"Invalid version format (alpha without decimal)");
393 }
394 else {
395 return BADVERSION($s,$errstr,"Invalid version format (misplaced underscore)");
396 }
397 }
398 elsif ($d) {
399 # anything else after integer part is just invalid data
400 return BADVERSION($s,$errstr,"Invalid version format (non-numeric data)");
401 }
402  
403 # scan the fractional part after the decimal point
404 if ($d && !isDIGIT($d) && ($strict || ! ($d eq ';' || isSPACE($d) || $d eq '}') )) {
405 # $strict or lax-but-not-the-end
406 return BADVERSION($s,$errstr,"Invalid version format (fractional part required)");
407 }
408  
409 while (isDIGIT($d)) {
410 $d++; $j++;
411 if ($d eq '.' && isDIGIT($d-1)) {
412 if ($alpha) {
413 return BADVERSION($s,$errstr,"Invalid version format (underscores before decimal)");
414 }
415 if ($strict) {
416 return BADVERSION($s,$errstr,"Invalid version format (dotted-decimal versions must begin with 'v')");
417 }
418 $d = $s; # start all over again
419 $qv = TRUE;
420 goto dotted_decimal_version;
421 }
422 if ($d eq '_') {
423 if ($strict) {
424 return BADVERSION($s,$errstr,"Invalid version format (no underscores)");
425 }
426 if ( $alpha ) {
427 return BADVERSION($s,$errstr,"Invalid version format (multiple underscores)");
428 }
429 if ( ! isDIGIT($d+1) ) {
430 return BADVERSION($s,$errstr,"Invalid version format (misplaced underscore)");
431 }
432 $width = $j;
433 $d++;
434 $alpha = TRUE;
435 }
436 }
437 }
438  
439 version_prescan_finish:
440 while (isSPACE($d)) {
441 $d++;
442 }
443  
444 if ($d && !isDIGIT($d) && (! ($d eq ';' || $d eq '}') )) {
445 # trailing non-numeric data
446 return BADVERSION($s,$errstr,"Invalid version format (non-numeric data)");
447 }
448  
449 if (defined $sqv) {
450 $$sqv = $qv;
451 }
452 if (defined $swidth) {
453 $$swidth = $width;
454 }
455 if (defined $ssaw_decimal) {
456 $$ssaw_decimal = $saw_decimal;
457 }
458 if (defined $salpha) {
459 $$salpha = $alpha;
460 }
461 return $d;
462 }
463  
464 sub scan_version {
465 my ($s, $rv, $qv) = @_;
466 my $start;
467 my $pos;
468 my $last;
469 my $errstr;
470 my $saw_decimal = 0;
471 my $width = 3;
472 my $alpha = FALSE;
473 my $vinf = FALSE;
474 my @av;
475  
476 $s = new ExtUtils::MakeMaker::charstar $s;
477  
478 while (isSPACE($s)) { # leading whitespace is OK
479 $s++;
480 }
481  
482 $last = prescan_version($s, FALSE, \$errstr, \$qv, \$saw_decimal,
483 \$width, \$alpha);
484  
485 if ($errstr) {
486 # 'undef' is a special case and not an error
487 if ( $s ne 'undef') {
488 require Carp;
489 Carp::croak($errstr);
490 }
491 }
492  
493 $start = $s;
494 if ($s eq 'v') {
495 $s++;
496 }
497 $pos = $s;
498  
499 if ( $qv ) {
500 $$rv->{qv} = $qv;
501 }
502 if ( $alpha ) {
503 $$rv->{alpha} = $alpha;
504 }
505 if ( !$qv && $width < 3 ) {
506 $$rv->{width} = $width;
507 }
508  
509 while (isDIGIT($pos)) {
510 $pos++;
511 }
512 if (!isALPHA($pos)) {
513 my $rev;
514  
515 for (;;) {
516 $rev = 0;
517 {
518 # this is atoi() that delimits on underscores
519 my $end = $pos;
520 my $mult = 1;
521 my $orev;
522  
523 # the following if() will only be true after the decimal
524 # point of a version originally created with a bare
525 # floating point number, i.e. not quoted in any way
526 #
527 if ( !$qv && $s > $start && $saw_decimal == 1 ) {
528 $mult *= 100;
529 while ( $s < $end ) {
530 $orev = $rev;
531 $rev += $s * $mult;
532 $mult /= 10;
533 if ( (abs($orev) > abs($rev))
534 || (abs($rev) > $VERSION_MAX )) {
535 warn("Integer overflow in version %d",
536 $VERSION_MAX);
537 $s = $end - 1;
538 $rev = $VERSION_MAX;
539 $vinf = 1;
540 }
541 $s++;
542 if ( $s eq '_' ) {
543 $s++;
544 }
545 }
546 }
547 else {
548 while (--$end >= $s) {
549 $orev = $rev;
550 $rev += $end * $mult;
551 $mult *= 10;
552 if ( (abs($orev) > abs($rev))
553 || (abs($rev) > $VERSION_MAX )) {
554 warn("Integer overflow in version");
555 $end = $s - 1;
556 $rev = $VERSION_MAX;
557 $vinf = 1;
558 }
559 }
560 }
561 }
562  
563 # Append revision
564 push @av, $rev;
565 if ( $vinf ) {
566 $s = $last;
567 last;
568 }
569 elsif ( $pos eq '.' ) {
570 $s = ++$pos;
571 }
572 elsif ( $pos eq '_' && isDIGIT($pos+1) ) {
573 $s = ++$pos;
574 }
575 elsif ( $pos eq ',' && isDIGIT($pos+1) ) {
576 $s = ++$pos;
577 }
578 elsif ( isDIGIT($pos) ) {
579 $s = $pos;
580 }
581 else {
582 $s = $pos;
583 last;
584 }
585 if ( $qv ) {
586 while ( isDIGIT($pos) ) {
587 $pos++;
588 }
589 }
590 else {
591 my $digits = 0;
592 while ( ( isDIGIT($pos) || $pos eq '_' ) && $digits < 3 ) {
593 if ( $pos ne '_' ) {
594 $digits++;
595 }
596 $pos++;
597 }
598 }
599 }
600 }
601 if ( $qv ) { # quoted versions always get at least three terms
602 my $len = $#av;
603 # This for loop appears to trigger a compiler bug on OS X, as it
604 # loops infinitely. Yes, len is negative. No, it makes no sense.
605 # Compiler in question is:
606 # gcc version 3.3 20030304 (Apple Computer, Inc. build 1640)
607 # for ( len = 2 - len; len > 0; len-- )
608 # av_push(MUTABLE_AV(sv), newSViv(0));
609 #
610 $len = 2 - $len;
611 while ($len-- > 0) {
612 push @av, 0;
613 }
614 }
615  
616 # need to save off the current version string for later
617 if ( $vinf ) {
618 $$rv->{original} = "v.Inf";
619 $$rv->{vinf} = 1;
620 }
621 elsif ( $s > $start ) {
622 $$rv->{original} = $start->currstr($s);
623 if ( $qv && $saw_decimal == 1 && $start ne 'v' ) {
624 # need to insert a v to be consistent
625 $$rv->{original} = 'v' . $$rv->{original};
626 }
627 }
628 else {
629 $$rv->{original} = '0';
630 push(@av, 0);
631 }
632  
633 # And finally, store the AV in the hash
634 $$rv->{version} = \@av;
635  
636 # fix RT#19517 - special case 'undef' as string
637 if ($s eq 'undef') {
638 $s += 5;
639 }
640  
641 return $s;
642 }
643  
644 sub new {
645 my $class = shift;
646 unless (defined $class or $#_ > 1) {
647 require Carp;
648 Carp::croak('Usage: version::new(class, version)');
649 }
650  
651 my $self = bless ({}, ref ($class) || $class);
652 my $qv = FALSE;
653  
654 if ( $#_ == 1 ) { # must be CVS-style
655 $qv = TRUE;
656 }
657 my $value = pop; # always going to be the last element
658  
659 if ( ref($value) && eval('$value->isa("version")') ) {
660 # Can copy the elements directly
661 $self->{version} = [ @{$value->{version} } ];
662 $self->{qv} = 1 if $value->{qv};
663 $self->{alpha} = 1 if $value->{alpha};
664 $self->{original} = ''.$value->{original};
665 return $self;
666 }
667  
668 if ( not defined $value or $value =~ /^undef$/ ) {
669 # RT #19517 - special case for undef comparison
670 # or someone forgot to pass a value
671 push @{$self->{version}}, 0;
672 $self->{original} = "0";
673 return ($self);
674 }
675  
676  
677 if (ref($value) =~ m/ARRAY|HASH/) {
678 require Carp;
679 Carp::croak("Invalid version format (non-numeric data)");
680 }
681  
682 $value = _un_vstring($value);
683  
684 if ($Config{d_setlocale} && eval { require POSIX } ) {
685 require locale;
686 my $currlocale = POSIX::setlocale(&POSIX::LC_ALL);
687  
688 # if the current locale uses commas for decimal points, we
689 # just replace commas with decimal places, rather than changing
690 # locales
691 if ( POSIX::localeconv()->{decimal_point} eq ',' ) {
692 $value =~ tr/,/./;
693 }
694 }
695  
696 # exponential notation
697 if ( $value =~ /\d+.?\d*e[-+]?\d+/ ) {
698 $value = sprintf("%.9f",$value);
699 $value =~ s/(0+)$//; # trim trailing zeros
700 }
701  
702 my $s = scan_version($value, \$self, $qv);
703  
704 if ($s) { # must be something left over
705 warn("Version string '%s' contains invalid data; "
706 ."ignoring: '%s'", $value, $s);
707 }
708  
709 return ($self);
710 }
711  
712 *parse = \&new;
713  
714 sub numify {
715 my ($self) = @_;
716 unless (_verify($self)) {
717 require Carp;
718 Carp::croak("Invalid version object");
719 }
720 my $width = $self->{width} || 3;
721 my $alpha = $self->{alpha} || "";
722 my $len = $#{$self->{version}};
723 my $digit = $self->{version}[0];
724 my $string = sprintf("%d.", $digit );
725  
726 for ( my $i = 1 ; $i < $len ; $i++ ) {
727 $digit = $self->{version}[$i];
728 if ( $width < 3 ) {
729 my $denom = 10**(3-$width);
730 my $quot = int($digit/$denom);
731 my $rem = $digit - ($quot * $denom);
732 $string .= sprintf("%0".$width."d_%d", $quot, $rem);
733 }
734 else {
735 $string .= sprintf("%03d", $digit);
736 }
737 }
738  
739 if ( $len > 0 ) {
740 $digit = $self->{version}[$len];
741 if ( $alpha && $width == 3 ) {
742 $string .= "_";
743 }
744 $string .= sprintf("%0".$width."d", $digit);
745 }
746 else # $len = 0
747 {
748 $string .= sprintf("000");
749 }
750  
751 return $string;
752 }
753  
754 sub normal {
755 my ($self) = @_;
756 unless (_verify($self)) {
757 require Carp;
758 Carp::croak("Invalid version object");
759 }
760 my $alpha = $self->{alpha} || "";
761 my $len = $#{$self->{version}};
762 my $digit = $self->{version}[0];
763 my $string = sprintf("v%d", $digit );
764  
765 for ( my $i = 1 ; $i < $len ; $i++ ) {
766 $digit = $self->{version}[$i];
767 $string .= sprintf(".%d", $digit);
768 }
769  
770 if ( $len > 0 ) {
771 $digit = $self->{version}[$len];
772 if ( $alpha ) {
773 $string .= sprintf("_%0d", $digit);
774 }
775 else {
776 $string .= sprintf(".%0d", $digit);
777 }
778 }
779  
780 if ( $len <= 2 ) {
781 for ( $len = 2 - $len; $len != 0; $len-- ) {
782 $string .= sprintf(".%0d", 0);
783 }
784 }
785  
786 return $string;
787 }
788  
789 sub stringify {
790 my ($self) = @_;
791 unless (_verify($self)) {
792 require Carp;
793 Carp::croak("Invalid version object");
794 }
795 return exists $self->{original}
796 ? $self->{original}
797 : exists $self->{qv}
798 ? $self->normal
799 : $self->numify;
800 }
801  
802 sub vcmp {
803 require UNIVERSAL;
804 my ($left,$right,$swap) = @_;
805 my $class = ref($left);
806 unless ( UNIVERSAL::isa($right, $class) ) {
807 $right = $class->new($right);
808 }
809  
810 if ( $swap ) {
811 ($left, $right) = ($right, $left);
812 }
813 unless (_verify($left)) {
814 require Carp;
815 Carp::croak("Invalid version object");
816 }
817 unless (_verify($right)) {
818 require Carp;
819 Carp::croak("Invalid version format");
820 }
821 my $l = $#{$left->{version}};
822 my $r = $#{$right->{version}};
823 my $m = $l < $r ? $l : $r;
824 my $lalpha = $left->is_alpha;
825 my $ralpha = $right->is_alpha;
826 my $retval = 0;
827 my $i = 0;
828 while ( $i <= $m && $retval == 0 ) {
829 $retval = $left->{version}[$i] <=> $right->{version}[$i];
830 $i++;
831 }
832  
833 # tiebreaker for alpha with identical terms
834 if ( $retval == 0
835 && $l == $r
836 && $left->{version}[$m] == $right->{version}[$m]
837 && ( $lalpha || $ralpha ) ) {
838  
839 if ( $lalpha && !$ralpha ) {
840 $retval = -1;
841 }
842 elsif ( $ralpha && !$lalpha) {
843 $retval = +1;
844 }
845 }
846  
847 # possible match except for trailing 0's
848 if ( $retval == 0 && $l != $r ) {
849 if ( $l < $r ) {
850 while ( $i <= $r && $retval == 0 ) {
851 if ( $right->{version}[$i] != 0 ) {
852 $retval = -1; # not a match after all
853 }
854 $i++;
855 }
856 }
857 else {
858 while ( $i <= $l && $retval == 0 ) {
859 if ( $left->{version}[$i] != 0 ) {
860 $retval = +1; # not a match after all
861 }
862 $i++;
863 }
864 }
865 }
866  
867 return $retval;
868 }
869  
870 sub vbool {
871 my ($self) = @_;
872 return vcmp($self,$self->new("0"),1);
873 }
874  
875 sub vnoop {
876 require Carp;
877 Carp::croak("operation not supported with version object");
878 }
879  
880 sub is_alpha {
881 my ($self) = @_;
882 return (exists $self->{alpha});
883 }
884  
885 sub qv {
886 my $value = shift;
887 my $class = $CLASS;
888 if (@_) {
889 $class = ref($value) || $value;
890 $value = shift;
891 }
892  
893 $value = _un_vstring($value);
894 $value = 'v'.$value unless $value =~ /(^v|\d+\.\d+\.\d)/;
895 my $obj = $CLASS->new($value);
896 return bless $obj, $class;
897 }
898  
899 *declare = \&qv;
900  
901 sub is_qv {
902 my ($self) = @_;
903 return (exists $self->{qv});
904 }
905  
906  
907 sub _verify {
908 my ($self) = @_;
909 if ( ref($self)
910 && eval { exists $self->{version} }
911 && ref($self->{version}) eq 'ARRAY'
912 ) {
913 return 1;
914 }
915 else {
916 return 0;
917 }
918 }
919  
920 sub _is_non_alphanumeric {
921 my $s = shift;
922 $s = new ExtUtils::MakeMaker::charstar $s;
923 while ($s) {
924 return 0 if isSPACE($s); # early out
925 return 1 unless (isALPHA($s) || isDIGIT($s) || $s =~ /[.-]/);
926 $s++;
927 }
928 return 0;
929 }
930  
931 sub _un_vstring {
932 my $value = shift;
933 # may be a v-string
934 if ( length($value) >= 3 && $value !~ /[._]/
935 && _is_non_alphanumeric($value)) {
936 my $tvalue;
937 if ( $] ge 5.008_001 ) {
938 $tvalue = _find_magic_vstring($value);
939 $value = $tvalue if length $tvalue;
940 }
941 elsif ( $] ge 5.006_000 ) {
942 $tvalue = sprintf("v%vd",$value);
943 if ( $tvalue =~ /^v\d+(\.\d+){2,}$/ ) {
944 # must be a v-string
945 $value = $tvalue;
946 }
947 }
948 }
949 return $value;
950 }
951  
952 sub _find_magic_vstring {
953 my $value = shift;
954 my $tvalue = '';
955 require B;
956 my $sv = B::svref_2object(\$value);
957 my $magic = ref($sv) eq 'B::PVMG' ? $sv->MAGIC : undef;
958 while ( $magic ) {
959 if ( $magic->TYPE eq 'V' ) {
960 $tvalue = $magic->PTR;
961 $tvalue =~ s/^v?(.+)$/v$1/;
962 last;
963 }
964 else {
965 $magic = $magic->MOREMAGIC;
966 }
967 }
968 return $tvalue;
969 }
970  
971 sub _VERSION {
972 my ($obj, $req) = @_;
973 my $class = ref($obj) || $obj;
974  
975 no strict 'refs';
976 if ( exists $INC{"$class.pm"} and not %{"$class\::"} and $] >= 5.008) {
977 # file but no package
978 require Carp;
979 Carp::croak( "$class defines neither package nor VERSION"
980 ."--version check failed");
981 }
982  
983 my $version = eval "\$$class\::VERSION";
984 if ( defined $version ) {
985 local $^W if $] <= 5.008;
986 $version = ExtUtils::MakeMaker::version::vpp->new($version);
987 }
988  
989 if ( defined $req ) {
990 unless ( defined $version ) {
991 require Carp;
992 my $msg = $] < 5.006
993 ? "$class version $req required--this is only version "
994 : "$class does not define \$$class\::VERSION"
995 ."--version check failed";
996  
997 if ( $ENV{VERSION_DEBUG} ) {
998 Carp::confess($msg);
999 }
1000 else {
1001 Carp::croak($msg);
1002 }
1003 }
1004  
1005 $req = ExtUtils::MakeMaker::version::vpp->new($req);
1006  
1007 if ( $req > $version ) {
1008 require Carp;
1009 if ( $req->is_qv ) {
1010 Carp::croak(
1011 sprintf ("%s version %s required--".
1012 "this is only version %s", $class,
1013 $req->normal, $version->normal)
1014 );
1015 }
1016 else {
1017 Carp::croak(
1018 sprintf ("%s version %s required--".
1019 "this is only version %s", $class,
1020 $req->stringify, $version->stringify)
1021 );
1022 }
1023 }
1024 }
1025  
1026 return defined $version ? $version->stringify : undef;
1027 }
1028  
1029 1; #this line is important and will help the module return a true value