|
|
945158 |
diff -up constant-1.27/lib/constant.pm.orig constant-1.27/lib/constant.pm
|
|
|
945158 |
--- constant-1.27/lib/constant.pm.orig 2014-09-09 10:42:02.450977378 +0200
|
|
|
945158 |
+++ constant-1.27/lib/constant.pm 2014-09-09 10:42:25.026227379 +0200
|
|
|
945158 |
@@ -4,7 +4,7 @@ use strict;
|
|
|
945158 |
use warnings::register;
|
|
|
945158 |
|
|
|
945158 |
use vars qw($VERSION %declared);
|
|
|
945158 |
-$VERSION = '1.27';
|
|
|
945158 |
+$VERSION = '1.31';
|
|
|
945158 |
|
|
|
945158 |
#=======================================================================
|
|
|
945158 |
|
|
|
945158 |
@@ -25,12 +25,22 @@ BEGIN {
|
|
|
945158 |
# We'd like to do use constant _CAN_PCS => $] > 5.009002
|
|
|
945158 |
# but that's a bit tricky before we load the constant module :-)
|
|
|
945158 |
# By doing this, we save 1 run time check for *every* call to import.
|
|
|
945158 |
- no strict 'refs';
|
|
|
945158 |
my $const = $] > 5.009002;
|
|
|
945158 |
- *_CAN_PCS = sub () {$const};
|
|
|
945158 |
-
|
|
|
945158 |
my $downgrade = $] < 5.015004; # && $] >= 5.008
|
|
|
945158 |
- *_DOWNGRADE = sub () { $downgrade };
|
|
|
945158 |
+ my $constarray = exists &_make_const;
|
|
|
945158 |
+ if ($const) {
|
|
|
945158 |
+ Internals::SvREADONLY($const, 1);
|
|
|
945158 |
+ Internals::SvREADONLY($downgrade, 1);
|
|
|
945158 |
+ $constant::{_CAN_PCS} = \$const;
|
|
|
945158 |
+ $constant::{_DOWNGRADE} = \$downgrade;
|
|
|
945158 |
+ $constant::{_CAN_PCS_FOR_ARRAY} = \$constarray;
|
|
|
945158 |
+ }
|
|
|
945158 |
+ else {
|
|
|
945158 |
+ no strict 'refs';
|
|
|
945158 |
+ *{"_CAN_PCS"} = sub () {$const};
|
|
|
945158 |
+ *{"_DOWNGRADE"} = sub () { $downgrade };
|
|
|
945158 |
+ *{"_CAN_PCS_FOR_ARRAY"} = sub () { $constarray };
|
|
|
945158 |
+ }
|
|
|
945158 |
}
|
|
|
945158 |
|
|
|
945158 |
#=======================================================================
|
|
|
945158 |
@@ -128,20 +138,41 @@ sub import {
|
|
|
945158 |
|
|
|
945158 |
# The constant serves to optimise this entire block out on
|
|
|
945158 |
# 5.8 and earlier.
|
|
|
945158 |
- if (_CAN_PCS && $symtab && !exists $symtab->{$name}) {
|
|
|
945158 |
- # No typeglob yet, so we can use a reference as space-
|
|
|
945158 |
- # efficient proxy for a constant subroutine
|
|
|
945158 |
+ if (_CAN_PCS) {
|
|
|
945158 |
+ # Use a reference as a proxy for a constant subroutine.
|
|
|
945158 |
+ # If this is not a glob yet, it saves space. If it is
|
|
|
945158 |
+ # a glob, we must still create it this way to get the
|
|
|
945158 |
+ # right internal flags set, as constants are distinct
|
|
|
945158 |
+ # from subroutines created with sub(){...}.
|
|
|
945158 |
# The check in Perl_ck_rvconst knows that inlinable
|
|
|
945158 |
# constants from cv_const_sv are read only. So we have to:
|
|
|
945158 |
Internals::SvREADONLY($scalar, 1);
|
|
|
945158 |
- $symtab->{$name} = \$scalar;
|
|
|
945158 |
- ++$flush_mro;
|
|
|
945158 |
+ if ($symtab && !exists $symtab->{$name}) {
|
|
|
945158 |
+ $symtab->{$name} = \$scalar;
|
|
|
945158 |
+ ++$flush_mro;
|
|
|
945158 |
+ }
|
|
|
945158 |
+ else {
|
|
|
945158 |
+ local $constant::{_dummy} = \$scalar;
|
|
|
945158 |
+ *$full_name = \&{"_dummy"};
|
|
|
945158 |
+ }
|
|
|
945158 |
} else {
|
|
|
945158 |
*$full_name = sub () { $scalar };
|
|
|
945158 |
}
|
|
|
945158 |
} elsif (@_) {
|
|
|
945158 |
my @list = @_;
|
|
|
945158 |
- *$full_name = sub () { @list };
|
|
|
945158 |
+ if (_CAN_PCS_FOR_ARRAY) {
|
|
|
945158 |
+ _make_const($list[$_]) for 0..$#list;
|
|
|
945158 |
+ _make_const(@list);
|
|
|
945158 |
+ if ($symtab && !exists $symtab->{$name}) {
|
|
|
945158 |
+ $symtab->{$name} = \@list;
|
|
|
945158 |
+ $flush_mro++;
|
|
|
945158 |
+ }
|
|
|
945158 |
+ else {
|
|
|
945158 |
+ local $constant::{_dummy} = \@list;
|
|
|
945158 |
+ *$full_name = \&{"_dummy"};
|
|
|
945158 |
+ }
|
|
|
945158 |
+ }
|
|
|
945158 |
+ else { *$full_name = sub () { @list }; }
|
|
|
945158 |
} else {
|
|
|
945158 |
*$full_name = sub () { };
|
|
|
945158 |
}
|
|
|
945158 |
@@ -190,7 +221,7 @@ This pragma allows you to declare consta
|
|
|
945158 |
|
|
|
945158 |
When you declare a constant such as C<PI> using the method shown
|
|
|
945158 |
above, each machine your script runs upon can have as many digits
|
|
|
945158 |
-of accuracy as it can use. Also, your program will be easier to
|
|
|
945158 |
+of accuracy as it can use. Also, your program will be easier to
|
|
|
945158 |
read, more likely to be maintained (and maintained correctly), and
|
|
|
945158 |
far less likely to send a space probe to the wrong planet because
|
|
|
945158 |
nobody noticed the one equation in which you wrote C<3.14195>.
|
|
|
945158 |
@@ -203,7 +234,7 @@ away if the constant is false.
|
|
|
945158 |
=head1 NOTES
|
|
|
945158 |
|
|
|
945158 |
As with all C<use> directives, defining a constant happens at
|
|
|
945158 |
-compile time. Thus, it's probably not correct to put a constant
|
|
|
945158 |
+compile time. Thus, it's probably not correct to put a constant
|
|
|
945158 |
declaration inside of a conditional statement (like C
|
|
|
945158 |
{ use constant ... }>).
|
|
|
945158 |
|
|
|
945158 |
@@ -236,8 +267,8 @@ their own constants to override those in
|
|
|
945158 |
The use of all caps for constant names is merely a convention,
|
|
|
945158 |
although it is recommended in order to make constants stand out
|
|
|
945158 |
and to help avoid collisions with other barewords, keywords, and
|
|
|
945158 |
-subroutine names. Constant names must begin with a letter or
|
|
|
945158 |
-underscore. Names beginning with a double underscore are reserved. Some
|
|
|
945158 |
+subroutine names. Constant names must begin with a letter or
|
|
|
945158 |
+underscore. Names beginning with a double underscore are reserved. Some
|
|
|
945158 |
poor choices for names will generate warnings, if warnings are enabled at
|
|
|
945158 |
compile time.
|
|
|
945158 |
|
|
|
945158 |
@@ -312,15 +343,15 @@ constants without any problems.
|
|
|
945158 |
=head1 TECHNICAL NOTES
|
|
|
945158 |
|
|
|
945158 |
In the current implementation, scalar constants are actually
|
|
|
945158 |
-inlinable subroutines. As of version 5.004 of Perl, the appropriate
|
|
|
945158 |
+inlinable subroutines. As of version 5.004 of Perl, the appropriate
|
|
|
945158 |
scalar constant is inserted directly in place of some subroutine
|
|
|
945158 |
-calls, thereby saving the overhead of a subroutine call. See
|
|
|
945158 |
+calls, thereby saving the overhead of a subroutine call. See
|
|
|
945158 |
L<perlsub/"Constant Functions"> for details about how and when this
|
|
|
945158 |
happens.
|
|
|
945158 |
|
|
|
945158 |
In the rare case in which you need to discover at run time whether a
|
|
|
945158 |
particular constant has been declared via this module, you may use
|
|
|
945158 |
-this function to examine the hash C<%constant::declared>. If the given
|
|
|
945158 |
+this function to examine the hash C<%constant::declared>. If the given
|
|
|
945158 |
constant name does not include a package name, the current package is
|
|
|
945158 |
used.
|
|
|
945158 |
|
|
|
945158 |
@@ -335,11 +366,12 @@ used.
|
|
|
945158 |
|
|
|
945158 |
=head1 CAVEATS
|
|
|
945158 |
|
|
|
945158 |
-In the current version of Perl, list constants are not inlined
|
|
|
945158 |
-and some symbols may be redefined without generating a warning.
|
|
|
945158 |
+List constants are not inlined unless you are using Perl v5.20 or higher.
|
|
|
945158 |
+In v5.20 or higher, they are still not read-only, but that may change in
|
|
|
945158 |
+future versions.
|
|
|
945158 |
|
|
|
945158 |
It is not possible to have a subroutine or a keyword with the same
|
|
|
945158 |
-name as a constant in the same package. This is probably a Good Thing.
|
|
|
945158 |
+name as a constant in the same package. This is probably a Good Thing.
|
|
|
945158 |
|
|
|
945158 |
A constant with a name in the list C
|
|
|
945158 |
ENV INC SIG> is not allowed anywhere but in package C<main::>, for
|