Skip to content

Commit fcb3eb9

Browse files
committed
Add tuple method
1 parent e38397a commit fcb3eb9

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This is not a complete list of changes. See repository for full details:
55
https://github.com/Perl/version.pm
66

77
* Add to_decimal and to_dotted_decimal methods
8+
* Add tuple method
89

910
0.9931
1011
* Fix definitions of LIKELY and UNLIKELY on older perls

lib/version.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ our (@ISA, $STRICT, $LAX);
3030
*version::normal = \&version::vpp::normal;
3131
*version::to_decimal = \&version::vpp::to_decimal;
3232
*version::to_dotted_decimal = \&version::vpp::to_dotted_decimal;
33+
*version::tuple = \&version::vpp::tuple;
3334
if ($] >= 5.009000) {
3435
no strict 'refs';
3536
*version::stringify = \&version::vpp::stringify;
@@ -51,6 +52,7 @@ our (@ISA, $STRICT, $LAX);
5152
*version::normal = \&version::vxs::normal;
5253
*version::to_decimal = \&version::vxs::to_decimal;
5354
*version::to_dotted_decimal = \&version::vxs::to_dotted_decimal;
55+
*version::tuple = \&version::vxs::tuple;
5456
if ($] >= 5.009000) {
5557
no strict 'refs';
5658
*version::stringify = \&version::vxs::stringify;

lib/version.pod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ This returns a new version object for the normalized version, much like C<< vers
283283

284284
version->parse('1.002')->to_dotted_decimal; # v1.2.0
285285

286+
=head2 tuple()
287+
288+
This turns the components of the version into a list. E.g.
289+
290+
version->parse('1.2.3')->tuple; # (1, 2, 3)
291+
286292
=head2 stringify()
287293

288294
Returns a string that is as close to the original representation as possible.
@@ -294,6 +300,11 @@ a version object is interpolated into a string.
294300
version->parse('1.200')->stringify; # 1.2
295301
version->parse(1.02_30)->stringify; # 1.023
296302

303+
=head2 tuple
304+
305+
Returns an array of non-negative integers that is used for comparison purposes with
306+
other version objects.
307+
297308
=head1 EXPORTED FUNCTIONS
298309

299310
=head2 qv()

t/coretests.pm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ sub BaseTests {
4949
$version = $CLASS->$method("v1.2.3_4");
5050
is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
5151

52+
my $version = $CLASS->$method("v1.2.3.4");
53+
is_deeply ([ $version->tuple ], [1, 2, 3, 4], 'Tuple seems to work');
54+
5255
# test illegal formats
5356
eval {my $version = $CLASS->$method("1.2_3_4")};
5457
like($@, qr/multiple underscores/,

vperl/vpp.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,10 @@ sub is_qv {
875875
return (exists $self->{qv});
876876
}
877877

878+
sub tuple {
879+
my ($self) = @_;
880+
return @{ $self->{version} };
881+
}
878882

879883
sub _verify {
880884
my ($self) = @_;

vutil/vxs.inc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
{VXS_CLASS "::qv", VXSp(version_qv), VXSXSDP(NULL)},
7373
{VXS_CLASS "::declare", VXSp(version_qv), VXSXSDP(NULL)},
7474
{VXS_CLASS "::is_qv", VXSp(version_is_qv), VXSXSDP(NULL)},
75+
{VXS_CLASS "::tuple", VXSp(version_tuple), VXSXSDP(NULL)},
7576
#else
7677

7778
#ifndef dVAR
@@ -479,4 +480,33 @@ VXS(version_is_qv)
479480
S_version_check_key(aTHX_ cv, "qv", 2);
480481
}
481482

483+
VXS(version_tuple)
484+
{
485+
dXSARGS;
486+
if (items != 1)
487+
croak_xs_usage(cv, "lobj");
488+
SP -= items;
489+
{
490+
SV * lobj;
491+
int i;
492+
VTYPECHECK(lobj, ST(0), "lobj");
493+
494+
SV** avptr = hv_fetchs(MUTABLE_HV(lobj), "version", 0);
495+
if (!avptr || !SvROK(*avptr) || SvTYPE(SvRV(*avptr)) != SVt_PVAV) {
496+
PUTBACK;
497+
return;
498+
}
499+
AV* version = MUTABLE_AV(SvRV(*avptr));
500+
for (i = 0; i < av_count(version); ++i) {
501+
SV** svptr = av_fetch(version, i, 0);
502+
if (!svptr || !*svptr) {
503+
PUTBACK;
504+
return;
505+
}
506+
XPUSHs(*svptr);
507+
}
508+
PUTBACK;
509+
}
510+
}
511+
482512
#endif

0 commit comments

Comments
 (0)