$v = version->new(1.002); # 1.002, but compares like 1.2.0
$v = version->new(1.002003); # 1.002003
- $v2 = version->new( "1.2.3"); # v1.2.3
+ $v2 = version->new("1.2.3"); # v1.2.3
In specific, version numbers initialized as L<Numeric Versions> will
stringify in Numeric form. Version numbers initialized as L<Extended Versions>
# Prints
$v = version->new( "v1.200"); # v1.200.0
$v = version->new("v1.20.0"); # v1.20.0
- $v = qv("v1.2.3); # v1.2.3
+ $v = qv("v1.2.3"); # v1.2.3
$v = qv("1.2.3"); # v1.2.3
$v = qv("1.20"); # v1.20.0
Overloading has been used with version objects to provide a natural
interface for their use. All mathematical operations are forbidden,
-since they don't make any sense for base version objects.
+since they don't make any sense for base version objects. Consequently,
+there is no overloaded numification available. If you want to use a
+version object in a numeric context for some reason, see the L<numify>
+object method.
=over 4
or as an object method:
$v1 = version->new(12.3);
+ $v2 = $v1->new(12.3);
+
+and in each case, $v1 and $v2 will be identical. NOTE: if you create
+a new object using an existing object like this:
+
$v2 = $v1->new();
-and in each case, $v1 and $v2 will be identical.
+the new object B<will not> be a clone of the existing object. In the
+example case, $v2 will be an empty object of the same type as $v1.
=back
$vs1 = 1.2.3; # encoded as \1\2\3
$vs2 = v1.2; # encoded as \1\2
-However, the use of v-strings to initialize version objects with this
-module is only possible with Perl 5.8.1 or better (which contain special
-code to enable it). Their use is B<strongly> discouraged in all
-circumstances (especially the leading 'v' style), since the meaning will
-change depending on which Perl you are running. It is better to directly
-use L<"Extended Versions"> to ensure the proper interpretation.
+However, the use of bare v-strings to initialize version objects is
+B<strongly> discouraged in all circumstances (especially the leading
+'v' style), since the meaning will change depending on which Perl you
+are running. It is better to directly use L<"Extended Versions"> to
+ensure the proper interpretation.
+
+If you insist on using bare v-strings with Perl > 5.6.0, be aware of the
+following limitations:
+1) For Perl releases 5.6.0 through 5.8.0, the v-string code merely guesses,
+based on some characteristics of v-strings. You B<must> use a three part
+version, e.g. 1.2.3 or v1.2.3 in order for this heuristic to be successful.
+
+2) For Perl releases 5.8.1 and later, v-strings have changed in the Perl
+core to be magical, which means that the version.pm code can automatically
+determine whether the v-string encoding was used.
=head2 Types of Versions Objects
$newver = version->new("12.3.1_1");
print $newver; # v12.3.1_1
+=back
+
=head2 Replacement UNIVERSAL::VERSION
In addition to the version objects, this modules also replaces the core
"Version string contains invalid data; ignoring");
# from here on out capture the warning and test independently
+ {
my $warning;
local $SIG{__WARN__} = sub { $warning = $_[0] };
$version = $CLASS->new("99 and 44/100 pure");
# test reformed UNIVERSAL::VERSION
diag "Replacement UNIVERSAL::VERSION tests" if $Verbose;
+
+ my $error_regex = $] < 5.006
+ ? 'version \d required'
+ : 'does not define \$...::VERSION';
- # we know this file is here since we require it ourselves
- $version = $Test::More::VERSION;
- eval "use Test::More $version";
- unlike($@, qr/Test::More version $version/,
- 'Replacement eval works with exact version');
-
- # test as class method
- $new_version = Test::More->VERSION;
- cmp_ok($new_version,'cmp',$version, "Called as class method");
-
- # this should fail even with old UNIVERSAL::VERSION
- $version = $Test::More::VERSION+0.01;
- eval "use Test::More $version";
- like($@, qr/Test::More version $version/,
- 'Replacement eval works with incremented version');
-
- TODO: {
- local $TODO = "Test fails with Test::More versions ending in _0X";
- $version =~ s/\.0$//; #convert to string and remove trailing '.0'
- chop($version); # shorten by 1 digit, should still succeed
- eval "use Test::More $version";
- unlike($@, qr/Test::More version $version/,
- 'Replacement eval works with single digit');
- }
+ {
+ open F, ">aaa.pm" or die "Cannot open aaa.pm: $!\n";
+ print F "package aaa;\n\$aaa::VERSION=0.58;\n1;\n";
+ close F;
+
+ $version = 0.58; $version = sprintf("%.3f",$version);
+ eval "use lib '.'; use aaa $version";
+ unlike($@, qr/aaa version $version/,
+ 'Replacement eval works with exact version');
+
+ # test as class method
+ $new_version = "aaa"->VERSION;
+ cmp_ok($new_version,'eq',$version, "Called as class method");
- $version += 0.1; # this would fail with old UNIVERSAL::VERSION
- eval "use Test::More $version";
- like($@, qr/Test::More version $version/,
- 'Replacement eval works with incremented digit');
+ eval "print Completely::Unknown::Module->VERSION";
+ if ( $] < 5.008 ) {
+ unlike($@, qr/$error_regex/,
+ "Don't freak if the module doesn't even exist");
+ }
+ else {
+ unlike($@, qr/defines neither package nor VERSION/,
+ "Don't freak if the module doesn't even exist");
+ }
+
+ # this should fail even with old UNIVERSAL::VERSION
+ $version += 0.01; $version = sprintf("%.3f",$version);
+ eval "use lib '.'; use aaa $version";
+ like($@, qr/aaa version $version/,
+ 'Replacement eval works with incremented version');
+
+ $version =~ s/0+$//; #convert to string and remove trailing 0's
+ chop($version); # shorten by 1 digit, should still succeed
+ eval "use lib '.'; use aaa $version";
+ unlike($@, qr/aaa version $version/,
+ 'Replacement eval works with single digit');
+
+ # this would fail with old UNIVERSAL::VERSION
+ $version += 0.1; $version = sprintf("%.3f",$version);
+ eval "use lib '.'; use aaa $version";
+ like($@, qr/aaa version $version/,
+ 'Replacement eval works with incremented digit');
+ unlink 'aaa.pm';
+ }
{ # dummy up some variously broken modules for testing
open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
print F "1;\n";
close F;
- my $error_regex;
+
+ eval "use lib '.'; use xxx 3;";
if ( $] < 5.008 ) {
- $error_regex = 'xxx does not define \$xxx::VERSION';
+ like($@, qr/$error_regex/,
+ 'Replacement handles modules without package or VERSION');
}
else {
- $error_regex = 'xxx defines neither package nor VERSION';
+ like($@, qr/defines neither package nor VERSION/,
+ 'Replacement handles modules without package or VERSION');
}
-
- eval "use lib '.'; use xxx 3;";
- like ($@, qr/$error_regex/,
- 'Replacement handles modules without package or VERSION');
- eval "use lib '.'; use xxx; $version = xxx->VERSION";
+ eval "use lib '.'; use xxx; \$version = xxx->VERSION";
unlike ($@, qr/$error_regex/,
'Replacement handles modules without package or VERSION');
- ok (defined($version), "Called as class method");
+ ok (!defined($version), "Called as class method");
unlink 'xxx.pm';
}
print F "package yyy;\n#look ma no VERSION\n1;\n";
close F;
eval "use lib '.'; use yyy 3;";
- like ($@, qr/^yyy does not define \$yyy::VERSION/,
+ like ($@, qr/$error_regex/,
'Replacement handles modules without VERSION');
eval "use lib '.'; use yyy; print yyy->VERSION";
- unlike ($@, qr/^yyy does not define \$yyy::VERSION/,
+ unlike ($@, qr/$error_regex/,
'Replacement handles modules without VERSION');
unlink 'yyy.pm';
}
print F "package zzz;\n\@VERSION = ();\n1;\n";
close F;
eval "use lib '.'; use zzz 3;";
- like ($@, qr/^zzz does not define \$zzz::VERSION/,
+ like ($@, qr/$error_regex/,
'Replacement handles modules without VERSION');
eval "use lib '.'; use zzz; print zzz->VERSION";
- unlike ($@, qr/^zzz does not define \$zzz::VERSION/,
+ unlike ($@, qr/$error_regex/,
'Replacement handles modules without VERSION');
unlink 'zzz.pm';
}
$version = $CLASS->new(0.000001);
unlike($warning, qr/^Version string '1e-06' contains invalid data/,
"Very small version objects");
+ }
SKIP: {
# dummy up a legal module for testing RT#19017
SKIP: {
# test locale handling
+ my $warning;
+ local $SIG{__WARN__} = sub { $warning = $_[0] };
my $ver = 1.23; # has to be floating point number
my $loc;
while (<DATA>) {
is ($v, "1.230", "Locale doesn't apply to version objects");
ok ($v == $ver, "Comparison to locale floating point");
}
+
+ eval 'my $v = $CLASS->new("1._1");';
+ unlike($@, qr/^Invalid version format \(alpha with zero width\)/,
+ "Invalid version format 1._1");
}
1;