Blame SOURCES/constant-1.33-update.patch

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