Filename | C:/tmp64ng/perl/lib/constant.pm |
Statements | Executed 824 statements in 0s |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 0s | 0s | BEGIN@126 | constant::
1 | 1 | 1 | 0s | 0s | BEGIN@2 | constant::
1 | 1 | 1 | 0s | 0s | BEGIN@24 | constant::
1 | 1 | 1 | 0s | 0s | BEGIN@3 | constant::
1 | 1 | 1 | 0s | 0s | BEGIN@39 | constant::
1 | 1 | 1 | 0s | 0s | BEGIN@4 | constant::
1 | 1 | 1 | 0s | 0s | BEGIN@6 | constant::
1 | 1 | 1 | 0s | 0s | BEGIN@64 | constant::
33 | 1 | 1 | 0s | 0s | CORE:match (opcode) | constant::
3 | 3 | 1 | 0s | 0s | CORE:qr (opcode) | constant::
33 | 1 | 1 | 0s | 0s | CORE:regcomp (opcode) | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:159] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:175] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:177] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:40] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:41] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:42] | constant::
33 | 33 | 4 | 0s | 0s | import | constant::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package constant; | ||||
2 | 2 | 0s | 1 | 0s | # spent 0s within constant::BEGIN@2 which was called:
# once (0s+0s) by File::Spec::Unix::BEGIN@127 at line 2 # spent 0s making 1 call to constant::BEGIN@2 |
3 | 2 | 0s | 2 | 0s | # spent 0s within constant::BEGIN@3 which was called:
# once (0s+0s) by File::Spec::Unix::BEGIN@127 at line 3 # spent 0s making 1 call to constant::BEGIN@3
# spent 0s making 1 call to strict::import |
4 | 2 | 0s | 2 | 0s | # spent 0s within constant::BEGIN@4 which was called:
# once (0s+0s) by File::Spec::Unix::BEGIN@127 at line 4 # spent 0s making 1 call to constant::BEGIN@4
# spent 0s making 1 call to warnings::register::import |
5 | |||||
6 | 2 | 0s | 2 | 0s | # spent 0s within constant::BEGIN@6 which was called:
# once (0s+0s) by File::Spec::Unix::BEGIN@127 at line 6 # spent 0s making 1 call to constant::BEGIN@6
# spent 0s making 1 call to vars::import |
7 | 1 | 0s | $VERSION = '1.31'; | ||
8 | |||||
9 | #======================================================================= | ||||
10 | |||||
11 | # Some names are evil choices. | ||||
12 | 1 | 0s | my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD }; | ||
13 | 1 | 0s | $keywords{UNITCHECK}++ if $] > 5.009; | ||
14 | |||||
15 | 1 | 0s | my %forced_into_main = map +($_, 1), | ||
16 | qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG }; | ||||
17 | |||||
18 | 1 | 0s | my %forbidden = (%keywords, %forced_into_main); | ||
19 | |||||
20 | 1 | 0s | 1 | 0s | my $normal_constant_name = qr/^_?[^\W_0-9]\w*\z/; # spent 0s making 1 call to constant::CORE:qr |
21 | 1 | 0s | 1 | 0s | my $tolerable = qr/^[A-Za-z_]\w*\z/; # spent 0s making 1 call to constant::CORE:qr |
22 | 1 | 0s | 1 | 0s | my $boolean = qr/^[01]?\z/; # spent 0s making 1 call to constant::CORE:qr |
23 | |||||
24 | # spent 0s within constant::BEGIN@24 which was called:
# once (0s+0s) by File::Spec::Unix::BEGIN@127 at line 44 | ||||
25 | # We'd like to do use constant _CAN_PCS => $] > 5.009002 | ||||
26 | # but that's a bit tricky before we load the constant module :-) | ||||
27 | # By doing this, we save 1 run time check for *every* call to import. | ||||
28 | 1 | 0s | my $const = $] > 5.009002; | ||
29 | 1 | 0s | my $downgrade = $] < 5.015004; # && $] >= 5.008 | ||
30 | 1 | 0s | my $constarray = exists &_make_const; | ||
31 | 1 | 0s | if ($const) { | ||
32 | 1 | 0s | 1 | 0s | Internals::SvREADONLY($const, 1); # spent 0s making 1 call to Internals::SvREADONLY |
33 | 1 | 0s | 1 | 0s | Internals::SvREADONLY($downgrade, 1); # spent 0s making 1 call to Internals::SvREADONLY |
34 | 1 | 0s | $constant::{_CAN_PCS} = \$const; | ||
35 | 1 | 0s | $constant::{_DOWNGRADE} = \$downgrade; | ||
36 | 1 | 0s | $constant::{_CAN_PCS_FOR_ARRAY} = \$constarray; | ||
37 | } | ||||
38 | else { | ||||
39 | 2 | 0s | 2 | 0s | # spent 0s within constant::BEGIN@39 which was called:
# once (0s+0s) by File::Spec::Unix::BEGIN@127 at line 39 # spent 0s making 1 call to constant::BEGIN@39
# spent 0s making 1 call to strict::unimport |
40 | *{"_CAN_PCS"} = sub () {$const}; | ||||
41 | *{"_DOWNGRADE"} = sub () { $downgrade }; | ||||
42 | *{"_CAN_PCS_FOR_ARRAY"} = sub () { $constarray }; | ||||
43 | } | ||||
44 | 1 | 0s | 1 | 0s | } # spent 0s making 1 call to constant::BEGIN@24 |
45 | |||||
46 | #======================================================================= | ||||
47 | # import() - import symbols into user's namespace | ||||
48 | # | ||||
49 | # What we actually do is define a function in the caller's namespace | ||||
50 | # which returns the value. The function we create will normally | ||||
51 | # be inlined as a constant, thereby avoiding further sub calling | ||||
52 | # overhead. | ||||
53 | #======================================================================= | ||||
54 | # spent 0s within constant::import which was called 33 times, avg 0s/call:
# once (0s+0s) by File::Spec::Unix::BEGIN@127 at line 127 of File/Spec/Unix.pm
# once (0s+0s) by File::Spec::Unix::BEGIN@220 at line 220 of File/Spec/Unix.pm
# once (0s+0s) by File::Spec::Unix::BEGIN@242 at line 242 of File/Spec/Unix.pm
# once (0s+0s) by File::Spec::Unix::BEGIN@136 at line 136 of File/Spec/Unix.pm
# once (0s+0s) by File::Spec::Unix::BEGIN@145 at line 145 of File/Spec/Unix.pm
# once (0s+0s) by Encode::Alias::BEGIN@6 at line 6 of Encode/Alias.pm
# once (0s+0s) by JSON::PP::BEGIN@41 at line 41 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@27 at line 27 of JSON/PP.pm
# once (0s+0s) by JSON::PP::IncrParser::BEGIN@1421 at line 1421 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@31 at line 31 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@43 at line 43 of JSON/PP.pm
# once (0s+0s) by JSON::PP::IncrParser::BEGIN@1420 at line 1420 of JSON/PP.pm
# once (0s+0s) by JSON::PP::IncrParser::BEGIN@1419 at line 1419 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@37 at line 37 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@28 at line 28 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@35 at line 35 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@34 at line 34 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@21 at line 21 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@25 at line 25 of JSON/PP.pm
# once (0s+0s) by JSON::PP::IncrParser::BEGIN@1418 at line 1418 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@36 at line 36 of JSON/PP.pm
# once (0s+0s) by JSON::PP::IncrParser::BEGIN@1422 at line 1422 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@32 at line 32 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@23 at line 23 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@22 at line 22 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@39 at line 39 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@30 at line 30 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@29 at line 29 of JSON/PP.pm
# once (0s+0s) by JSON::PP::IncrParser::BEGIN@1417 at line 1417 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@38 at line 38 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@24 at line 24 of JSON/PP.pm
# once (0s+0s) by JSON::PP::BEGIN@26 at line 26 of JSON/PP.pm
# once (0s+0s) by Encode::BEGIN@8 at line 8 of Encode.pm | ||||
55 | 33 | 0s | my $class = shift; | ||
56 | 33 | 0s | return unless @_; # Ignore 'use constant;' | ||
57 | 33 | 0s | my $constants; | ||
58 | 33 | 0s | my $multiple = ref $_[0]; | ||
59 | 33 | 0s | my $pkg = caller; | ||
60 | 33 | 0s | my $flush_mro; | ||
61 | my $symtab; | ||||
62 | |||||
63 | 33 | 0s | if (_CAN_PCS) { | ||
64 | 2 | 0s | 2 | 0s | # spent 0s within constant::BEGIN@64 which was called:
# once (0s+0s) by File::Spec::Unix::BEGIN@127 at line 64 # spent 0s making 1 call to constant::BEGIN@64
# spent 0s making 1 call to strict::unimport |
65 | 33 | 0s | $symtab = \%{$pkg . '::'}; | ||
66 | }; | ||||
67 | |||||
68 | 33 | 0s | if ( $multiple ) { | ||
69 | if (ref $_[0] ne 'HASH') { | ||||
70 | require Carp; | ||||
71 | Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'"); | ||||
72 | } | ||||
73 | $constants = shift; | ||||
74 | } else { | ||||
75 | 33 | 0s | unless (defined $_[0]) { | ||
76 | require Carp; | ||||
77 | Carp::croak("Can't use undef as constant name"); | ||||
78 | } | ||||
79 | 33 | 0s | $constants->{+shift} = undef; | ||
80 | } | ||||
81 | |||||
82 | 33 | 0s | foreach my $name ( keys %$constants ) { | ||
83 | # Normal constant name | ||||
84 | 33 | 0s | 66 | 0s | if ($name =~ $normal_constant_name and !$forbidden{$name}) { # spent 0s making 33 calls to constant::CORE:match, avg 0s/call
# spent 0s making 33 calls to constant::CORE:regcomp, avg 0s/call |
85 | # Everything is okay | ||||
86 | |||||
87 | # Name forced into main, but we're not in main. Fatal. | ||||
88 | } elsif ($forced_into_main{$name} and $pkg ne 'main') { | ||||
89 | require Carp; | ||||
90 | Carp::croak("Constant name '$name' is forced into main::"); | ||||
91 | |||||
92 | # Starts with double underscore. Fatal. | ||||
93 | } elsif ($name =~ /^__/) { | ||||
94 | require Carp; | ||||
95 | Carp::croak("Constant name '$name' begins with '__'"); | ||||
96 | |||||
97 | # Maybe the name is tolerable | ||||
98 | } elsif ($name =~ $tolerable) { | ||||
99 | # Then we'll warn only if you've asked for warnings | ||||
100 | if (warnings::enabled()) { | ||||
101 | if ($keywords{$name}) { | ||||
102 | warnings::warn("Constant name '$name' is a Perl keyword"); | ||||
103 | } elsif ($forced_into_main{$name}) { | ||||
104 | warnings::warn("Constant name '$name' is " . | ||||
105 | "forced into package main::"); | ||||
106 | } | ||||
107 | } | ||||
108 | |||||
109 | # Looks like a boolean | ||||
110 | # use constant FRED == fred; | ||||
111 | } elsif ($name =~ $boolean) { | ||||
112 | require Carp; | ||||
113 | if (@_) { | ||||
114 | Carp::croak("Constant name '$name' is invalid"); | ||||
115 | } else { | ||||
116 | Carp::croak("Constant name looks like boolean value"); | ||||
117 | } | ||||
118 | |||||
119 | } else { | ||||
120 | # Must have bad characters | ||||
121 | require Carp; | ||||
122 | Carp::croak("Constant name '$name' has invalid characters"); | ||||
123 | } | ||||
124 | |||||
125 | { | ||||
126 | 35 | 0s | 2 | 0s | # spent 0s within constant::BEGIN@126 which was called:
# once (0s+0s) by File::Spec::Unix::BEGIN@127 at line 126 # spent 0s making 1 call to constant::BEGIN@126
# spent 0s making 1 call to strict::unimport |
127 | 33 | 0s | my $full_name = "${pkg}::$name"; | ||
128 | 33 | 0s | $declared{$full_name}++; | ||
129 | 33 | 0s | if ($multiple || @_ == 1) { | ||
130 | 33 | 0s | my $scalar = $multiple ? $constants->{$name} : $_[0]; | ||
131 | |||||
132 | if (_DOWNGRADE) { # for 5.8 to 5.14 | ||||
133 | # Work around perl bug #31991: Sub names (actually glob | ||||
134 | # names in general) ignore the UTF8 flag. So we have to | ||||
135 | # turn it off to get the "right" symbol table entry. | ||||
136 | utf8::is_utf8 $name and utf8::encode $name; | ||||
137 | } | ||||
138 | |||||
139 | # The constant serves to optimise this entire block out on | ||||
140 | # 5.8 and earlier. | ||||
141 | 33 | 0s | if (_CAN_PCS) { | ||
142 | # Use a reference as a proxy for a constant subroutine. | ||||
143 | # If this is not a glob yet, it saves space. If it is | ||||
144 | # a glob, we must still create it this way to get the | ||||
145 | # right internal flags set, as constants are distinct | ||||
146 | # from subroutines created with sub(){...}. | ||||
147 | # The check in Perl_ck_rvconst knows that inlinable | ||||
148 | # constants from cv_const_sv are read only. So we have to: | ||||
149 | 33 | 0s | 33 | 0s | Internals::SvREADONLY($scalar, 1); # spent 0s making 33 calls to Internals::SvREADONLY, avg 0s/call |
150 | 33 | 0s | if ($symtab && !exists $symtab->{$name}) { | ||
151 | 33 | 0s | $symtab->{$name} = \$scalar; | ||
152 | 33 | 0s | ++$flush_mro; | ||
153 | } | ||||
154 | else { | ||||
155 | local $constant::{_dummy} = \$scalar; | ||||
156 | *$full_name = \&{"_dummy"}; | ||||
157 | } | ||||
158 | } else { | ||||
159 | *$full_name = sub () { $scalar }; | ||||
160 | } | ||||
161 | } elsif (@_) { | ||||
162 | my @list = @_; | ||||
163 | if (_CAN_PCS_FOR_ARRAY) { | ||||
164 | _make_const($list[$_]) for 0..$#list; | ||||
165 | _make_const(@list); | ||||
166 | if ($symtab && !exists $symtab->{$name}) { | ||||
167 | $symtab->{$name} = \@list; | ||||
168 | $flush_mro++; | ||||
169 | } | ||||
170 | else { | ||||
171 | local $constant::{_dummy} = \@list; | ||||
172 | *$full_name = \&{"_dummy"}; | ||||
173 | } | ||||
174 | } | ||||
175 | else { *$full_name = sub () { @list }; } | ||||
176 | } else { | ||||
177 | *$full_name = sub () { }; | ||||
178 | } | ||||
179 | } | ||||
180 | } | ||||
181 | # Flush the cache exactly once if we make any direct symbol table changes. | ||||
182 | 33 | 0s | 33 | 0s | mro::method_changed_in($pkg) if _CAN_PCS && $flush_mro; # spent 0s making 33 calls to mro::method_changed_in, avg 0s/call |
183 | } | ||||
184 | |||||
185 | 1 | 0s | 1; | ||
186 | |||||
187 | __END__ | ||||
# spent 0s within constant::CORE:match which was called 33 times, avg 0s/call:
# 33 times (0s+0s) by constant::import at line 84, avg 0s/call | |||||
sub constant::CORE:qr; # opcode | |||||
# spent 0s within constant::CORE:regcomp which was called 33 times, avg 0s/call:
# 33 times (0s+0s) by constant::import at line 84, avg 0s/call |