1
package Pandoc::Version;
10
Pandoc::Version - version number of pandoc and its libraries
14
our $VERSION = '0.6.0';
16
use overload '""' => 'string', '0+' => 'number',
17
cmp => 'cmp', '<=>' => 'cmp', fallback => 1;
19
use Scalar::Util qw(reftype blessed);
21
our @CARP_NOT = ('Pandoc');
26
# We accept array or string input
27
# (or mixed but let's not document that!)
31
$num =~ /^\d+$/ or croak 'invalid version number';
32
$num =~ s/^0+(?=\d)//; # ensure decimal interpretation
36
map { s/^v//i; split /\./ } ## no critic
37
map { 'ARRAY' CORE::eq (reftype $_ // "") ? @$_ : $_ }
40
croak 'invalid version number' unless @nums;
42
return bless \@nums => $class;
45
sub string { join '.', @{ $_[0] } }
48
my ($major, @minors) = @{ $_[0] };
49
no warnings qw(uninitialized numeric);
51
my $minor = join '', map { sprintf '%03d', $_ } @minors;
52
return 0+ "$major.$minor"; # return a true number
59
(blessed $_ and $_->isa('Pandoc::Version'))
60
? $_ : Pandoc::Version->new($_ // ())
62
return $a->number <=> $b->number;
66
my ($a, $b) = map { Pandoc::Version->new($_) } @_;
67
pop @$a while @$a > @$b;
68
pop @$b while @$b > @$a;
70
return $a->number == $b->number;
73
my %cmp_truth_table = (
83
my ($self, $req) = @_;
86
my @parts = split qr{\s*,\s*}, $req;
87
for my $part (@parts) {
88
my ($op, $ver) = $part =~ m{^\s*(==|>=|>|<=|<|!=)?\s*v?(\d+(\.\d+)*)$};
89
croak "invalid version requirement: $req" unless defined $ver;
91
my $cmp = $self->cmp($ver) + 1; # will be 0 for <, 1 for ==, 2 for >
92
return unless $cmp_truth_table{$op || '>='}->[$cmp];
100
return [ map { 0+ $_ } @$self ];
109
$version = Pandoc::Version->new("1.17.2"); # create version
110
$version = bless [1,17,2], 'Pandoc::Version'; # equivalent
112
"$version"; # stringify to "1.17.2"
113
$version > 1.9; # compare
114
$version->[0]; # major
115
$version->[1]; # minor
117
$version->match('1.17'); # true for 1.17, 1.17.x, 1.17.x.y...
121
This module is used to store and compare version numbers of pandoc executable
122
and Haskell libraries compiled into pandoc. A Pandoc::Version object is an
123
array reference of one or more non-negative integer values.
125
In most cases there is no need to create Pandoc::Version objects by hand. Just
126
use the instances returned by methods C<version> and C<libs> of module
127
L<Pandoc> and trust in overloading.
133
Return a string representation of a version, for instance C<"1.17.0.4">. This
134
method is automatically called by overloading in string context.
138
Return a number representation of a version, for instance C<1.017000004>. This
139
method is automatically called by overloading in number context.
141
=head2 cmp( $version )
143
Compare two version numbers. This is method is used automatically by
144
overloading to compare version objects with strings or numbers (operators
145
C<eq>, C<lt>, C<le>, C<ge>, C<==>, C<< < >>, C<< > >>, C<< <= >>, and C<< >=
148
=head2 match( $version )
150
Return whether a version number matches another version number if cut to the
151
same number of parts. For instance C<1.2.3> matches C<1>, C<1.2>, and C<1.2.3>.
153
=head2 fulfills( $version_requirement )
155
Return whether a version number fullfills a version requirement, such as
156
C<=1.16, !=1.17>'. See L<CPAN::Meta::Spec/Version Ranges> for possible values.
160
Return an array reference of the version number to serialize in JSON format.
164
L<version> is a similar module for Perl version numbers.
166
L<SemVer> extends versions to Semantic Versioning as described at L<http://semver.org/>.