File: Compare.pm

package info (click to toggle)
libversion-compare-perl 0.14-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: perl: 66; makefile: 2
file content (151 lines) | stat: -rw-r--r-- 3,316 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package Version::Compare;
{
  $Version::Compare::VERSION = '0.14';
}
# ABSTRACT: Compare version strings

use warnings;
use strict;


sub max {
    my $x = shift;
    my $y = shift;
    return ( $x > $y ? $x : $y );
}

## no critic(ProhibitNumberedNames ProhibitCStyleForLoops)
sub version_compare {
    my $ver1 = shift || 0;
    my $ver2 = shift || 0;
    my @v1 = split /[.+:~-]/, $ver1;
    my @v2 = split /[.+:~-]/, $ver2;

    for ( my $i = 0 ; $i < max( scalar(@v1), scalar(@v2) ) ; $i++ ) {

        # Add missing version parts if one string is shorter than the other
        # i.e. 0 should be lt 0.2.1 and not equal, so we append .0
        # -> 0.0.0 <=> 0.2.1 -> -1
        push( @v1, 0 ) unless defined( $v1[$i] );
        push( @v2, 0 ) unless defined( $v2[$i] );
        if ( int( $v1[$i] ) > int( $v2[$i] ) ) {
            return 1;
        }
        elsif ( int( $v1[$i] ) < int( $v2[$i] ) ) {
            return -1;
        }
    }
    return 0;
}
## use critic

## no critic (RequireArgUnpacking ProhibitBuiltinHomonyms)
sub cmp {
    return version_compare(@_);
}
## use critic


1; # End of Version::Compare

__END__

=pod

=head1 NAME

Version::Compare - Compare version strings

=head1 VERSION

version 0.14

=head1 SYNOPSIS

    use Version::Compare;

    if(&Version::Compare::version_compare('2.6.26','2.6.0') == 1) {
        print "2.6.26 is greater than 2.6.0\n";
    }

=head1 NAME

Version::Compare - Comparing version strings

=head1 SUBROUTINES/METHODS

=head2 max

Return the bigger of the two numerical values

=head2 version_compare

Compare two unix-style version strings like 2.6.23.1 and 2.6.33 and return and sort-like
return code (1 => LHS bigger, 0 => equal, -1 => RHS bigger)

0.0 < 0.5 < 0.10 < 0.99 < 1 < 1.0~rc1 < 1.0 < 1.0+b1 < 1.0+nmu1 < 1.1 < 2.0

=head2 cmp

See L<version_compare>.

=head1 AUTHOR

Dominik Schulz, C<< <dominik.schulz at gauner.org> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-version-compare at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Version-Compare>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Version::Compare

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Version-Compare>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Version-Compare>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Version-Compare>

=item * Search CPAN

L<http://search.cpan.org/dist/Version-Compare/>

=back

=head1 LICENSE AND COPYRIGHT

Copyright 2012 Dominik Schulz

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=head1 AUTHOR

Dominik Schulz <dominik.schulz@gauner.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Dominik Schulz.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut