Sort-Versions-1.5/0040755000103200010320000000000007722241514011650 5ustar ededSort-Versions-1.5/MANIFEST0100644000103200010320000000006507330365271013001 0ustar ededMANIFEST Makefile.PL README Versions.pm t/versions.t Sort-Versions-1.5/Makefile.PL0100644000103200010320000000025107722237607013625 0ustar ededuse ExtUtils::MakeMaker; use Carp; # $Id: Makefile.PL,v 1.4 2003/08/24 22:43:19 ed Exp $ &WriteMakefile( NAME => 'Sort::Versions', VERSION_FROM => 'Versions.pm', ); Sort-Versions-1.5/README0100644000103200010320000000235507722240765012541 0ustar eded Sort::Versions - a perl 5 module for sorting of revision (and similar) numbers This module allows easy sorting (via comparisons) of mixed text and numeric strings, similar to the complex "version numbers" that many revision control packages and shared library systems use. For an explanation of the algorithm, it's easiest to look at these examples: 1.1 < 1.2 1.1a < 1.2 1.1 < 1.1.1 1.1 < 1.1a 1.1.a < 1.1a 1 < a a < b 1 < 2 (special handling for leading zeros) 0002 < 1 1.06 < 1.5 (a hyphen binds looser than a period) 1-1 < 1-2 1-2 < 1.2 To install, unpack the tarball and say: perl Makefile.PL make make test If the tests are successful, say as root: make install And that's it. Further documentation is available in the Versions.pm file, which may be accessed via 'perldoc Sort::Versions' after installation. * Changes in this release This is version 1.5; there are no functional changes since the last version but some tidying and extra tests. * Author and copying The files in this package are copyright Kenneth J. Albanowski, Ed Avis, and Matt Johnson. This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Sort-Versions-1.5/Versions.pm0100644000103200010320000001026007722241406014012 0ustar eded#!/usr/bin/perl # $Id: Versions.pm,v 1.9 2003/08/24 22:58:14 ed Exp $ # Copyright (c) 1996, Kenneth J. Albanowski. All rights reserved. This # program is free software; you can redistribute it and/or modify it under # the same terms as Perl itself. package Sort::Versions; use vars '$VERSION'; $VERSION = '1.5'; require Exporter; @ISA=qw(Exporter); @EXPORT=qw(&versions &versioncmp); @EXPORT_OK=qw(); sub versioncmp( $$ ) { my @A = ($_[0] =~ /([-.]|\d+|[^-.\d]+)/g); my @B = ($_[1] =~ /([-.]|\d+|[^-.\d]+)/g); my ($A, $B); while (@A and @B) { $A = shift @A; $B = shift @B; if ($A eq '-' and $B eq '-') { next; } elsif ( $A eq '-' ) { return -1; } elsif ( $B eq '-') { return 1; } elsif ($A eq '.' and $B eq '.') { next; } elsif ( $A eq '.' ) { return -1; } elsif ( $B eq '.' ) { return 1; } elsif ($A =~ /^\d+$/ and $B =~ /^\d+$/) { if ($A =~ /^0/ || $B =~ /^0/) { return $A cmp $B if $A cmp $B; } else { return $A <=> $B if $A <=> $B; } } else { $A = uc $A; $B = uc $B; return $A cmp $B if $A cmp $B; } } @A <=> @B; } sub versions() { my $callerpkg = (caller)[0]; my $caller_a = "${callerpkg}::a"; my $caller_b = "${callerpkg}::b"; no strict 'refs'; return versioncmp($$caller_a, $$caller_b); } =head1 NAME Sort::Versions - a perl 5 module for sorting of revision-like numbers =head1 SYNOPSIS use Sort::Versions; @l = sort { versioncmp($a, $b) } qw( 1.2 1.2.0 1.2a.0 1.2.a 1.a 02.a ); ... use Sort::Versions; print 'lower' if versioncmp('1.2', '1.2a') == -1; ... use Sort::Versions; %h = (1 => 'd', 2 => 'c', 3 => 'b', 4 => 'a'); @h = sort { versioncmp($h{$a}, $h{$b}) } keys %h; =head1 DESCRIPTION Sort::Versions allows easy sorting of mixed non-numeric and numeric strings, like the 'version numbers' that many shared library systems and revision control packages use. This is quite useful if you are trying to deal with shared libraries. It can also be applied to applications that intersperse variable-width numeric fields within text. Other applications can undoubtedly be found. For an explanation of the algorithm, itE<39>s simplest to look at these examples: 1.1 < 1.2 1.1a < 1.2 1.1 < 1.1.1 1.1 < 1.1a 1.1.a < 1.1a 1 < a a < b 1 < 2 1.1-3 < 1.1-4 1.1-5 < 1.1.6 More precisely (but less comprehensibly), the two strings are treated as subunits delimited by periods or hyphens. Each subunit can contain any number of groups of digits or non-digits. If digit groups are being compared on both sides, a numeric comparison is used, otherwise a ASCII ordering is used. A group or subgroup with more units will win if all comparisons are equal. A period binds digit groups together more tightly than a hyphen. Some packages use a different style of version numbering: a simple real number written as a decimal. Sort::Versions has limited support for this style: when comparing two subunits which are both digit groups, if either subunit has a leading zero, then both are treated like digits after a decimal point. So for example: 0002 < 1 1.06 < 1.5 This wonE<39>t always work, because there wonE<39>t always be a leading zero in real-number style version numbers. There is no way for Sort::Versions to know which style was intended. But a lot of the time it will do the right thing. If you are making up version numbers, the style with (possibly) more than one dot is the style to use. =head1 USAGE The function C takes two arguments and compares them like C. With perl 5.6 or later, you can also use this function directly in sorting: @l = sort versioncmp qw(1.1 1.2 1.0.3); The function C can be used directly as a sort function even on perl 5.005 and earlier, but its use is deprecated. =head1 AUTHOR Ed Avis and Matt Johnson for recent releases; the original author is Kenneth J. Albanowski . Thanks to Hack Kampbjørn and Slaven Rezic for patches and bug reports. Copyright (c) 1996, Kenneth J. Albanowski. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1; Sort-Versions-1.5/t/0040755000103200010320000000000007722241514012113 5ustar ededSort-Versions-1.5/t/versions.t0100755000103200010320000000344507722236540014161 0ustar eded#!/usr/bin/perl #$Id: versions.t,v 1.9 2003/08/24 22:33:03 ed Exp $ use strict; use Sort::Versions; use Test::More; my @tests; while() { if(/^\s*(\S+)\s*([<>])\s*(\S+)\s*$/) { push @tests, $1,$3 if $2 eq "<"; push @tests, $3,$1 if $2 eq ">"; } } plan tests => (@tests / 2 * 3) + 3; my @l = sort versions qw(1.2 1.2a); is($l[0], "1.2"); @l = sort { versioncmp($a, $b) } qw(1.2 1.2a); is($l[0], "1.2"); SKIP: { skip "requires perl 5.6.0", 1 unless ($] >= 5.006); @l = sort versioncmp qw(1.2 1.2a); is($l[0], "1.2"); } my $i=4; while (@tests) { ($a, $b) = @tests[0, 1]; # Test both the versioncmp() and versions() interfaces, in both # the main package and other packages. # is(versions(), -1, "versions($a, $b)"); $i++; is(versioncmp($a, $b), -1, "versioncmp($a, $b)"); $i++; undef $a; undef $b; # just in case eval { package Foo; use Sort::Versions; ($a, $b) = @tests[0, 1]; if (versions() != -1) { die "failed versions() in foreign package"; } if (versioncmp($a, $b) != -1) { die "failed versioncmp() in foreign package"; } }; if ($@) { fail($@); } else { pass("foreign package tests ($tests[0], $tests[1])"); } shift @tests; shift @tests; } __END__ # Simple . only tests 1.2 < 1.3 1.2 < 1.2.1 1.2.1 < 1.3 1.2 < 1.2a 1.2a < 1.3 1.2 < 1.2.b 1.2.1 < 1.2a 1.2.b < 1.2a # Assorted non-numerics a < b a < a.b a.b < a.c a.1 < a.a 1 < a 1a < a 1a < 2 # Null version point 1..1 < 1.1.1 # Leading 0 tests 1 > 0002 1.5 > 1.06 # Handling mixed -. versions 1 < 1-1 1-1 < 1-2 1-2 < 1.2 1-2 < 1.0-1 1-2 < 1.0 1-2 < 1.3 1.2-1 < 1.2a-1 1.3-4.6-7 < 1.3-4.8 1.3-4.6-7 < 1.3-4.6.7 1.3-4a-7 < 1.3-4a-7.4 # 'Bug' reported by pgw99 1.2-1 < 1.2.1-1 1.2.1-1 < 1.2.1-2 1.2.1-2 < 1.3.0-1 Sort-Versions-1.5/Changes0100644000103200010320000000534407722241514013146 0ustar eded2003-08-24 23:58 ed * README, Versions.pm: Version 1.5. 2003-08-24 23:57 ed * mkdist: Changed to 'mkdist' written in Perl; now checks version numbers. 2003-08-24 23:43 ed * Makefile.PL: Removed DISTNAME - change to Perl standard convention of calling the package Sort-Versions-xxx.tar.gz. Though I don't know quite what DISTNAME did. 2003-08-24 23:41 ed * Makefile.PL: Get the version number from Versions.pm rather than hardcoding it in Makefile.PL. 2003-08-24 23:37 ed * Versions.pm: Removed CVS log; I'm not that keen on it, especially as most of the log messages were empty. 2003-08-24 23:33 ed * t/versions.t: Use Test::More (based on patch from mwj99). 2003-08-24 23:25 ed * t/versions.t: Added some more test cases and commented the existing tests. I think this partly follows a patch from mwj99. 2002-09-01 19:20 ed * README: Some revisions and modernizing prompted by Matt's new version (although I didn't incorporate all his changes). 2002-09-01 15:03 ed * Versions.pm: Updated my email address. 2002-03-09 18:19 ed * t/versions.t: Added some tests for calling versioncmp directly, if perl >= 5.6. 2002-03-09 18:19 ed * Versions.pm: Made versions() deprecated, so versioncmp() is the routine to call. Small code tidying. 2002-03-09 17:38 ed * t/versions.t: Run each test in package main and package Foo. 2002-03-09 17:26 ed * Versions.pm, t/versions.t: Applied patch from Slaven Rezic to let versions() work when called from a package other than main. But this is not the final answer, I intend to deprecate versions() and move the code into versioncmp(), which has saner argument passing (not the magic $a and $b). 2002-01-28 19:06 ed * Versions.pm: Version 1.3: patch from Hack Kampbjørn for '-' digit groupings as well as '.'. 2002-01-28 19:05 ed * README: Added some rather lame examples of -. 2002-01-28 19:03 ed * t/versions.t: Added some tests for - digit grouping. 2001-07-28 18:07 ed * t/versions.t: Whoops - got the leading-zero tests the wrong way round. 2001-07-28 18:07 ed * README: Whoops - got the leading-zero examples the wrong way round in the README. 2001-07-28 18:02 ed * mkdist: Modified from Lingua::Preferred to Sort::Versions. 2001-07-28 17:52 ed * Versions.pm: Added $VERSION. 2001-07-28 17:40 ed * mkdist: Initial revision 2001-07-28 17:34 ed * t/versions.t: Added tests for leading-zero numeric comparisons. 2001-07-28 17:33 ed * Versions.pm: Added support for numeric comparisons where one version number has a leading zero. 2001-07-28 17:32 ed * README: Fixed spelling mistake. 2001-07-28 17:32 ed * Makefile.PL: Version 1.2. 2001-07-27 23:27 ed * MANIFEST, Makefile.PL, README, Versions.pm, t/versions.t: Initial revision