In perl.git, the branch blead has been updated

<http://perl5.git.perl.org/perl.git/commitdiff/c5764f70840f5068e5ce459d0b23d3afa4eb65de?hp=27d8d4f034c50a2472e34bef47d5d2f00c8330c1>

- Log -----------------------------------------------------------------
commit c5764f70840f5068e5ce459d0b23d3afa4eb65de
Author: Nicholas Clark <[email protected]>
Date:   Mon Aug 17 13:15:58 2009 +0100

    Convert perl version check in import() from run time to compile time.

M       lib/constant.pm

commit 672c0ce9f4737708d6146bebe978c334c9647c4d
Author: Nicholas Clark <[email protected]>
Date:   Mon Aug 17 11:51:39 2009 +0100

    Generate perl version-dependent regexps once, rather than every call to 
import.

M       lib/constant.pm
-----------------------------------------------------------------------

Summary of changes:
 lib/constant.pm |   25 +++++++++++++++++++------
 1 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/lib/constant.pm b/lib/constant.pm
index 906b777..b77c085 100644
--- a/lib/constant.pm
+++ b/lib/constant.pm
@@ -4,7 +4,7 @@ use strict;
 use warnings::register;
 
 use vars qw($VERSION %declared);
-$VERSION = '1.17';
+$VERSION = '1.18';
 
 #=======================================================================
 
@@ -17,6 +17,20 @@ 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/;
+
+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};
+}
+
 #=======================================================================
 # import() - import symbols into user's namespace
 #
@@ -32,9 +46,8 @@ sub import {
     my $multiple  = ref $_[0];
     my $pkg = caller;
     my $symtab;
-    my $str_end = $] >= 5.006 ? "\\z" : "\\Z";
 
-    if ($] > 5.009002) {
+    if (_CAN_PCS) {
        no strict 'refs';
        $symtab = \%{$pkg . '::'};
     };
@@ -56,7 +69,7 @@ sub import {
        }
 
        # Normal constant name
-       if ($name =~ /^_?[^\W_0-9]\w*$str_end/ and !$forbidden{$name}) {
+       if ($name =~ $normal_constant_name and !$forbidden{$name}) {
            # Everything is okay
 
        # Name forced into main, but we're not in main. Fatal.
@@ -70,7 +83,7 @@ sub import {
            Carp::croak("Constant name '$name' begins with '__'");
 
        # Maybe the name is tolerable
-       } elsif ($name =~ /^[A-Za-z_]\w*$str_end/) {
+       } elsif ($name =~ $tolerable) {
            # Then we'll warn only if you've asked for warnings
            if (warnings::enabled()) {
                if ($keywords{$name}) {
@@ -83,7 +96,7 @@ sub import {
 
        # Looks like a boolean
        # use constant FRED == fred;
-       } elsif ($name =~ /^[01]?$str_end/) {
+       } elsif ($name =~ $boolean) {
             require Carp;
            if (@_) {
                Carp::croak("Constant name '$name' is invalid");

--
Perl5 Master Repository

Reply via email to