Blame SOURCES/constant-1.33-update.patch

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