Commit | Line | Data |
0d863452 |
1 | package feature; |
2 | |
3 | our $VERSION = '1.00'; |
7168684c |
4 | $feature::hint_bits = 0x00020000; # HINT_LOCALIZE_HH |
0d863452 |
5 | |
6 | # (feature name) => (internal name, used in %^H) |
7 | my %feature = ( |
7b9ef140 |
8 | switch => 'feature_switch', |
9 | "~~" => "feature_~~", |
10 | say => "feature_say", |
bc9b29db |
11 | err => "feature_err", |
12 | ); |
13 | |
14 | my %feature_bundle = ( |
15 | "5.10" => [qw(switch ~~ say err)], |
0d863452 |
16 | ); |
17 | |
18 | |
19 | # Here are some notes that probably shouldn't be in the public |
20 | # documentation, but which it's useful to have somewhere. |
21 | # |
22 | # One side-effect of the change is that C<prototype("CORE::continue")> |
23 | # no longer throws the error C<Can't find an opnumber for "continue">. |
24 | # One of the tests in t/op/cproto.t had to be changed to accommodate |
25 | # this, but it really shouldn't affect real-world code. |
26 | # |
27 | # TODO: |
28 | # - sort out the smartmatch semantics |
29 | # - think about versioned features (use switch => 2) |
30 | # |
31 | # -- Robin 2005-12 |
32 | |
33 | =head1 NAME |
34 | |
35 | feature - Perl pragma to enable new syntactic features |
36 | |
37 | =head1 SYNOPSIS |
38 | |
bc9b29db |
39 | use feature qw(switch say); |
0d863452 |
40 | given ($foo) { |
bc9b29db |
41 | when (1) { say "\$foo == 1" } |
42 | when ([2,3]) { say "\$foo == 2 || \$foo == 3" } |
43 | when (/^a[bc]d$/) { say "\$foo eq 'abd' || \$foo eq 'acd'" } |
44 | when ($_ > 100) { say "\$foo > 100" } |
45 | default { say "None of the above" } |
0d863452 |
46 | } |
47 | |
48 | =head1 DESCRIPTION |
49 | |
50 | It is usually impossible to add new syntax to Perl without breaking |
51 | some existing programs. This pragma provides a way to minimize that |
52 | risk. New syntactic constructs can be enabled by C<use feature 'foo'>, |
53 | and will be parsed only when the appropriate feature pragma is in |
54 | scope. |
55 | |
56 | =head2 The 'switch' feature |
57 | |
58 | C<use feature 'switch'> tells the compiler to enable the Perl 6 |
59 | given/when construct from here to the end of the enclosing BLOCK. |
60 | |
61 | See L<perlsyn/"Switch statements"> for details. |
62 | |
63 | =head2 The '~~' feature |
64 | |
65 | C<use feature '~~'> tells the compiler to enable the Perl 6 |
66 | smart match C<~~> operator from here to the end of the enclosing BLOCK. |
67 | |
68 | See L<perlsyn/"Smart Matching in Detail"> for details. |
69 | |
70 | =head2 The 'say' feature |
71 | |
72 | C<use feature 'say'> tells the compiler to enable the Perl 6 |
73 | C<say> function from here to the end of the enclosing BLOCK. |
74 | |
75 | See L<perlfunc/say> for details. |
76 | |
bc9b29db |
77 | =head2 the 'err' feature |
78 | |
79 | C<use feature 'err'> tells the compiler to enable the C<err> |
80 | operator from here to the end of the enclosing BLOCK. |
81 | |
82 | C<err> is a low-precedence variant of the C<//> operator: |
83 | see C<perlop> for details. |
84 | |
85 | =head1 FEATURE BUNDLES |
86 | |
87 | It's possible to load a whole slew of features in one go, using |
88 | a I<feature bundle>. The name of a feature bundle is prefixed with |
89 | a colon, to distinguish it from an actual feature. At present, the |
90 | only feature bundle is C<use feature ":5.10">, which is equivalent |
91 | to C<use feature qw(switch ~~ say err)>. |
92 | |
0d863452 |
93 | =cut |
94 | |
95 | sub import { |
96 | $^H |= $feature::hint_bits; # Need this or %^H won't work |
97 | |
98 | my $class = shift; |
99 | if (@_ == 0) { |
100 | require Carp; |
101 | Carp->import("croak"); |
102 | croak("No features specified"); |
103 | } |
104 | while (@_) { |
105 | my $name = shift(@_); |
bc9b29db |
106 | if ($name =~ /^:(.*)/) { |
107 | if (!exists $feature_bundle{$1}) { |
108 | require Carp; |
109 | Carp->import("croak"); |
110 | croak(sprintf('Feature bundle "%s" is not supported by Perl %vd', |
111 | $1, $^V)); |
112 | } |
113 | unshift @_, @{$feature_bundle{$1}}; |
114 | next; |
115 | } |
0d863452 |
116 | if (!exists $feature{$name}) { |
117 | require Carp; |
118 | Carp->import("croak"); |
119 | croak(sprintf('Feature "%s" is not supported by Perl %vd', |
120 | $name, $^V)); |
121 | } |
122 | $^H{$feature{$name}} = 1; |
123 | } |
124 | } |
125 | |
126 | sub unimport { |
127 | my $class = shift; |
128 | |
129 | # A bare C<no feature> should disable *all* features |
bc9b29db |
130 | if (!@_) { |
131 | delete @^H{ values(%feature) }; |
132 | return; |
133 | } |
134 | |
135 | while (@_) { |
136 | my $name = shift; |
137 | if ($name =~ /^:(.*)/) { |
138 | if (!exists $feature_bundle{$1}) { |
139 | require Carp; |
140 | Carp->import("croak"); |
141 | croak(sprintf('Feature bundle "%s" is not supported by Perl %vd', |
142 | $1, $^V)); |
143 | } |
144 | unshift @_, @{$feature_bundle{$1}}; |
145 | next; |
146 | } |
0d863452 |
147 | if (!exists($feature{$name})) { |
148 | require Carp; |
149 | Carp->import("croak"); |
150 | croak(sprintf('Feature "%s" is not supported by Perl %vd', |
151 | $name, $^V)); |
152 | } |
153 | else { |
154 | delete $^H{$feature{$name}}; |
155 | } |
156 | } |
0d863452 |
157 | } |
158 | |
159 | 1; |