Blame SOURCES/constant-1.33-update.patch

b382ef
diff -up constant-1.27/lib/constant.pm.127 constant-1.27/lib/constant.pm
b382ef
--- constant-1.27/lib/constant.pm.127	2015-04-27 13:39:46.613767559 +0200
b382ef
+++ constant-1.27/lib/constant.pm	2015-01-27 23:47:42.000000000 +0100
b382ef
@@ -3,8 +3,8 @@ use 5.008;
b382ef
 use strict;
b382ef
 use warnings::register;
b382ef
 
b382ef
-use vars qw($VERSION %declared);
b382ef
-$VERSION = '1.27';
b382ef
+our $VERSION = '1.33';
b382ef
+our %declared;
b382ef
 
b382ef
 #=======================================================================
b382ef
 
b382ef
@@ -24,13 +24,24 @@ my $boolean = qr/^[01]?\z/;
b382ef
 BEGIN {
b382ef
     # We'd like to do use constant _CAN_PCS => $] > 5.009002
b382ef
     # but that's a bit tricky before we load the constant module :-)
b382ef
-    # By doing this, we save 1 run time check for *every* call to import.
b382ef
-    no strict 'refs';
b382ef
+    # By doing this, we save several run time checks for *every* call
b382ef
+    # to import.
b382ef
     my $const = $] > 5.009002;
b382ef
-    *_CAN_PCS = sub () {$const};
b382ef
-
b382ef
     my $downgrade = $] < 5.015004; # && $] >= 5.008
b382ef
-    *_DOWNGRADE = sub () { $downgrade };
b382ef
+    my $constarray = exists &_make_const;
b382ef
+    if ($const) {
b382ef
+	Internals::SvREADONLY($const, 1);
b382ef
+	Internals::SvREADONLY($downgrade, 1);
b382ef
+	$constant::{_CAN_PCS}   = \$const;
b382ef
+	$constant::{_DOWNGRADE} = \$downgrade;
b382ef
+	$constant::{_CAN_PCS_FOR_ARRAY} = \$constarray;
b382ef
+    }
b382ef
+    else {
b382ef
+	no strict 'refs';
b382ef
+	*{"_CAN_PCS"}   = sub () {$const};
b382ef
+	*{"_DOWNGRADE"} = sub () { $downgrade };
b382ef
+	*{"_CAN_PCS_FOR_ARRAY"} = sub () { $constarray };
b382ef
+    }
b382ef
 }
b382ef
 
b382ef
 #=======================================================================
