Filename | /usr/share/perl5/vendor_perl/constant.pm |
Statements | Executed 699 statements in 2.61ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
28 | 28 | 20 | 965µs | 1.06ms | import | constant::
1 | 1 | 1 | 26µs | 26µs | BEGIN@2 | constant::
1 | 1 | 1 | 24µs | 24µs | BEGIN@24 | constant::
1 | 1 | 1 | 19µs | 54µs | BEGIN@6 | constant::
1 | 1 | 1 | 17µs | 36µs | BEGIN@3 | constant::
1 | 1 | 1 | 14µs | 31µs | BEGIN@116 | constant::
1 | 1 | 1 | 13µs | 31µs | BEGIN@28 | constant::
1 | 1 | 1 | 13µs | 122µs | BEGIN@4 | constant::
1 | 1 | 1 | 12µs | 29µs | BEGIN@54 | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:140] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:144] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:146] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:30] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:33] | constant::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package constant; | ||||
2 | 2 | 73µs | 1 | 26µs | # spent 26µs within constant::BEGIN@2 which was called:
# once (26µs+0s) by CGI::Util::BEGIN@10 at line 2 # spent 26µs making 1 call to constant::BEGIN@2 |
3 | 2 | 39µs | 2 | 56µs | # spent 36µs (17+20) within constant::BEGIN@3 which was called:
# once (17µs+20µs) by CGI::Util::BEGIN@10 at line 3 # spent 36µs making 1 call to constant::BEGIN@3
# spent 20µs making 1 call to strict::import |
4 | 2 | 56µs | 2 | 231µs | # spent 122µs (13+109) within constant::BEGIN@4 which was called:
# once (13µs+109µs) by CGI::Util::BEGIN@10 at line 4 # spent 122µs making 1 call to constant::BEGIN@4
# spent 109µs making 1 call to warnings::register::import |
5 | |||||
6 | 2 | 205µs | 2 | 89µs | # spent 54µs (19+35) within constant::BEGIN@6 which was called:
# once (19µs+35µs) by CGI::Util::BEGIN@10 at line 6 # spent 54µs making 1 call to constant::BEGIN@6
# spent 35µs making 1 call to vars::import |
7 | 1 | 900ns | $VERSION = '1.27'; | ||
8 | |||||
9 | #======================================================================= | ||||
10 | |||||
11 | # Some names are evil choices. | ||||
12 | 1 | 9µs | my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD }; | ||
13 | 1 | 2µs | $keywords{UNITCHECK}++ if $] > 5.009; | ||
14 | |||||
15 | 1 | 8µs | my %forced_into_main = map +($_, 1), | ||
16 | qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG }; | ||||
17 | |||||
18 | 1 | 10µs | my %forbidden = (%keywords, %forced_into_main); | ||
19 | |||||
20 | 1 | 9µs | my $normal_constant_name = qr/^_?[^\W_0-9]\w*\z/; | ||
21 | 1 | 2µs | my $tolerable = qr/^[A-Za-z_]\w*\z/; | ||
22 | 1 | 1µs | my $boolean = qr/^[01]?\z/; | ||
23 | |||||
24 | # spent 24µs within constant::BEGIN@24 which was called:
# once (24µs+0s) by CGI::Util::BEGIN@10 at line 34 | ||||
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 | 2 | 122µs | 2 | 50µs | # spent 31µs (13+18) within constant::BEGIN@28 which was called:
# once (13µs+18µs) by CGI::Util::BEGIN@10 at line 28 # spent 31µs making 1 call to constant::BEGIN@28
# spent 18µs making 1 call to strict::unimport |
29 | 1 | 2µs | my $const = $] > 5.009002; | ||
30 | 1 | 11µs | *_CAN_PCS = sub () {$const}; | ||
31 | |||||
32 | 1 | 1µs | my $downgrade = $] < 5.015004; # && $] >= 5.008 | ||
33 | 1 | 12µs | *_DOWNGRADE = sub () { $downgrade }; | ||
34 | 1 | 118µs | 1 | 24µs | } # spent 24µs making 1 call to constant::BEGIN@24 |
35 | |||||
36 | #======================================================================= | ||||
37 | # import() - import symbols into user's namespace | ||||
38 | # | ||||
39 | # What we actually do is define a function in the caller's namespace | ||||
40 | # which returns the value. The function we create will normally | ||||
41 | # be inlined as a constant, thereby avoiding further sub calling | ||||
42 | # overhead. | ||||
43 | #======================================================================= | ||||
44 | # spent 1.06ms (965µs+90µs) within constant::import which was called 28 times, avg 38µs/call:
# once (68µs+5µs) by Data::Dumper::BEGIN@271 at line 271 of Data/Dumper.pm
# once (62µs+6µs) by Foswiki::Plugins::JQueryPlugin::Plugin::BEGIN@9 at line 9 of /var/www/foswiki11/lib/Foswiki/Plugins/JQueryPlugin/Plugin.pm
# once (59µs+5µs) by Foswiki::Render::BEGIN@81 at line 81 of /var/www/foswiki11/lib/Foswiki/Render.pm
# once (54µs+4µs) by Foswiki::Logger::PlainFile::BEGIN@35 at line 35 of /var/www/foswiki11/lib/Foswiki/Logger/PlainFile.pm
# once (51µs+5µs) by CGI::Util::BEGIN@10 at line 10 of CGI/Util.pm
# once (52µs+4µs) by Foswiki::Templates::BEGIN@42 at line 42 of /var/www/foswiki11/lib/Foswiki/Templates.pm
# once (40µs+4µs) by MultipartBuffer::BEGIN@3908 at line 3908 of CGI.pm
# once (38µs+4µs) by Foswiki::MetaCache::BEGIN@31 at line 31 of /var/www/foswiki11/lib/Foswiki/MetaCache.pm
# once (37µs+4µs) by CGI::BEGIN@33 at line 33 of CGI.pm
# once (35µs+3µs) by File::Temp::BEGIN@83 at line 83 of File/Temp.pm
# once (33µs+2µs) by Time::Local::BEGIN@27 at line 27 of Time/Local.pm
# once (31µs+3µs) by Foswiki::Plugins::RenderPlugin::BEGIN@30 at line 30 of /var/www/foswiki11/lib/Foswiki/Plugins/RenderPlugin.pm
# once (30µs+2µs) by Encode::BEGIN@8 at line 8 of Encode.pm
# once (29µs+3µs) by Foswiki::Store::QueryAlgorithms::BruteForce::BEGIN@40 at line 40 of /var/www/foswiki11/lib/Foswiki/Store/QueryAlgorithms/BruteForce.pm
# once (28µs+3µs) by Foswiki::Sandbox::BEGIN@43 at line 43 of /var/www/foswiki11/lib/Foswiki/Sandbox.pm
# once (28µs+3µs) by Foswiki::Validation::BEGIN@54 at line 54 of /var/www/foswiki11/lib/Foswiki/Validation.pm
# once (26µs+2µs) by Foswiki::Iterator::BEGIN@26 at line 26 of /var/www/foswiki11/lib/Foswiki/Iterator.pm
# once (26µs+2µs) by Foswiki::UI::BEGIN@165 at line 165 of /var/www/foswiki11/lib/Foswiki/UI.pm
# once (26µs+3µs) by Foswiki::Query::HoistREs::BEGIN@33 at line 33 of /var/www/foswiki11/lib/Foswiki/Query/HoistREs.pm
# once (26µs+2µs) by Foswiki::LoginManager::BEGIN@68 at line 68 of /var/www/foswiki11/lib/Foswiki/LoginManager.pm
# once (26µs+2µs) by Encode::Alias::BEGIN@6 at line 6 of Encode/Alias.pm
# once (21µs+6µs) by File::Temp::BEGIN@96 at line 96 of File/Temp.pm
# once (24µs+2µs) by File::Temp::BEGIN@95 at line 95 of File/Temp.pm
# once (24µs+2µs) by Time::Local::BEGIN@28 at line 28 of Time/Local.pm
# once (24µs+2µs) by File::Temp::BEGIN@86 at line 86 of File/Temp.pm
# once (22µs+2µs) by File::Temp::BEGIN@90 at line 90 of File/Temp.pm
# once (22µs+2µs) by File::Temp::BEGIN@94 at line 94 of File/Temp.pm
# once (21µs+2µs) by Time::Local::BEGIN@29 at line 29 of Time/Local.pm | ||||
45 | 28 | 20µs | my $class = shift; | ||
46 | 28 | 14µs | return unless @_; # Ignore 'use constant;' | ||
47 | 28 | 5µs | my $constants; | ||
48 | 28 | 21µs | my $multiple = ref $_[0]; | ||
49 | 28 | 75µs | my $pkg = caller; | ||
50 | 28 | 3µs | my $flush_mro; | ||
51 | 28 | 4µs | my $symtab; | ||
52 | |||||
53 | 28 | 11µs | if (_CAN_PCS) { | ||
54 | 2 | 417µs | 2 | 46µs | # spent 29µs (12+17) within constant::BEGIN@54 which was called:
# once (12µs+17µs) by CGI::Util::BEGIN@10 at line 54 # spent 29µs making 1 call to constant::BEGIN@54
# spent 17µs making 1 call to strict::unimport |
55 | 28 | 42µs | $symtab = \%{$pkg . '::'}; | ||
56 | }; | ||||
57 | |||||
58 | 28 | 12µs | if ( $multiple ) { | ||
59 | if (ref $_[0] ne 'HASH') { | ||||
60 | require Carp; | ||||
61 | Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'"); | ||||
62 | } | ||||
63 | $constants = shift; | ||||
64 | } else { | ||||
65 | 28 | 10µs | unless (defined $_[0]) { | ||
66 | require Carp; | ||||
67 | Carp::croak("Can't use undef as constant name"); | ||||
68 | } | ||||
69 | 28 | 42µs | $constants->{+shift} = undef; | ||
70 | } | ||||
71 | |||||
72 | 28 | 66µs | foreach my $name ( keys %$constants ) { | ||
73 | # Normal constant name | ||||
74 | 28 | 160µs | if ($name =~ $normal_constant_name and !$forbidden{$name}) { | ||
75 | # Everything is okay | ||||
76 | |||||
77 | # Name forced into main, but we're not in main. Fatal. | ||||
78 | } elsif ($forced_into_main{$name} and $pkg ne 'main') { | ||||
79 | require Carp; | ||||
80 | Carp::croak("Constant name '$name' is forced into main::"); | ||||
81 | |||||
82 | # Starts with double underscore. Fatal. | ||||
83 | } elsif ($name =~ /^__/) { | ||||
84 | require Carp; | ||||
85 | Carp::croak("Constant name '$name' begins with '__'"); | ||||
86 | |||||
87 | # Maybe the name is tolerable | ||||
88 | } elsif ($name =~ $tolerable) { | ||||
89 | # Then we'll warn only if you've asked for warnings | ||||
90 | if (warnings::enabled()) { | ||||
91 | if ($keywords{$name}) { | ||||
92 | warnings::warn("Constant name '$name' is a Perl keyword"); | ||||
93 | } elsif ($forced_into_main{$name}) { | ||||
94 | warnings::warn("Constant name '$name' is " . | ||||
95 | "forced into package main::"); | ||||
96 | } | ||||
97 | } | ||||
98 | |||||
99 | # Looks like a boolean | ||||
100 | # use constant FRED == fred; | ||||
101 | } elsif ($name =~ $boolean) { | ||||
102 | require Carp; | ||||
103 | if (@_) { | ||||
104 | Carp::croak("Constant name '$name' is invalid"); | ||||
105 | } else { | ||||
106 | Carp::croak("Constant name looks like boolean value"); | ||||
107 | } | ||||
108 | |||||
109 | } else { | ||||
110 | # Must have bad characters | ||||
111 | require Carp; | ||||
112 | Carp::croak("Constant name '$name' has invalid characters"); | ||||
113 | } | ||||
114 | |||||
115 | { | ||||
116 | 30 | 423µs | 2 | 48µs | # spent 31µs (14+17) within constant::BEGIN@116 which was called:
# once (14µs+17µs) by CGI::Util::BEGIN@10 at line 116 # spent 31µs making 1 call to constant::BEGIN@116
# spent 17µs making 1 call to strict::unimport |
117 | 28 | 29µs | my $full_name = "${pkg}::$name"; | ||
118 | 28 | 48µs | $declared{$full_name}++; | ||
119 | 28 | 25µs | if ($multiple || @_ == 1) { | ||
120 | 28 | 16µs | my $scalar = $multiple ? $constants->{$name} : $_[0]; | ||
121 | |||||
122 | if (_DOWNGRADE) { # for 5.8 to 5.14 | ||||
123 | # Work around perl bug #31991: Sub names (actually glob | ||||
124 | # names in general) ignore the UTF8 flag. So we have to | ||||
125 | # turn it off to get the "right" symbol table entry. | ||||
126 | utf8::is_utf8 $name and utf8::encode $name; | ||||
127 | } | ||||
128 | |||||
129 | # The constant serves to optimise this entire block out on | ||||
130 | # 5.8 and earlier. | ||||
131 | 28 | 32µs | if (_CAN_PCS && $symtab && !exists $symtab->{$name}) { | ||
132 | # No typeglob yet, so we can use a reference as space- | ||||
133 | # efficient proxy for a constant subroutine | ||||
134 | # The check in Perl_ck_rvconst knows that inlinable | ||||
135 | # constants from cv_const_sv are read only. So we have to: | ||||
136 | 28 | 156µs | 28 | 43µs | Internals::SvREADONLY($scalar, 1); # spent 43µs making 28 calls to Internals::SvREADONLY, avg 2µs/call |
137 | 28 | 23µs | $symtab->{$name} = \$scalar; | ||
138 | 28 | 11µs | ++$flush_mro; | ||
139 | } else { | ||||
140 | *$full_name = sub () { $scalar }; | ||||
141 | } | ||||
142 | } elsif (@_) { | ||||
143 | my @list = @_; | ||||
144 | *$full_name = sub () { @list }; | ||||
145 | } else { | ||||
146 | *$full_name = sub () { }; | ||||
147 | } | ||||
148 | } | ||||
149 | } | ||||
150 | # Flush the cache exactly once if we make any direct symbol table changes. | ||||
151 | 28 | 257µs | 28 | 47µs | mro::method_changed_in($pkg) if _CAN_PCS && $flush_mro; # spent 47µs making 28 calls to mro::method_changed_in, avg 2µs/call |
152 | } | ||||
153 | |||||
154 | 1 | 12µs | 1; | ||
155 | |||||
156 | __END__ |