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