This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Increase $constant::VERSION to 1.28
[perl5.git] / dist / constant / lib / constant.pm
index ef618c4..861d4f0 100644 (file)
@@ -1,10 +1,10 @@
 package constant;
-use 5.005;
+use 5.008;
 use strict;
 use warnings::register;
 
 use vars qw($VERSION %declared);
-$VERSION = '1.23';
+$VERSION = '1.28';
 
 #=======================================================================
 
@@ -17,30 +17,26 @@ my %forced_into_main = map +($_, 1),
 
 my %forbidden = (%keywords, %forced_into_main);
 
-my $str_end = $] >= 5.006 ? "\\z" : "\\Z";
-my $normal_constant_name = qr/^_?[^\W_0-9]\w*$str_end/;
-my $tolerable = qr/^[A-Za-z_]\w*$str_end/;
-my $boolean = qr/^[01]?$str_end/;
+my $normal_constant_name = qr/^_?[^\W_0-9]\w*\z/;
+my $tolerable = qr/^[A-Za-z_]\w*\z/;
+my $boolean = qr/^[01]?\z/;
 
 BEGIN {
     # We'd like to do use constant _CAN_PCS => $] > 5.009002
     # but that's a bit tricky before we load the constant module :-)
     # By doing this, we save 1 run time check for *every* call to import.
-    no strict 'refs';
     my $const = $] > 5.009002;
-    *_CAN_PCS = sub () {$const};
-
-    # Before this makes its way into a dev perl release, we have to do
-    # browser-sniffing, as it were....
-    return unless $const;
-    *{chr 256} = \3;
-    if (exists ${__PACKAGE__."::"}{"\xc4\x80"}) {
-       delete ${__PACKAGE__."::"}{"\xc4\x80"};
-       *_DOWNGRADE = sub () {1};
+    my $downgrade = $] < 5.015004; # && $] >= 5.008
+    if ($const) {
+       Internals::SvREADONLY($const, 1);
+       Internals::SvREADONLY($downgrade, 1);
+       $constant::{_CAN_PCS}   = \$const;
+       $constant::{_DOWNGRADE} = \$downgrade;
     }
     else {
-       delete ${__PACKAGE__."::"}{chr 256};
-       *_DOWNGRADE = sub () {0};
+       no strict 'refs';
+       *{"_CAN_PCS"}   = sub () {$const};
+       *{"_DOWNGRADE"} = sub () { $downgrade };
     }
 }
 
@@ -130,7 +126,7 @@ sub import {
            if ($multiple || @_ == 1) {
                my $scalar = $multiple ? $constants->{$name} : $_[0];
 
-               if (_DOWNGRADE) { # for 5.10 to 5.14
+               if (_DOWNGRADE) { # for 5.8 to 5.14
                    # Work around perl bug #31991: Sub names (actually glob
                    # names in general) ignore the UTF8 flag. So we have to
                    # turn it off to get the "right" symbol table entry.
@@ -139,14 +135,23 @@ sub import {
 
                # The constant serves to optimise this entire block out on
                # 5.8 and earlier.
-               if (_CAN_PCS && $symtab && !exists $symtab->{$name}) {
-                   # No typeglob yet, so we can use a reference as space-
-                   # efficient proxy for a constant subroutine
+               if (_CAN_PCS) {
+                   # Use a reference as a proxy for a constant subroutine.
+                   # If this is not a glob yet, it saves space.  If it is
+                   # a glob, we must still create it this way to get the
+                   # right internal flags set, as constants are distinct
+                   # from subroutines created with sub(){...}.
                    # The check in Perl_ck_rvconst knows that inlinable
                    # constants from cv_const_sv are read only. So we have to:
                    Internals::SvREADONLY($scalar, 1);
-                   $symtab->{$name} = \$scalar;
-                   ++$flush_mro;
+                   if ($symtab && !exists $symtab->{$name}) {
+                       $symtab->{$name} = \$scalar;
+                       ++$flush_mro;
+                   }
+                   else {
+                       local $constant::{_dummy} = \$scalar;
+                       *$full_name = \&{"_dummy"};
+                   }
                } else {
                    *$full_name = sub () { $scalar };
                }
@@ -373,9 +378,6 @@ C<< CONSTANT => 'value' >>.
 
 L<Readonly> - Facility for creating read-only scalars, arrays, hashes.
 
-L<Const> - Facility for creating read-only variables. Similar to C<Readonly>,
-but uses C<SvREADONLY> instead of C<tie>.
-
 L<Attribute::Constant> - Make read-only variables via attribute
 
 L<Scalar::Readonly> - Perl extension to the C<SvREADONLY> scalar flag