2 # Maemian::Relation::Version -- comparison operators on Debian versions
4 # Copyright (C) 1998 Christian Schwarz and Richard Braakman
5 # Copyright (C) 2004-2009 Russ Allbery <rra@debian.org>
6 # Copyright (C) 2009 Adam D. Barratt <adam@adam-barratt.org.uk>
8 # This program is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by the Free
10 # Software Foundation; either version 2 of the License, or (at your option)
13 # This program is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 # You should have received a copy of the GNU General Public License along with
19 # this program. If not, see <http://www.gnu.org/licenses/>.
21 package Maemian::Relation::Version;
30 our @EXPORT = qw(versions_equal versions_lte versions_gte versions_lt
31 versions_gt versions_compare);
34 use AptPkg::Config '$_config';
35 my $versioning = $_config->system->versioning;
39 Maemian::Relation::Version - Comparison operators on Debian versions
43 print "yes\n" if versions_equal('1.0', '1.00');
44 print "yes\n" if versions_gte('1.1', '1.0');
45 print "no\n" if versions_lte('1.1', '1.0');
46 print "yes\n" if versions_gt('1.1', '1.0');
47 print "no\n" if versions_lt('1.1', '1.1');
48 print "yes\n" if versions_compare('1.1', '<=', '1.1');
52 This module provides five functions for comparing version numbers. The
53 underlying implementation uses C<libapt-pkg-perl> to ensure that
54 the results match what dpkg will expect.
60 =item versions_equal(A, B)
62 Returns true if A is equal to B (C<=>) and false otherwise.
72 $result = $versioning->compare($p, $q);
74 return ($result == 0);
77 =item versions_lte(A, B)
79 Returns true if A is less than or equal (C<< <= >>) to B and false
90 $result = $versioning->compare($p, $q);
92 return ($result <= 0);
95 =item versions_gte(A, B)
97 Returns true if A is greater than or equal (C<< >= >>) to B and false
106 return 1 if $p eq $q;
108 $result = $versioning->compare($p, $q);
110 return ($result >= 0);
113 =item versions_lt(A, B)
115 Returns true if A is less than (C<<< << >>>) B and false otherwise.
123 return 0 if $p eq $q;
125 $result = $versioning->compare($p, $q);
127 return ($result < 0);
130 =item versions_gt(A, B)
132 Returns true if A is greater than (C<<< >> >>>) B and false otherwise.
140 return 0 if $p eq $q;
142 $result = $versioning->compare($p, $q);
144 return ($result > 0);
147 =item versions_compare(A, OP, B)
149 Returns true if A OP B, where OP is one of C<=>, C<< <= >>, C<< >= >>,
150 C<<< << >>>, or C<<< >> >>>, and false otherwise.
154 sub versions_compare {
155 my ($p, $op, $q) = @_;
156 if ($op eq '=') { return versions_equal($p, $q) }
157 elsif ($op eq '<=') { return versions_lte ($p, $q) }
158 elsif ($op eq '>=') { return versions_gte ($p, $q) }
159 elsif ($op eq '<<') { return versions_lt ($p, $q) }
160 elsif ($op eq '>>') { return versions_gt ($p, $q) }
161 else { croak("unknown operator $op") }
168 Originally written by Russ Allbery <rra@debian.org> for Maemian and adapted
169 to use libapt-pkg-perl by Adam D. Barratt <adam@adam-barratt-org.uk>.
180 # indent-tabs-mode: nil
181 # cperl-indent-level: 4
183 # vim: syntax=perl sw=4 sts=4 ts=4 et shiftround