Version change to ExtUtils::MM_Unix missed in change #30380.
[p5sagit/p5-mst-13.2.git] / lib / constant.pm
index 19edca0..f1b4d73 100644 (file)
@@ -5,12 +5,12 @@ use 5.006_00;
 use warnings::register;
 
 our($VERSION, %declared);
-$VERSION = '1.06';
+$VERSION = '1.09';
 
 #=======================================================================
 
 # Some names are evil choices.
-my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD };
+my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK UNITCHECK END DESTROY AUTOLOAD };
 
 my %forced_into_main = map +($_, 1),
     qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG };
@@ -30,6 +30,13 @@ sub import {
     return unless @_;                  # Ignore 'use constant;'
     my $constants;
     my $multiple  = ref $_[0];
+    my $pkg = caller;
+    my $symtab;
+
+    if ($] > 5.009002) {
+       no strict 'refs';
+       $symtab = \%{$pkg . '::'};
+    };
 
     if ( $multiple ) {
        if (ref $_[0] ne 'HASH') {
@@ -46,7 +53,6 @@ sub import {
            require Carp;
            Carp::croak("Can't use undef as constant name");
        }
-       my $pkg = caller;
 
        # Normal constant name
        if ($name =~ /^_?[^\W_0-9]\w*\z/ and !$forbidden{$name}) {
@@ -94,19 +100,24 @@ sub import {
            no strict 'refs';
            my $full_name = "${pkg}::$name";
            $declared{$full_name}++;
-           if ($multiple) {
-               my $scalar = $constants->{$name};
-               *$full_name = sub () { $scalar };
-           } else {
-               if (@_ == 1) {
-                   my $scalar = $_[0];
-                   *$full_name = sub () { $scalar };
-               } elsif (@_) {
-                   my @list = @_;
-                   *$full_name = sub () { @list };
+           if ($multiple || @_ == 1) {
+               my $scalar = $multiple ? $constants->{$name} : $_[0];
+               if ($symtab && !exists $symtab->{$name}) {
+                   # No typeglob yet, so we can use a reference as space-
+                   # efficient proxy for a constant subroutine
+                   # 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;
+                   &Internals::inc_sub_generation;
                } else {
-                   *$full_name = sub () { };
+                   *$full_name = sub () { $scalar };
                }
+           } elsif (@_) {
+               my @list = @_;
+               *$full_name = sub () { @list };
+           } else {
+               *$full_name = sub () { };
            }
        }
     }