This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Extract duplicate common subs into common file
[perl5.git] / dist / Devel-PPPort / parts / inc / inctools
CommitLineData
3976648e
KW
1# These are tools that must be included in ppport.h. It doesn't work if given
2# a .pl suffix
3
4sub format_version
5{
6 my $ver = shift;
7
8 $ver =~ s/$/000000/;
9 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
10
11 $v = int $v;
12 $s = int $s;
13
14 if ($r < 5 || ($r == 5 && $v < 6)) {
15 if ($s % 10) {
16 die "invalid version '$ver'\n";
17 }
18 $s /= 10;
19
20 $ver = sprintf "%d.%03d", $r, $v;
21 $s > 0 and $ver .= sprintf "_%02d", $s;
22
23 return $ver;
24 }
25
26 return sprintf "%d.%d.%d", $r, $v, $s;
27}
28
29sub parse_version
30{
31 my $ver = shift;
32
33 if ($ver =~ /^(\d+)\.(\d+)\.(\d+)$/) {
34 return ($1, $2, $3);
35 }
36 elsif ($ver !~ /^\d+\.\d{3}(?:_\d{2})?$/) {
37 die "cannot parse version '$ver'\n";
38 }
39
40 $ver =~ s/_//g;
41 $ver =~ s/$/000000/;
42
43 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
44
45 $v = int $v;
46 $s = int $s;
47
48 if ($r < 5 || ($r == 5 && $v < 6)) {
49 if ($s % 10) {
50 die "cannot parse version '$ver'\n";
51 }
52 $s /= 10;
53 }
54
55 return ($r, $v, $s);
56}
57
581;