← Index
NYTProf Performance Profile   « block view • line view • sub view »
For t/app_dpath.t
  Run on Tue Jun 5 15:25:28 2012
Reported on Tue Jun 5 15:26:06 2012

Filename/home/ss5/perl5/perlbrew/perls/perl-5.14.1/lib/5.14.1/feature.pm
StatementsExecuted 14 statements in 150µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11156µs56µsfeature::::importfeature::import
0000s0sfeature::::croakfeature::croak
0000s0sfeature::::unimportfeature::unimport
0000s0sfeature::::unknown_featurefeature::unknown_feature
0000s0sfeature::::unknown_feature_bundlefeature::unknown_feature_bundle
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package feature;
2
313µsour $VERSION = '1.20';
4
5# (feature name) => (internal name, used in %^H)
6111µsmy %feature = (
7 switch => 'feature_switch',
8 say => "feature_say",
9 state => "feature_state",
10 unicode_strings => "feature_unicode",
11);
12
13# This gets set (for now) in $^H as well as in %^H,
14# for runtime speed of the uc/lc/ucfirst/lcfirst functions.
15# See HINT_UNI_8_BIT in perl.h.
1611µsour $hint_uni8bit = 0x00000800;
17
18# NB. the latest bundle must be loaded by the -E switch (see toke.c)
19
20129µsmy %feature_bundle = (
21 "5.10" => [qw(switch say state)],
22 "5.11" => [qw(switch say state unicode_strings)],
23 "5.12" => [qw(switch say state unicode_strings)],
24 "5.13" => [qw(switch say state unicode_strings)],
25 "5.14" => [qw(switch say state unicode_strings)],
26);
27
28# special case
2913µs$feature_bundle{"5.9.5"} = $feature_bundle{"5.10"};
30
31# TODO:
32# - think about versioned features (use feature switch => 2)
33
34=head1 NAME
35
36feature - Perl pragma to enable new features
37
38=head1 SYNOPSIS
39
40 use feature qw(switch say);
41 given ($foo) {
42 when (1) { say "\$foo == 1" }
43 when ([2,3]) { say "\$foo == 2 || \$foo == 3" }
44 when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" }
45 when ($_ > 100) { say "\$foo > 100" }
46 default { say "None of the above" }
47 }
48
49 use feature ':5.10'; # loads all features available in perl 5.10
50
51=head1 DESCRIPTION
52
53It is usually impossible to add new syntax to Perl without breaking
54some existing programs. This pragma provides a way to minimize that
55risk. New syntactic constructs, or new semantic meanings to older
56constructs, can be enabled by C<use feature 'foo'>, and will be parsed
57only when the appropriate feature pragma is in scope.
58
59=head2 Lexical effect
60
61Like other pragmas (C<use strict>, for example), features have a lexical
62effect. C<use feature qw(foo)> will only make the feature "foo" available
63from that point to the end of the enclosing block.
64
65 {
66 use feature 'say';
67 say "say is available here";
68 }
69 print "But not here.\n";
70
71=head2 C<no feature>
72
73Features can also be turned off by using C<no feature "foo">. This too
74has lexical effect.
75
76 use feature 'say';
77 say "say is available here";
78 {
79 no feature 'say';
80 print "But not here.\n";
81 }
82 say "Yet it is here.";
83
84C<no feature> with no features specified will turn off all features.
85
86=head2 The 'switch' feature
87
88C<use feature 'switch'> tells the compiler to enable the Perl 6
89given/when construct.
90
91See L<perlsyn/"Switch statements"> for details.
92
93=head2 The 'say' feature
94
95C<use feature 'say'> tells the compiler to enable the Perl 6
96C<say> function.
97
98See L<perlfunc/say> for details.
99
100=head2 the 'state' feature
101
102C<use feature 'state'> tells the compiler to enable C<state>
103variables.
104
105See L<perlsub/"Persistent Private Variables"> for details.
106
107=head2 the 'unicode_strings' feature
108
109C<use feature 'unicode_strings'> tells the compiler to use Unicode semantics
110in all string operations executed within its scope (unless they are also
111within the scope of either C<use locale> or C<use bytes>). The same applies
112to all regular expressions compiled within the scope, even if executed outside
113it.
114
115C<no feature 'unicode_strings'> tells the compiler to use the traditional
116Perl semantics wherein the native character set semantics is used unless it is
117clear to Perl that Unicode is desired. This can lead to some surprises
118when the behavior suddenly changes. (See
119L<perlunicode/The "Unicode Bug"> for details.) For this reason, if you are
120potentially using Unicode in your program, the
121C<use feature 'unicode_strings'> subpragma is B<strongly> recommended.
122
123This subpragma is available starting with Perl 5.11.3, but was not fully
124implemented until 5.13.8.
125
126=head1 FEATURE BUNDLES
127
128It's possible to load a whole slew of features in one go, using
129a I<feature bundle>. The name of a feature bundle is prefixed with
130a colon, to distinguish it from an actual feature. At present, the
131only feature bundle is C<use feature ":5.10"> which is equivalent
132to C<use feature qw(switch say state)>.
133
134Specifying sub-versions such as the C<0> in C<5.10.0> in feature bundles has
135no effect: feature bundles are guaranteed to be the same for all sub-versions.
136
137=head1 IMPLICIT LOADING
138
139There are two ways to load the C<feature> pragma implicitly :
140
141=over 4
142
143=item *
144
145By using the C<-E> switch on the command-line instead of C<-e>. It enables
146all available features in the main compilation unit (that is, the one-liner.)
147
148=item *
149
150By requiring explicitly a minimal Perl version number for your program, with
151the C<use VERSION> construct, and when the version is higher than or equal to
1525.10.0. That is,
153
154 use 5.10.0;
155
156will do an implicit
157
158 use feature ':5.10';
159
160and so on. Note how the trailing sub-version is automatically stripped from the
161version.
162
163But to avoid portability warnings (see L<perlfunc/use>), you may prefer:
164
165 use 5.010;
166
167with the same effect.
168
169=back
170
171=cut
172
173
# spent 56µs within feature::import which was called: # once (56µs+0s) by File::Glob::BEGIN@7 at line 7 of File/Glob.pm
sub import {
174334µs my $class = shift;
175 if (@_ == 0) {
176 croak("No features specified");
177 }
178 while (@_) {
179537µs my $name = shift(@_);
180 if (substr($name, 0, 1) eq ":") {
181 my $v = substr($name, 1);
182 if (!exists $feature_bundle{$v}) {
183 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
184 if (!exists $feature_bundle{$v}) {
185 unknown_feature_bundle(substr($name, 1));
186 }
187 }
188 unshift @_, @{$feature_bundle{$v}};
189 next;
190 }
191 if (!exists $feature{$name}) {
192 unknown_feature($name);
193 }
194 $^H{$feature{$name}} = 1;
195 $^H |= $hint_uni8bit if $name eq 'unicode_strings';
196 }
197}
198
199sub unimport {
200 my $class = shift;
201
202 # A bare C<no feature> should disable *all* features
203 if (!@_) {
204 delete @^H{ values(%feature) };
205 $^H &= ~ $hint_uni8bit;
206 return;
207 }
208
209 while (@_) {
210 my $name = shift;
211 if (substr($name, 0, 1) eq ":") {
212 my $v = substr($name, 1);
213 if (!exists $feature_bundle{$v}) {
214 $v =~ s/^([0-9]+)\.([0-9]+).[0-9]+$/$1.$2/;
215 if (!exists $feature_bundle{$v}) {
216 unknown_feature_bundle(substr($name, 1));
217 }
218 }
219 unshift @_, @{$feature_bundle{$v}};
220 next;
221 }
222 if (!exists($feature{$name})) {
223 unknown_feature($name);
224 }
225 else {
226 delete $^H{$feature{$name}};
227 $^H &= ~ $hint_uni8bit if $name eq 'unicode_strings';
228 }
229 }
230}
231
232sub unknown_feature {
233 my $feature = shift;
234 croak(sprintf('Feature "%s" is not supported by Perl %vd',
235 $feature, $^V));
236}
237
238sub unknown_feature_bundle {
239 my $feature = shift;
240 croak(sprintf('Feature bundle "%s" is not supported by Perl %vd',
241 $feature, $^V));
242}
243
244sub croak {
245 require Carp;
246 Carp::croak(@_);
247}
248
249131µs1;