public
Created

Comparing some version numbers for a post on http://blogs.perl.org/users/chisel

  • Download Gist
cmp_versions.pl
Perl
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
#!/usr/bin/env perl
use strict;
use warnings;
use feature ':5.12';
 
use version;
use Text::SimpleTable;
 
my @versionstrings = (
'0.0.4',
'0.0.5',
'0.0.5_01',
'0.0.5_50',
'0.0.5_99',
'0.0.5',
'0.0.6',
);
 
my @versions;
foreach my $vstring (@versionstrings) {
push @versions, version->parse($vstring);
}
 
 
my $t1 = Text::SimpleTable->new(
[12, 'Input String'],
[12, 'normal()'],
[14, 'cmp-prev'],
[14, 'cmp-next'],
);
for (my$i=0; $i<@versions; $i++) {
my $cmp_with_prev;
 
if($i>0) {
given ($i) {
when ($versions[$i] < $versions[$i-1]) {
$cmp_with_prev = "< " . $versions[$i-1];
}
when ($versions[$i] == $versions[$i-1]) {
$cmp_with_prev = "== " . $versions[$i-1];
}
when ($versions[$i] > $versions[$i-1]) {
$cmp_with_prev = " > " . $versions[$i-1];
}
}
}
else {
$cmp_with_prev = '';
}
 
my $cmp_with_next;
if($i<@versions-1) {
given ($i) {
when ($versions[$i] < $versions[$i+1]) {
$cmp_with_next = "< " . $versions[$i+1];
}
when ($versions[$i] == $versions[$i+1]) {
$cmp_with_next = "== " . $versions[$i+1];
}
when ($versions[$i] > $versions[$i+1]) {
$cmp_with_next = " > " . $versions[$i+1];
}
}
}
else {
$cmp_with_next = '';
}
 
$t1->row(
$versionstrings[$i],
$versions[$i]->normal,
$cmp_with_prev,
$cmp_with_next,
);
}
 
say $t1->draw;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.