b382ef
@@ -46,13 +57,13 @@ sub import {
b382ef
     return unless @_;			# Ignore 'use constant;'
b382ef
     my $constants;
b382ef
     my $multiple  = ref $_[0];
b382ef
-    my $pkg = caller;
b382ef
+    my $caller = caller;
b382ef
     my $flush_mro;
b382ef
     my $symtab;
b382ef
 
b382ef
     if (_CAN_PCS) {
b382ef
 	no strict 'refs';
b382ef
-	$symtab = \%{$pkg . '::'};
b382ef
+	$symtab = \%{$caller . '::'};
b382ef
     };
b382ef
 
b382ef
     if ( $multiple ) {
b382ef
@@ -70,6 +81,20 @@ sub import {
b382ef
     }
b382ef
 
b382ef
     foreach my $name ( keys %$constants ) {
b382ef
+	my $pkg;
b382ef
+	my $symtab = $symtab;
b382ef
+	my $orig_name = $name;
b382ef
+	if ($name =~ s/(.*)(?:::|')(?=.)//s) {
b382ef
+	    $pkg = $1;
b382ef
+	    if (_CAN_PCS && $pkg ne $caller) {
b382ef
+		no strict 'refs';
b382ef
+		$symtab = \%{$pkg . '::'};
b382ef
+	    }
b382ef
+	}
b382ef
+	else {
b382ef
+	    $pkg = $caller;
b382ef
+	}
b382ef
+
b382ef
 	# Normal constant name
b382ef
 	if ($name =~ $normal_constant_name and !$forbidden{$name}) {
b382ef
 	    # Everything is okay
b382ef
@@ -117,7 +142,7 @@ sub import {
b382ef
 	    my $full_name = "${pkg}::$name";
b382ef
 	    $declared{$full_name}++;
b382ef
 	    if ($multiple || @_ == 1) {
b382ef
-		my $scalar = $multiple ? $constants->{$name} : $_[0];
b382ef
+		my $scalar = $multiple ? $constants->{$orig_name} : $_[0];
b382ef
 
b382ef
 		if (_DOWNGRADE) { # for 5.8 to 5.14
b382ef
 		    # Work around perl bug #31991: Sub names (actually glob
b382ef
@@ -128,27 +153,50 @@ sub import {
b382ef
 
b382ef
 		# The constant serves to optimise this entire block out on
b382ef
 		# 5.8 and earlier.
b382ef
-		if (_CAN_PCS && $symtab && !exists $symtab->{$name}) {
b382ef
-		    # No typeglob yet, so we can use a reference as space-
b382ef
-		    # efficient proxy for a constant subroutine
b382ef
+		if (_CAN_PCS) {
b382ef
+		    # Use a reference as a proxy for a constant subroutine.
b382ef
+		    # If this is not a glob yet, it saves space.  If it is
b382ef
+		    # a glob, we must still create it this way to get the
b382ef
+		    # right internal flags set, as constants are distinct
b382ef
+		    # from subroutines created with sub(){...}.
b382ef
 		    # The check in Perl_ck_rvconst knows that inlinable
b382ef
 		    # constants from cv_const_sv are read only. So we have to:
b382ef
 		    Internals::SvREADONLY($scalar, 1);
b382ef
-		    $symtab->{$name} = \$scalar;
b382ef
-		    ++$flush_mro;
b382ef
+		    if (!exists $symtab->{$name}) {
b382ef
+			$symtab->{$name} = \$scalar;
b382ef
+			++$flush_mro->{$pkg};
b382ef
+		    }
b382ef
+		    else {
b382ef
+			local $constant::{_dummy} = \$scalar;
b382ef
+			*$full_name = \&{"_dummy"};
b382ef
+		    }
b382ef
 		} else {
b382ef
 		    *$full_name = sub () { $scalar };
b382ef
 		}
b382ef
 	    } elsif (@_) {
b382ef
 		my @list = @_;
b382ef
-		*$full_name = sub () { @list };
b382ef
+		if (_CAN_PCS_FOR_ARRAY) {
b382ef
+		    _make_const($list[$_]) for 0..$#list;
b382ef
+		    _make_const(@list);
b382ef
+		    if (!exists $symtab->{$name}) {
b382ef
+			$symtab->{$name} = \@list;
b382ef
+			$flush_mro->{$pkg}++;
b382ef
+		    }
b382ef
+		    else {
b382ef
+			local $constant::{_dummy} = \@list;
b382ef
+			*$full_name = \&{"_dummy"};
b382ef
+		    }
b382ef
+		}
b382ef
+		else { *$full_name = sub () { @list }; }
b382ef
 	    } else {
b382ef
 		*$full_name = sub () { };
b382ef
 	    }
b382ef
 	}
b382ef
     }
b382ef
     # Flush the cache exactly once if we make any direct symbol table changes.
b382ef
-    mro::method_changed_in($pkg) if _CAN_PCS && $flush_mro;
b382ef
+    if (_CAN_PCS && $flush_mro) {
b382ef
+	mro::method_changed_in($_) for keys %$flush_mro;
b382ef
+    }
b382ef
 }
b382ef
 
b382ef
 1;
b382ef
@@ -190,7 +238,7 @@ This pragma allows you to declare consta
b382ef
 
b382ef
 When you declare a constant such as C<PI> using the method shown
b382ef
 above, each machine your script runs upon can have as many digits
b382ef
-of accuracy as it can use. Also, your program will be easier to
b382ef
+of accuracy as it can use.  Also, your program will be easier to
b382ef
 read, more likely to be maintained (and maintained correctly), and
b382ef
 far less likely to send a space probe to the wrong planet because
b382ef
 nobody noticed the one equation in which you wrote C<3.14195>.
b382ef
@@ -203,7 +251,7 @@ away if the constant is false.
b382ef
 =head1 NOTES
b382ef
 
b382ef
 As with all C<use> directives, defining a constant happens at
b382ef
-compile time. Thus, it's probably not correct to put a constant
b382ef
+compile time.  Thus, it's probably not correct to put a constant
b382ef
 declaration inside of a conditional statement (like C
b382ef
 { use constant ... }>).
b382ef
 
b382ef
@@ -221,10 +269,6 @@ point to data which may be changed, as t
b382ef
     ARRAY->[1] = " be changed";
b382ef
     print ARRAY->[1];
b382ef
 
b382ef
-Dereferencing constant references incorrectly (such as using an array
b382ef
-subscript on a constant hash reference, or vice versa) will be trapped at
b382ef
-compile time.
b382ef
-
b382ef
 Constants belong to the package they are defined in.  To refer to a
b382ef
 constant defined in another package, specify the full package name, as
b382ef
 in C<Some::Package::CONSTANT>.  Constants may be exported by modules,
b382ef
@@ -233,11 +277,18 @@ as C<< Some::Package->CONSTANT >> or as
b382ef
 C<$obj> is an instance of C<Some::Package>.  Subclasses may define
b382ef
 their own constants to override those in their base class.
b382ef
 
b382ef
+As of version 1.32 of this module, constants can be defined in packages
b382ef
+other than the caller, by including the package name in the name of the
b382ef
+constant:
b382ef
+
b382ef
+    use constant "OtherPackage::FWIBBLE" => 7865;
b382ef
+    constant->import("Other::FWOBBLE",$value); # dynamically at run time
b382ef
+
b382ef
 The use of all caps for constant names is merely a convention,
b382ef
 although it is recommended in order to make constants stand out
b382ef
 and to help avoid collisions with other barewords, keywords, and
b382ef
-subroutine names. Constant names must begin with a letter or
b382ef
-underscore. Names beginning with a double underscore are reserved. Some
b382ef
+subroutine names.  Constant names must begin with a letter or
b382ef
+underscore.  Names beginning with a double underscore are reserved.  Some
b382ef
 poor choices for names will generate warnings, if warnings are enabled at
b382ef
 compile time.
b382ef
 
b382ef
@@ -312,15 +363,15 @@ constants without any problems.
b382ef
 =head1 TECHNICAL NOTES
b382ef
 
b382ef
 In the current implementation, scalar constants are actually
b382ef
-inlinable subroutines. As of version 5.004 of Perl, the appropriate
b382ef
+inlinable subroutines.  As of version 5.004 of Perl, the appropriate
b382ef
 scalar constant is inserted directly in place of some subroutine
b382ef
-calls, thereby saving the overhead of a subroutine call. See
b382ef
+calls, thereby saving the overhead of a subroutine call.  See
b382ef
 L<perlsub/"Constant Functions"> for details about how and when this
b382ef
 happens.
b382ef
 
b382ef
 In the rare case in which you need to discover at run time whether a
b382ef
 particular constant has been declared via this module, you may use
b382ef
-this function to examine the hash C<%constant::declared>. If the given
b382ef
+this function to examine the hash C<%constant::declared>.  If the given
b382ef
 constant name does not include a package name, the current package is
b382ef
 used.
b382ef
 
b382ef
@@ -335,11 +386,12 @@ used.
b382ef
 
b382ef
 =head1 CAVEATS
b382ef
 
b382ef
-In the current version of Perl, list constants are not inlined
b382ef
-and some symbols may be redefined without generating a warning.
b382ef
+List constants are not inlined unless you are using Perl v5.20 or higher.
b382ef
+In v5.20 or higher, they are still not read-only, but that may change in
b382ef
+future versions.
b382ef
 
b382ef
 It is not possible to have a subroutine or a keyword with the same
b382ef
-name as a constant in the same package. This is probably a Good Thing.
b382ef
+name as a constant in the same package.  This is probably a Good Thing.
b382ef
 
b382ef
 A constant with a name in the list C
b382ef
 ENV INC SIG> is not allowed anywhere but in package C<main::>, for
b382ef
diff -up constant-1.27/t/constant.t.127 constant-1.27/t/constant.t
b382ef
--- constant-1.27/t/constant.t.127	2013-03-21 01:48:49.000000000 +0100
b382ef
+++ constant-1.27/t/constant.t	2015-01-24 16:02:08.000000000 +0100
b382ef
@@ -9,7 +9,7 @@ END { @warnings && print STDERR join "\n
b382ef
 
b382ef
 
b382ef
 use strict;
b382ef
-use Test::More tests => 96;
b382ef
+use Test::More tests => 109;
b382ef
 my $TB = Test::More->builder;
b382ef
 
b382ef
 BEGIN { use_ok('constant'); }
b382ef
@@ -122,7 +122,7 @@ print $output CCODE->($curr_test+4);
b382ef
 $TB->current_test($curr_test+4);
b382ef
 
b382ef
 eval q{ CCODE->{foo} };
b382ef
-ok scalar($@ =~ /^Constant is not a HASH/);
b382ef
+ok scalar($@ =~ /^Constant is not a HASH|Not a HASH reference/);
b382ef
 
b382ef
 
b382ef
 # Allow leading underscore
b382ef
@@ -346,3 +346,78 @@ $kloong = 'schlozhauer';
b382ef
     eval 'use constant undef, 5; 1';
b382ef
     like $@, qr/\ACan't use undef as constant name at /;
b382ef
 }
b382ef
+
b382ef
+# Constants created by "use constant" should be read-only
b382ef
+
b382ef
+# This test will not test what we are trying to test if this glob entry
b382ef
+# exists already, so test that, too.
b382ef
+ok !exists $::{immutable};
b382ef
+eval q{
b382ef
+    use constant immutable => 23987423874;
b382ef
+    for (immutable) { eval { $_ = 22 } }
b382ef
+    like $@, qr/^Modification of a read-only value attempted at /,
b382ef
+	'constant created in empty stash slot is immutable';
b382ef
+    eval { for (immutable) { ${\$_} = 432 } };
b382ef
+    SKIP: {
b382ef
+	require Config;
b382ef
+	if ($Config::Config{useithreads}) {
b382ef
+	    skip "fails under threads", 1 if $] < 5.019003;
b382ef
+	}
b382ef
+	like $@, qr/^Modification of a read-only value attempted at /,
b382ef
+	    '... and immutable through refgen, too';
b382ef
+    }
b382ef
+};
b382ef
+() = \&{"immutable"}; # reify
b382ef
+eval 'for (immutable) { $_ = 42 }';
b382ef
+like $@, qr/^Modification of a read-only value attempted at /,
b382ef
+    '... and after reification';
b382ef
+
b382ef
+# Use an existing stash element this time.
b382ef
+# This next line is sufficient to trigger a different code path in
b382ef
+# constant.pm.
b382ef
+() = \%::existing_stash_entry;
b382ef
+use constant existing_stash_entry => 23987423874;
b382ef
+for (existing_stash_entry) { eval { $_ = 22 } }
b382ef
+like $@, qr/^Modification of a read-only value attempted at /,
b382ef
+    'constant created in existing stash slot is immutable';
b382ef
+eval { for (existing_stash_entry) { ${\$_} = 432 } };
b382ef
+SKIP: {
b382ef
+    if ($Config::Config{useithreads}) {
b382ef
+	skip "fails under threads", 1 if $] < 5.019003;
b382ef
+    }
b382ef
+    like $@, qr/^Modification of a read-only value attempted at /,
b382ef
+	'... and immutable through refgen, too';
b382ef
+}
b382ef
+
b382ef
+# Test that list constants are also immutable.  This only works under
b382ef
+# 5.19.3 and later.
b382ef
+SKIP: {
b382ef
+    skip "fails under 5.19.2 and earlier", 3 if $] < 5.019003;
b382ef
+    local $TODO = "disabled for now; breaks CPAN; see perl #119045";
b382ef
+    use constant constant_list => 1..2;
b382ef
+    for (constant_list) {
b382ef
+	my $num = $_;
b382ef
+	eval { $_++ };
b382ef
+	like $@, qr/^Modification of a read-only value attempted at /,
b382ef
+	    "list constant has constant elements ($num)";
b382ef
+    }
b382ef
+    undef $TODO;
b382ef
+    # Whether values are modifiable or no, modifying them should not affect
b382ef
+    # future return values.
b382ef
+    my @values;
b382ef
+    for(1..2) {
b382ef
+	for ((constant_list)[0]) {
b382ef
+	    push @values, $_;
b382ef
+	    eval {$_++};
b382ef
+	}
b382ef
+    }
b382ef
+    is $values[1], $values[0],
b382ef
+	'modifying list const elements does not affect future retavls';
b382ef
+}
b382ef
+
b382ef
+use constant { "tahi" => 1, "rua::rua" => 2, "toru'toru" => 3 };
b382ef
+use constant "wha::wha" => 4;
b382ef
+is tahi, 1, 'unqualified constant declared with constants in other pkgs';
b382ef
+is rua::rua, 2, 'constant declared with ::';
b382ef
+is toru::toru, 3, "constant declared with '";
b382ef
+is wha::wha, 4, 'constant declared by itself with ::';
b382ef
diff -up constant-1.27/t/utf8.t.127 constant-1.27/t/utf8.t