From 39eded6e46ca29c86b28b0b524e9ca6d069c6148 Mon Sep 17 00:00:00 2001 From: Tuomas Jormola Date: Fri, 27 May 2016 12:42:47 +0300 Subject: [PATCH] Use version objects when handling version numbers Besides using decimal numbers as package version number, it's also possible to use the dotted-decimal form (i.e. vX.Y.Z) in Perl. Currently, MooseX::Storage uses direct numeric comparison of the version strings which doesn't really work and it also spews "Argument ... isn't numeric in ..." warnings should the class define its version in dotted-decimal form. With this patch we're now creating version objects from the version strings, which accepts both decimal and dotted-decimal version formats as input and handles the comparison correctly. --- lib/MooseX/Storage/Engine.pm | 40 +++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index d35be0f..167181c 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -6,6 +6,7 @@ our $VERSION = '0.52'; use Moose; use Scalar::Util qw(refaddr blessed); use Carp 'confess'; +use version 0.77; use namespace::autoclean; # the class marker when @@ -162,28 +163,29 @@ my %OBJECT_HANDLERS = ( (exists $data->{$CLASS_MARKER}) || confess "Serialized item has no class marker"; # check the class more thoroughly here ... - my ($class, $version, $authority) = (split '-' => $data->{$CLASS_MARKER}); + my ($class, $version_string, $authority) = (split '-' => $data->{$CLASS_MARKER}); my $meta = eval { $class->meta }; confess "Class ($class) is not loaded, cannot unpack" if $@; - if ($options->{check_version}) { - my $meta_version = $meta->version; - if (defined $meta_version && $version) { - if ($options->{check_version} eq 'allow_less_than') { - ($meta_version <= $version) - || confess "Class ($class) versions is not less than currently available." - . " got=($version) available=($meta_version)"; - } - elsif ($options->{check_version} eq 'allow_greater_than') { - ($meta->version >= $version) - || confess "Class ($class) versions is not greater than currently available." - . " got=($version) available=($meta_version)"; - } - else { - ($meta->version == $version) - || confess "Class ($class) versions don't match." - . " got=($version) available=($meta_version)"; - } + if ($options->{check_version} && + defined $version_string && + defined $meta->version) { + my $version = version->parse($version_string); + my $meta_version = version->parse($meta->version); + if ($options->{check_version} eq 'allow_less_than') { + ($meta_version <= $version) + || confess "Class ($class) versions is not less than currently available." + . " got=($version) available=($meta_version)"; + } + elsif ($options->{check_version} eq 'allow_greater_than') { + ($meta_version >= $version) + || confess "Class ($class) versions is not greater than currently available." + . " got=($version) available=($meta_version)"; + } + else { + ($meta_version == $version) + || confess "Class ($class) versions don't match." + . " got=($version) available=($meta_version)"; } }