← Index
NYTProf Performance Profile   « block view • line view • sub view »
For ddd2.pl
  Run on Tue May 25 16:52:24 2010
Reported on Tue May 25 16:57:00 2010

File /usr/local/lib/perl5/5.8.8/constant.pm
Statements Executed 1378
Statement Execution Time 16.9ms
Subroutines — ordered by exclusive time
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
8080613.8ms14.7msconstant::::importconstant::import
8012964µs964µsconstant::::CORE:matchconstant::CORE:match (opcode)
0000s0sconstant::::BEGINconstant::BEGIN
0000s0sconstant::::__ANON__[:103]constant::__ANON__[:103]
0000s0sconstant::::__ANON__[:106]constant::__ANON__[:106]
0000s0sconstant::::__ANON__[:108]constant::__ANON__[:108]
0000s0sconstant::::__ANON__[:99]constant::__ANON__[:99]
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package constant;
2
3387µs126µsuse strict;
# spent 26µs making 1 call to strict::import
4351µsuse 5.006_00;
53856µs1789µsuse warnings::register;
# spent 789µs making 1 call to warnings::register::import
6
716µsour($VERSION, %declared);
816µs$VERSION = '1.05';
9
10#=======================================================================
11
12# Some names are evil choices.
13121µsmy %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD };
14
15117µsmy %forced_into_main = map +($_, 1),
16 qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG };
17
18117µsmy %forbidden = (%keywords, %forced_into_main);
19
20#=======================================================================
21# import() - import symbols into user's namespace
22#
23# What we actually do is define a function in the caller's namespace
24# which returns the value. The function we create will normally
25# be inlined as a constant, thereby avoiding further sub calling
26# overhead.
27#=======================================================================
28
# spent 14.7ms (13.8+964µs) within constant::import which was called 80 times, avg 184µs/call: # once (220µs+12µs) by Net::HTTP::Methods::zlib_ok at line 67 of IO/Compress/Gzip/Constants.pm # once (203µs+11µs) by Net::HTTP::Methods::zlib_ok at line 466 of IO/Compress/Base/Common.pm # once (195µs+15µs) by Net::HTTP::Methods::zlib_ok at line 789 of IO/Compress/Base/Common.pm # once (197µs+11µs) by Net::HTTP::Methods::zlib_ok at line 470 of IO/Compress/Base/Common.pm # once (191µs+16µs) by Net::HTTP::Methods::zlib_ok at line 461 of IO/Compress/Base/Common.pm # once (187µs+18µs) by Class::DBI::_require_class at line 93 of Class/Date.pm # once (193µs+11µs) by Net::HTTP::Methods::zlib_ok at line 111 of IO/Compress/Gzip/Constants.pm # once (191µs+12µs) by Net::HTTP::Methods::zlib_ok at line 89 of IO/Compress/Gzip/Constants.pm # once (186µs+15µs) by Net::HTTP::Methods::zlib_ok at line 65 of IO/Compress/Gzip/Constants.pm # once (186µs+14µs) by Net::HTTP::Methods::zlib_ok at line 54 of Compress/Zlib.pm # once (185µs+12µs) by Net::HTTP::Methods::zlib_ok at line 131 of IO/Compress/Base/Common.pm # once (185µs+11µs) by Net::HTTP::Methods::zlib_ok at line 86 of IO/Compress/Gzip/Constants.pm # once (178µs+17µs) by Net::HTTP::Methods::zlib_ok at line 78 of Compress/Raw/Zlib.pm # once (180µs+15µs) by Net::HTTP::Methods::zlib_ok at line 130 of IO/Compress/Base/Common.pm # once (179µs+16µs) by Net::HTTP::Methods::zlib_ok at line 14 of IO/Uncompress/Base.pm # once (181µs+12µs) by Net::HTTP::Methods::zlib_ok at line 104 of Compress/Raw/Zlib.pm # once (182µs+11µs) by Net::HTTP::Methods::zlib_ok at line 70 of IO/Compress/Gzip/Constants.pm # once (181µs+12µs) by Net::HTTP::Methods::zlib_ok at line 77 of IO/Compress/Gzip/Constants.pm # once (182µs+10µs) by Net::HTTP::Methods::zlib_ok at line 106 of Compress/Raw/Zlib.pm # once (179µs+13µs) by Net::HTTP::Methods::zlib_ok at line 69 of IO/Compress/Gzip/Constants.pm # once (181µs+11µs) by Net::HTTP::Methods::zlib_ok at line 474 of IO/Compress/Base/Common.pm # once (178µs+13µs) by Net::HTTP::Methods::zlib_ok at line 476 of IO/Compress/Base/Common.pm # once (176µs+15µs) by Net::HTTP::Methods::zlib_ok at line 41 of IO/Compress/Base/Common.pm # once (180µs+10µs) by Net::HTTP::Methods::zlib_ok at line 43 of IO/Compress/Base/Common.pm # once (176µs+12µs) by Net::HTTP::Methods::zlib_ok at line 75 of IO/Compress/Gzip/Constants.pm # once (175µs+12µs) by Class::DBI::_require_class at line 595 of Class/Date.pm # once (177µs+10µs) by Net::HTTP::Methods::zlib_ok at line 76 of IO/Compress/Gzip/Constants.pm # once (175µs+11µs) by Net::HTTP::Methods::zlib_ok at line 79 of Compress/Raw/Zlib.pm # once (175µs+11µs) by Net::HTTP::Methods::zlib_ok at line 115 of IO/Compress/Gzip/Constants.pm # once (173µs+12µs) by Net::HTTP::Methods::zlib_ok at line 143 of IO/Compress/Gzip/Constants.pm # once (174µs+11µs) by Net::HTTP::Methods::zlib_ok at line 113 of IO/Compress/Gzip/Constants.pm # once (170µs+14µs) by Net::HTTP::Methods::zlib_ok at line 95 of Compress/Raw/Zlib.pm # once (172µs+11µs) by Net::HTTP::Methods::zlib_ok at line 98 of Compress/Raw/Zlib.pm # once (172µs+11µs) by Net::HTTP::Methods::zlib_ok at line 96 of Compress/Raw/Zlib.pm # once (171µs+11µs) by Net::HTTP::Methods::zlib_ok at line 57 of Compress/Zlib.pm # once (172µs+10µs) by Net::HTTP::Methods::zlib_ok at line 119 of IO/Compress/Gzip/Constants.pm # once (172µs+10µs) by Net::HTTP::Methods::zlib_ok at line 88 of IO/Compress/Gzip/Constants.pm # once (171µs+11µs) by Net::HTTP::Methods::zlib_ok at line 114 of IO/Compress/Gzip/Constants.pm # once (169µs+13µs) by Net::HTTP::Methods::zlib_ok at line 66 of IO/Compress/Gzip/Constants.pm # once (171µs+11µs) by Net::HTTP::Methods::zlib_ok at line 475 of IO/Compress/Base/Common.pm # once (170µs+12µs) by Net::HTTP::Methods::zlib_ok at line 462 of IO/Compress/Base/Common.pm # once (171µs+11µs) by Net::HTTP::Methods::zlib_ok at line 15 of IO/Uncompress/Base.pm # once (168µs+13µs) by Class::DBI::_require_class at line 592 of Class/Date.pm # once (171µs+10µs) by Net::HTTP::Methods::zlib_ok at line 469 of IO/Compress/Base/Common.pm # once (167µs+12µs) by Net::HTTP::Methods::zlib_ok at line 42 of IO/Compress/Base/Common.pm # once (165µs+13µs) by Net::HTTP::Methods::zlib_ok at line 97 of Compress/Raw/Zlib.pm # once (166µs+12µs) by Net::HTTP::Methods::zlib_ok at line 80 of Compress/Raw/Zlib.pm # once (167µs+11µs) by Net::HTTP::Methods::zlib_ok at line 109 of Compress/Raw/Zlib.pm # once (167µs+11µs) by Net::HTTP::Methods::zlib_ok at line 99 of Compress/Raw/Zlib.pm # once (167µs+11µs) by Net::HTTP::Methods::zlib_ok at line 87 of IO/Compress/Gzip/Constants.pm # once (165µs+13µs) by Net::HTTP::Methods::zlib_ok at line 790 of IO/Compress/Base/Common.pm # once (167µs+11µs) by Net::HTTP::Methods::zlib_ok at line 471 of IO/Compress/Base/Common.pm # once (167µs+11µs) by Net::HTTP::Methods::zlib_ok at line 132 of IO/Compress/Base/Common.pm # once (165µs+12µs) by Net::HTTP::Methods::zlib_ok at line 107 of Compress/Raw/Zlib.pm # once (165µs+12µs) by Net::HTTP::Methods::zlib_ok at line 80 of IO/Compress/Gzip/Constants.pm # once (164µs+13µs) by Net::HTTP::Methods::zlib_ok at line 109 of IO/Compress/Gzip/Constants.pm # once (164µs+13µs) by Net::HTTP::Methods::zlib_ok at line 477 of IO/Compress/Base/Common.pm # once (163µs+13µs) by Net::HTTP::Methods::zlib_ok at line 81 of Compress/Raw/Zlib.pm # once (164µs+12µs) by Net::HTTP::Methods::zlib_ok at line 464 of IO/Compress/Base/Common.pm # once (165µs+11µs) by Net::HTTP::Methods::zlib_ok at line 44 of IO/Compress/Base/Common.pm # once (166µs+10µs) by Net::HTTP::Methods::zlib_ok at line 791 of IO/Compress/Base/Common.pm # once (162µs+13µs) by Net::HTTP::Methods::zlib_ok at line 78 of IO/Compress/Gzip/Constants.pm # once (162µs+13µs) by Net::HTTP::Methods::zlib_ok at line 92 of IO/Compress/Gzip/Constants.pm # once (164µs+11µs) by Net::HTTP::Methods::zlib_ok at line 134 of IO/Compress/Base/Common.pm # once (163µs+11µs) by Net::HTTP::Methods::zlib_ok at line 82 of IO/Compress/Gzip/Constants.pm # once (161µs+13µs) by Net::HTTP::Methods::zlib_ok at line 84 of IO/Compress/Gzip/Constants.pm # once (162µs+12µs) by Net::HTTP::Methods::zlib_ok at line 478 of IO/Compress/Base/Common.pm # once (160µs+13µs) by Net::HTTP::Methods::zlib_ok at line 100 of Compress/Raw/Zlib.pm # once (161µs+12µs) by Class::DBI::_require_class at line 94 of Class/Date.pm # once (161µs+12µs) by Net::HTTP::Methods::zlib_ok at line 90 of IO/Compress/Gzip/Constants.pm # once (162µs+11µs) by Net::HTTP::Methods::zlib_ok at line 73 of IO/Compress/Gzip/Constants.pm # once (161µs+11µs) by Net::HTTP::Methods::zlib_ok at line 108 of Compress/Raw/Zlib.pm # once (161µs+11µs) by Net::HTTP::Methods::zlib_ok at line 74 of IO/Compress/Gzip/Constants.pm # once (160µs+12µs) by Net::HTTP::Methods::zlib_ok at line 465 of IO/Compress/Base/Common.pm # once (159µs+12µs) by Net::HTTP::Methods::zlib_ok at line 105 of Compress/Raw/Zlib.pm # once (160µs+11µs) by Net::HTTP::Methods::zlib_ok at line 463 of IO/Compress/Base/Common.pm # once (159µs+11µs) by Net::HTTP::Methods::zlib_ok at line 102 of Compress/Raw/Zlib.pm # once (157µs+12µs) by Net::HTTP::Methods::zlib_ok at line 55 of Compress/Zlib.pm # once (157µs+11µs) by Net::HTTP::Methods::zlib_ok at line 473 of IO/Compress/Base/Common.pm # once (156µs+11µs) by Net::HTTP::Methods::zlib_ok at line 56 of Compress/Zlib.pm
sub import {
2980464µs my $class = shift;
3080388µs return unless @_; # Ignore 'use constant;'
3180479µs my %constants = ();
3280423µs my $multiple = ref $_[0];
33
3480742µs if ( $multiple ) {
35 if (ref $_[0] ne 'HASH') {
36 require Carp;
37 Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'");
38 }
39 %constants = %{+shift};
40 } else {
4180577µs $constants{+shift} = undef;
42 }
43
44802.26ms foreach my $name ( keys %constants ) {
4580377µs unless (defined $name) {
46 require Carp;
47 Carp::croak("Can't use undef as constant name");
48 }
4980480µs my $pkg = caller;
50
51 # Normal constant name
52802.45ms80964µs if ($name =~ /^_?[^\W_0-9]\w*\z/ and !$forbidden{$name}) {
# spent 964µs making 80 calls to constant::CORE:match, avg 12µs/call
53 # Everything is okay
54
55 # Name forced into main, but we're not in main. Fatal.
56 } elsif ($forced_into_main{$name} and $pkg ne 'main') {
57 require Carp;
58 Carp::croak("Constant name '$name' is forced into main::");
59
60 # Starts with double underscore. Fatal.
61 } elsif ($name =~ /^__/) {
62 require Carp;
63 Carp::croak("Constant name '$name' begins with '__'");
64
65 # Maybe the name is tolerable
66 } elsif ($name =~ /^[A-Za-z_]\w*\z/) {
67 # Then we'll warn only if you've asked for warnings
68 if (warnings::enabled()) {
69 if ($keywords{$name}) {
70 warnings::warn("Constant name '$name' is a Perl keyword");
71 } elsif ($forced_into_main{$name}) {
72 warnings::warn("Constant name '$name' is " .
73 "forced into package main::");
74 }
75 }
76
77 # Looks like a boolean
78 # use constant FRED == fred;
79 } elsif ($name =~ /^[01]?\z/) {
80 require Carp;
81 if (@_) {
82 Carp::croak("Constant name '$name' is invalid");
83 } else {
84 Carp::croak("Constant name looks like boolean value");
85 }
86
87 } else {
88 # Must have bad characters
89 require Carp;
90 Carp::croak("Constant name '$name' has invalid characters");
91 }
92
93 {
94831.79ms199µs no strict 'refs';
# spent 99µs making 1 call to strict::unimport
9580540µs my $full_name = "${pkg}::$name";
9680655µs $declared{$full_name}++;
9780703µs if ($multiple) {
98 my $scalar = $constants{$name};
99 *$full_name = sub () { $scalar };
100 } else {
10180753µs if (@_ == 1) {
10280396µs my $scalar = $_[0];
103802.34ms *$full_name = sub () { $scalar };
104 } elsif (@_) {
105 my @list = @_;
106 *$full_name = sub () { @list };
107 } else {
108 *$full_name = sub () { };
109 }
110 }
111 }
112 }
113}
114
115122µs1;
116
117__END__
118
119=head1 NAME
120
121constant - Perl pragma to declare constants
122
123=head1 SYNOPSIS
124
125 use constant PI => 4 * atan2(1, 1);
126 use constant DEBUG => 0;
127
128 print "Pi equals ", PI, "...\n" if DEBUG;
129
130 use constant {
131 SEC => 0,
132 MIN => 1,
133 HOUR => 2,
134 MDAY => 3,
135 MON => 4,
136 YEAR => 5,
137 WDAY => 6,
138 YDAY => 7,
139 ISDST => 8,
140 };
141
142 use constant WEEKDAYS => qw(
143 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
144 );
145
146 print "Today is ", (WEEKDAYS)[ (localtime)[WDAY] ], ".\n";
147
148=head1 DESCRIPTION
149
150This will declare a symbol to be a constant with the given value.
151
152When you declare a constant such as C<PI> using the method shown
153above, each machine your script runs upon can have as many digits
154of accuracy as it can use. Also, your program will be easier to
155read, more likely to be maintained (and maintained correctly), and
156far less likely to send a space probe to the wrong planet because
157nobody noticed the one equation in which you wrote C<3.14195>.
158
159When a constant is used in an expression, perl replaces it with its
160value at compile time, and may then optimize the expression further.
161In particular, any code in an C<if (CONSTANT)> block will be optimized
162away if the constant is false.
163
164=head1 NOTES
165
166As with all C<use> directives, defining a constant happens at
167compile time. Thus, it's probably not correct to put a constant
168declaration inside of a conditional statement (like C<if ($foo)
169{ use constant ... }>).
170
171Constants defined using this module cannot be interpolated into
172strings like variables. However, concatenation works just fine:
173
174 print "Pi equals PI...\n"; # WRONG: does not expand "PI"
175 print "Pi equals ".PI."...\n"; # right
176
177Even though a reference may be declared as a constant, the reference may
178point to data which may be changed, as this code shows.
179
180 use constant ARRAY => [ 1,2,3,4 ];
181 print ARRAY->[1];
182 ARRAY->[1] = " be changed";
183 print ARRAY->[1];
184
185Dereferencing constant references incorrectly (such as using an array
186subscript on a constant hash reference, or vice versa) will be trapped at
187compile time.
188
189Constants belong to the package they are defined in. To refer to a
190constant defined in another package, specify the full package name, as
191in C<Some::Package::CONSTANT>. Constants may be exported by modules,
192and may also be called as either class or instance methods, that is,
193as C<< Some::Package->CONSTANT >> or as C<< $obj->CONSTANT >> where
194C<$obj> is an instance of C<Some::Package>. Subclasses may define
195their own constants to override those in their base class.
196
197The use of all caps for constant names is merely a convention,
198although it is recommended in order to make constants stand out
199and to help avoid collisions with other barewords, keywords, and
200subroutine names. Constant names must begin with a letter or
201underscore. Names beginning with a double underscore are reserved. Some
202poor choices for names will generate warnings, if warnings are enabled at
203compile time.
204
205=head2 List constants
206
207Constants may be lists of more (or less) than one value. A constant
208with no values evaluates to C<undef> in scalar context. Note that
209constants with more than one value do I<not> return their last value in
210scalar context as one might expect. They currently return the number
211of values, but B<this may change in the future>. Do not use constants
212with multiple values in scalar context.
213
214B<NOTE:> This implies that the expression defining the value of a
215constant is evaluated in list context. This may produce surprises:
216
217 use constant TIMESTAMP => localtime; # WRONG!
218 use constant TIMESTAMP => scalar localtime; # right
219
220The first line above defines C<TIMESTAMP> as a 9-element list, as
221returned by localtime() in list context. To set it to the string
222returned by localtime() in scalar context, an explicit C<scalar>
223keyword is required.
224
225List constants are lists, not arrays. To index or slice them, they
226must be placed in parentheses.
227
228 my @workdays = WEEKDAYS[1 .. 5]; # WRONG!
229 my @workdays = (WEEKDAYS)[1 .. 5]; # right
230
231=head2 Defining multiple constants at once
232
233Instead of writing multiple C<use constant> statements, you may define
234multiple constants in a single statement by giving, instead of the
235constant name, a reference to a hash where the keys are the names of
236the constants to be defined. Obviously, all constants defined using
237this method must have a single value.
238
239 use constant {
240 FOO => "A single value",
241 BAR => "This", "won't", "work!", # Error!
242 };
243
244This is a fundamental limitation of the way hashes are constructed in
245Perl. The error messages produced when this happens will often be
246quite cryptic -- in the worst case there may be none at all, and
247you'll only later find that something is broken.
248
249When defining multiple constants, you cannot use the values of other
250constants defined in the same declaration. This is because the
251calling package doesn't know about any constant within that group
252until I<after> the C<use> statement is finished.
253
254 use constant {
255 BITMASK => 0xAFBAEBA8,
256 NEGMASK => ~BITMASK, # Error!
257 };
258
259=head2 Magic constants
260
261Magical values and references can be made into constants at compile
262time, allowing for way cool stuff like this. (These error numbers
263aren't totally portable, alas.)
264
265 use constant E2BIG => ($! = 7);
266 print E2BIG, "\n"; # something like "Arg list too long"
267 print 0+E2BIG, "\n"; # "7"
268
269You can't produce a tied constant by giving a tied scalar as the
270value. References to tied variables, however, can be used as
271constants without any problems.
272
273=head1 TECHNICAL NOTES
274
275In the current implementation, scalar constants are actually
276inlinable subroutines. As of version 5.004 of Perl, the appropriate
277scalar constant is inserted directly in place of some subroutine
278calls, thereby saving the overhead of a subroutine call. See
279L<perlsub/"Constant Functions"> for details about how and when this
280happens.
281
282In the rare case in which you need to discover at run time whether a
283particular constant has been declared via this module, you may use
284this function to examine the hash C<%constant::declared>. If the given
285constant name does not include a package name, the current package is
286used.
287
288 sub declared ($) {
289 use constant 1.01; # don't omit this!
290 my $name = shift;
291 $name =~ s/^::/main::/;
292 my $pkg = caller;
293 my $full_name = $name =~ /::/ ? $name : "${pkg}::$name";
294 $constant::declared{$full_name};
295 }
296
297=head1 BUGS
298
299In the current version of Perl, list constants are not inlined
300and some symbols may be redefined without generating a warning.
301
302It is not possible to have a subroutine or a keyword with the same
303name as a constant in the same package. This is probably a Good Thing.
304
305A constant with a name in the list C<STDIN STDOUT STDERR ARGV ARGVOUT
306ENV INC SIG> is not allowed anywhere but in package C<main::>, for
307technical reasons.
308
309Unlike constants in some languages, these cannot be overridden
310on the command line or via environment variables.
311
312You can get into trouble if you use constants in a context which
313automatically quotes barewords (as is true for any subroutine call).
314For example, you can't say C<$hash{CONSTANT}> because C<CONSTANT> will
315be interpreted as a string. Use C<$hash{CONSTANT()}> or
316C<$hash{+CONSTANT}> to prevent the bareword quoting mechanism from
317kicking in. Similarly, since the C<< => >> operator quotes a bareword
318immediately to its left, you have to say C<< CONSTANT() => 'value' >>
319(or simply use a comma in place of the big arrow) instead of
320C<< CONSTANT => 'value' >>.
321
322=head1 AUTHOR
323
324Tom Phoenix, E<lt>F<rootbeer@redcat.com>E<gt>, with help from
325many other folks.
326
327Multiple constant declarations at once added by Casey West,
328E<lt>F<casey@geeknest.com>E<gt>.
329
330Documentation mostly rewritten by Ilmari Karonen,
331E<lt>F<perl@itz.pp.sci.fi>E<gt>.
332
333=head1 COPYRIGHT
334
335Copyright (C) 1997, 1999 Tom Phoenix
336
337This module is free software; you can redistribute it or modify it
338under the same terms as Perl itself.
339
340=cut
# spent 964µs within constant::CORE:match which was called 80 times, avg 12µs/call: # 80 times (964µs+0s) by constant::import at line 52 of constant.pm, avg 12µs/call
sub constant::CORE:match; # xsub